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.vfs2.filter;
018
019import java.io.Serializable;
020
021import org.apache.commons.vfs2.FileFilter;
022import org.apache.commons.vfs2.FileSelectInfo;
023import org.apache.commons.vfs2.FileSystemException;
024
025/**
026 * This filter accepts {@code File}s that can be read.
027 * <p>
028 * Example, showing how to print out a list of the current directory's
029 * <em>readable</em> files:
030 * </p>
031 *
032 * <pre>
033 * FileSystemManager fsManager = VFS.getManager();
034 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
035 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanReadFileFilter.CAN_READ));
036 * for (int i = 0; i &lt; files.length; i++) {
037 *     System.out.println(files[i]);
038 * }
039 * </pre>
040 *
041 * <p>
042 * Example, showing how to print out a list of the current directory's
043 * <em>un-readable</em> files:
044 * </p>
045 *
046 * <pre>
047 * FileSystemManager fsManager = VFS.getManager();
048 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
049 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanReadFileFilter.CANNOT_READ));
050 * for (int i = 0; i &lt; files.length; i++) {
051 *     System.out.println(files[i]);
052 * }
053 * </pre>
054 *
055 * <p>
056 * Example, showing how to print out a list of the current directory's
057 * <em>read-only</em> files:
058 * </p>
059 *
060 * <pre>
061 * FileSystemManager fsManager = VFS.getManager();
062 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
063 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanReadFileFilter.READ_ONLY));
064 * for (int i = 0; i &lt; files.length; i++) {
065 *     System.out.println(files[i]);
066 * }
067 * </pre>
068 *
069 * @author This code was originally ported from Apache Commons IO File Filter
070 * @see "https://commons.apache.org/proper/commons-io/"
071 * @since 2.4
072 */
073public class CanReadFileFilter implements FileFilter, Serializable {
074
075    /** Singleton instance of <em>readable</em> filter. */
076    public static final FileFilter CAN_READ = new CanReadFileFilter();
077
078    /** Singleton instance of not <em>readable</em> filter. */
079    public static final FileFilter CANNOT_READ = new NotFileFilter(CAN_READ);
080
081    /** Singleton instance of <em>read-only</em> filter. */
082    public static final FileFilter READ_ONLY = new AndFileFilter(CAN_READ, CanWriteFileFilter.CANNOT_WRITE);
083
084    private static final long serialVersionUID = 1L;
085
086    /**
087     * Restrictive constructor.
088     */
089    protected CanReadFileFilter() {
090    }
091
092    /**
093     * Checks to see if the file can be read.
094     *
095     * @param fileSelectInfo the File to check.
096     * @return {@code true} if the file can be read, otherwise {@code false}.
097     * @throws FileSystemException Thrown for file system errors.
098     */
099    @Override
100    public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
101        return fileSelectInfo.getFile().isReadable();
102    }
103
104}