Coverage Report - org.apache.commons.io.filefilter.EmptyFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
EmptyFileFilter
100%
8/8
87%
7/8
2.5
 
 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  
 import java.io.Serializable;
 21  
 
 22  
 /**
 23  
  * This filter accepts files or directories that are empty.
 24  
  * <p>
 25  
  * If the <code>File</code> is a directory it checks that
 26  
  * it contains no files.
 27  
  * <p>
 28  
  * Example, showing how to print out a list of the
 29  
  * current directory's empty files/directories:
 30  
  *
 31  
  * <pre>
 32  
  * File dir = new File(".");
 33  
  * String[] files = dir.list( EmptyFileFilter.EMPTY );
 34  
  * for ( int i = 0; i &lt; files.length; i++ ) {
 35  
  *     System.out.println(files[i]);
 36  
  * }
 37  
  * </pre>
 38  
  *
 39  
  * <p>
 40  
  * Example, showing how to print out a list of the
 41  
  * current directory's non-empty files/directories:
 42  
  *
 43  
  * <pre>
 44  
  * File dir = new File(".");
 45  
  * String[] files = dir.list( EmptyFileFilter.NOT_EMPTY );
 46  
  * for ( int i = 0; i &lt; files.length; i++ ) {
 47  
  *     System.out.println(files[i]);
 48  
  * }
 49  
  * </pre>
 50  
  *
 51  
  * @since 1.3
 52  
  * @version $Id: EmptyFileFilter.java 1642757 2014-12-01 21:09:30Z sebb $
 53  
  */
 54  
 public class EmptyFileFilter extends AbstractFileFilter implements Serializable {
 55  
 
 56  
     private static final long serialVersionUID = 3631422087512832211L;
 57  
 
 58  
     /** Singleton instance of <i>empty</i> filter */
 59  1
     public static final IOFileFilter EMPTY = new EmptyFileFilter();
 60  
 
 61  
     /** Singleton instance of <i>not-empty</i> filter */
 62  1
     public static final IOFileFilter NOT_EMPTY = new NotFileFilter(EMPTY);
 63  
 
 64  
     /**
 65  
      * Restrictive consructor.
 66  
      */
 67  1
     protected EmptyFileFilter() {
 68  1
     }
 69  
 
 70  
     /**
 71  
      * Checks to see if the file is empty.
 72  
      *
 73  
      * @param file  the file or directory to check
 74  
      * @return {@code true} if the file or directory
 75  
      *  is <i>empty</i>, otherwise {@code false}.
 76  
      */
 77  
     @Override
 78  
     public boolean accept(final File file) {
 79  16
         if (file.isDirectory()) {
 80  8
             final File[] files = file.listFiles();
 81  8
             return files == null || files.length == 0;
 82  
         } else {
 83  8
             return file.length() == 0;
 84  
         }
 85  
     }
 86  
 
 87  
 }