01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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(
60: EMPTY);
61:
62: /**
63: * Restrictive consructor.
64: */
65: protected EmptyFileFilter() {
66: }
67:
68: /**
69: * Checks to see if the file is empty.
70: *
71: * @param file the file or directory to check
72: * @return <code>true</code> if the file or directory
73: * is <i>empty</i>, otherwise <code>false</code>.
74: */
75: public boolean accept(File file) {
76: if (file.isDirectory()) {
77: File[] files = file.listFiles();
78: return (files == null || files.length == 0);
79: } else {
80: return (file.length() == 0);
81: }
82: }
83:
84: }
|