1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.vfs2.filter; 18 19 import java.io.Serializable; 20 21 import org.apache.commons.vfs2.FileContent; 22 import org.apache.commons.vfs2.FileFilter; 23 import org.apache.commons.vfs2.FileObject; 24 import org.apache.commons.vfs2.FileSelectInfo; 25 import org.apache.commons.vfs2.FileSystemException; 26 import org.apache.commons.vfs2.FileType; 27 28 /** 29 * This filter accepts files or directories that are empty. 30 * <p> 31 * If the {@code File} is a directory it checks that it contains no files. 32 * </p> 33 * <p> 34 * Example, showing how to print out a list of the current directory's empty files/directories: 35 * </p> 36 * 37 * <pre> 38 * FileSystemManager fsManager = VFS.getManager(); 39 * FileObject dir = fsManager.toFileObject(new File(".")); 40 * FileObject[] files = dir.findFiles(new FileFilterSelector(EmptyFileFilter.EMPTY)); 41 * for (int i = 0; i < files.length; i++) { 42 * System.out.println(files[i]); 43 * } 44 * </pre> 45 * 46 * <p> 47 * Example, showing how to print out a list of the current directory's non-empty files/directories: 48 * </p> 49 * 50 * <pre> 51 * FileSystemManager fsManager = VFS.getManager(); 52 * FileObject dir = fsManager.toFileObject(new File(".")); 53 * FileObject[] files = dir.findFiles(new FileFilterSelector(EmptyFileFilter.NOT_EMPTY)); 54 * for (int i = 0; i < files.length; i++) { 55 * System.out.println(files[i]); 56 * } 57 * </pre> 58 * 59 * @author This code was originally ported from Apache Commons IO File Filter 60 * @see "http://commons.apache.org/proper/commons-io/" 61 * @since 2.4 62 */ 63 public class EmptyFileFilter implements FileFilter, Serializable { 64 65 private static final long serialVersionUID = 1L; 66 67 /** Singleton instance of <i>empty</i> filter. */ 68 public static final FileFilter EMPTY = new EmptyFileFilter(); 69 70 /** Singleton instance of <i>not-empty</i> filter. */ 71 public static final FileFilter NOT_EMPTY = new NotFileFilter(EMPTY); 72 73 /** 74 * Restrictive constructor. 75 */ 76 protected EmptyFileFilter() { 77 } 78 79 /** 80 * Checks to see if the file is empty. A non-existing file is also considered empty. 81 * 82 * @param fileSelectInfo the file or directory to check 83 * 84 * @return {@code true} if the file or directory is <i>empty</i>, otherwise {@code false}. 85 * @throws FileSystemException Thrown for file system errors. 86 */ 87 @Override 88 public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException { 89 try (final FileObject file = fileSelectInfo.getFile()) { 90 if (!file.exists()) { 91 return true; 92 } 93 if (file.getType() == FileType.FOLDER) { 94 final FileObject[] files = file.getChildren(); 95 return files == null || files.length == 0; 96 } 97 try (final FileContent content = file.getContent()) { 98 return content.isEmpty(); 99 } 100 } 101 } 102 103 }