    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;
    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 *
    053 */
    054public class EmptyFileFilter extends AbstractFileFilter implements Serializable {
    055
    056    private static final long serialVersionUID = 3631422087512832211L;
    057
    058    /** Singleton instance of <i>empty</i> filter */
    059    public static final IOFileFilter EMPTY = new EmptyFileFilter();
    060
    061    /** Singleton instance of <i>not-empty</i> filter */
    062    public static final IOFileFilter NOT_EMPTY = new NotFileFilter(EMPTY);
    063
    064    /**
    065     * Restrictive constructor.
    066     */
    067    protected EmptyFileFilter() {
    068    }
    069
    070    /**
    071     * Checks to see if the file is empty.
    072     *
    073     * @param file  the file or directory to check
    074     * @return {@code true} if the file or directory
    075     *  is <i>empty</i>, otherwise {@code false}.
    076     */
    077    @Override
    078    public boolean accept(final File file) {
    079        if (file.isDirectory()) {
    080            final File[] files = file.listFiles();
    081            return files == null || files.length == 0;
    082        }
    083        return file.length() == 0;
    084    }
    085
    086}