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