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     */
017    package org.apache.commons.io.filefilter;
018    
019    import java.io.File;
020    import java.io.Serializable;
021    
022    /**
023     * This filter accepts <code>File</code>s that can be read.
024     * <p>
025     * Example, showing how to print out a list of the 
026     * current directory's <i>readable</i> files:
027     *
028     * <pre>
029     * File dir = new File(".");
030     * String[] files = dir.list( CanReadFileFilter.CAN_READ );
031     * for ( int i = 0; i &lt; files.length; i++ ) {
032     *     System.out.println(files[i]);
033     * }
034     * </pre>
035     *
036     * <p>
037     * Example, showing how to print out a list of the 
038     * current directory's <i>un-readable</i> files:
039     *
040     * <pre>
041     * File dir = new File(".");
042     * String[] files = dir.list( CanReadFileFilter.CANNOT_READ );
043     * for ( int i = 0; i &lt; files.length; i++ ) {
044     *     System.out.println(files[i]);
045     * }
046     * </pre>
047     *
048     * <p>
049     * Example, showing how to print out a list of the 
050     * current directory's <i>read-only</i> files:
051     *
052     * <pre>
053     * File dir = new File(".");
054     * String[] files = dir.list( CanReadFileFilter.READ_ONLY );
055     * for ( int i = 0; i &lt; files.length; i++ ) {
056     *     System.out.println(files[i]);
057     * }
058     * </pre>
059     *
060     * @since 1.3
061     * @version $Id: CanReadFileFilter.java 1307462 2012-03-30 15:13:11Z ggregory $
062     */
063    public class CanReadFileFilter extends AbstractFileFilter implements Serializable {
064        
065        /** Singleton instance of <i>readable</i> filter */
066        public static final IOFileFilter CAN_READ = new CanReadFileFilter();
067    
068        /** Singleton instance of not <i>readable</i> filter */
069        public static final IOFileFilter CANNOT_READ = new NotFileFilter(CAN_READ);
070        
071        /** Singleton instance of <i>read-only</i> filter */
072        public static final IOFileFilter READ_ONLY = new AndFileFilter(CAN_READ,
073                                                    CanWriteFileFilter.CANNOT_WRITE);
074        
075        /**
076         * Restrictive consructor.
077         */
078        protected CanReadFileFilter() {
079        }
080        
081        /**
082         * Checks to see if the file can be read.
083         * 
084         * @param file  the File to check.
085         * @return {@code true} if the file can be
086         *  read, otherwise {@code false}.
087         */
088        @Override
089        public boolean accept(File file) {
090            return file.canRead();
091        }
092        
093    }