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.io.filefilter;
18
19 import java.io.File;
20
21 /**
22 * This filter accepts files or directories that are empty.
23 * <p>
24 * If the <code>File</code> is a directory it checks that
25 * it contains no files.
26 * <p>
27 * Example, showing how to print out a list of the
28 * current directory's empty files/directories:
29 *
30 * <pre>
31 * File dir = new File(".");
32 * String[] files = dir.list( EmptyFileFilter.EMPTY );
33 * for ( int i = 0; i < files.length; i++ ) {
34 * System.out.println(files[i]);
35 * }
36 * </pre>
37 *
38 * <p>
39 * Example, showing how to print out a list of the
40 * current directory's non-empty files/directories:
41 *
42 * <pre>
43 * File dir = new File(".");
44 * String[] files = dir.list( EmptyFileFilter.NOT_EMPTY );
45 * for ( int i = 0; i < files.length; i++ ) {
46 * System.out.println(files[i]);
47 * }
48 * </pre>
49 *
50 * @since Commons IO 1.3
51 * @version $Revision: 437567 $
52 */
53 public class EmptyFileFilter extends AbstractFileFilter {
54
55 /** Singleton instance of <i>empty</i> filter */
56 public static final IOFileFilter EMPTY = new EmptyFileFilter();
57
58 /** Singleton instance of <i>not-empty</i> filter */
59 public static final IOFileFilter NOT_EMPTY = new NotFileFilter(EMPTY);
60
61 /**
62 * Restrictive consructor.
63 */
64 protected EmptyFileFilter() {
65 }
66
67 /**
68 * Checks to see if the file is empty.
69 *
70 * @param file the file or directory to check
71 * @return <code>true</code> if the file or directory
72 * is <i>empty</i>, otherwise <code>false</code>.
73 */
74 public boolean accept(File file) {
75 if (file.isDirectory()) {
76 File[] files = file.listFiles();
77 return (files == null || files.length == 0);
78 } else {
79 return (file.length() == 0);
80 }
81 }
82
83 }