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 files or directories that are empty.
024     * <p>
025     * If the <code>File</code> is a directory it checks that
026     * it contains no files.
027     * <p>
028     * Example, showing how to print out a list of the 
029     * current directory's empty files/directories:
030     *
031     * <pre>
032     * File dir = new File(".");
033     * String[] files = dir.list( EmptyFileFilter.EMPTY );
034     * for ( int i = 0; i &lt; files.length; i++ ) {
035     *     System.out.println(files[i]);
036     * }
037     * </pre>
038     *
039     * <p>
040     * Example, showing how to print out a list of the 
041     * current directory's non-empty files/directories:
042     *
043     * <pre>
044     * File dir = new File(".");
045     * String[] files = dir.list( EmptyFileFilter.NOT_EMPTY );
046     * for ( int i = 0; i &lt; files.length; i++ ) {
047     *     System.out.println(files[i]);
048     * }
049     * </pre>
050     *
051     * @since 1.3
052     * @version $Id: EmptyFileFilter.java 1307462 2012-03-30 15:13:11Z ggregory $
053     */
054    public class EmptyFileFilter extends AbstractFileFilter implements Serializable {
055        
056        /** Singleton instance of <i>empty</i> filter */
057        public static final IOFileFilter EMPTY = new EmptyFileFilter();
058        
059        /** Singleton instance of <i>not-empty</i> filter */
060        public static final IOFileFilter NOT_EMPTY = new NotFileFilter(EMPTY);
061        
062        /**
063         * Restrictive consructor.
064         */
065        protected EmptyFileFilter() {
066        }
067        
068        /**
069         * Checks to see if the file is empty.
070         * 
071         * @param file  the file or directory to check
072         * @return {@code true} if the file or directory
073         *  is <i>empty</i>, otherwise {@code false}.
074         */
075        @Override
076        public boolean accept(File file) {
077            if (file.isDirectory()) {
078                File[] files = file.listFiles();
079                return files == null || files.length == 0;
080            } else {
081                return file.length() == 0;
082            }
083        }
084        
085    }