001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.filefilter;
018
019import java.io.File;
020import java.io.Serializable;
021import java.nio.file.FileVisitResult;
022import java.nio.file.Files;
023import java.nio.file.Path;
024import java.nio.file.attribute.BasicFileAttributes;
025
026/**
027 * This filter accepts {@link File}s that can be read.
028 * <p>
029 * Example, showing how to print out a list of the current directory's <i>readable</i> files:
030 * </p>
031 * <h2>Using Classic IO</h2>
032 * <pre>
033 * File dir = FileUtils.current();
034 * String[] files = dir.list(CanReadFileFilter.CAN_READ);
035 * for (String file : files) {
036 *     System.out.println(file);
037 * }
038 * </pre>
039 *
040 * <p>
041 * Example, showing how to print out a list of the current directory's <i>un-readable</i> files:
042 *
043 * <pre>
044 * File dir = FileUtils.current();
045 * String[] files = dir.list(CanReadFileFilter.CANNOT_READ);
046 * for (String file : files) {
047 *     System.out.println(file);
048 * }
049 * </pre>
050 *
051 * <p>
052 * Example, showing how to print out a list of the current directory's <i>read-only</i> files:
053 *
054 * <pre>
055 * File dir = FileUtils.current();
056 * String[] files = dir.list(CanReadFileFilter.READ_ONLY);
057 * for (String file : files) {
058 *     System.out.println(file);
059 * }
060 * </pre>
061 * <h2>Deprecating Serialization</h2>
062 * <p>
063 * <em>Serialization is deprecated and will be removed in 3.0.</em>
064 * </p>
065 *
066 * @since 1.3
067 */
068public class CanReadFileFilter extends AbstractFileFilter implements Serializable {
069
070    /** Singleton instance of <i>readable</i> filter */
071    public static final IOFileFilter CAN_READ = new CanReadFileFilter();
072
073    /** Singleton instance of not <i>readable</i> filter */
074    public static final IOFileFilter CANNOT_READ = CAN_READ.negate();
075
076    /** Singleton instance of <i>read-only</i> filter */
077    public static final IOFileFilter READ_ONLY = CAN_READ.and(CanWriteFileFilter.CANNOT_WRITE);
078
079    private static final long serialVersionUID = 3179904805251622989L;
080
081    /**
082     * Restrictive constructor.
083     */
084    protected CanReadFileFilter() {
085    }
086
087    /**
088     * Checks to see if the file can be read.
089     *
090     * @param file the File to check.
091     * @return {@code true} if the file can be read, otherwise {@code false}.
092     */
093    @Override
094    public boolean accept(final File file) {
095        return file != null && file.canRead();
096    }
097
098    /**
099     * Checks to see if the file can be read.
100     * @param file the File to check.
101     *
102     * @return {@code true} if the file can be read, otherwise {@code false}.
103     * @since 2.9.0
104     */
105    @Override
106    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
107        return toFileVisitResult(file != null && Files.isReadable(file));
108    }
109
110}