Coverage Report - org.apache.commons.io.comparator.SizeFileComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
SizeFileComparator
100%
26/26
87%
14/16
3.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.comparator;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.Serializable;
 21  
 import java.util.Comparator;
 22  
 
 23  
 import org.apache.commons.io.FileUtils;
 24  
 
 25  
 /**
 26  
  * Compare the <b>length/size</b> of two files for order (see
 27  
  * {@link File#length()} and {@link FileUtils#sizeOfDirectory(File)}).
 28  
  * <p>
 29  
  * This comparator can be used to sort lists or arrays of files
 30  
  * by their length/size.
 31  
  * <p>
 32  
  * Example of sorting a list of files using the
 33  
  * {@link #SIZE_COMPARATOR} singleton instance:
 34  
  * <pre>
 35  
  *       List&lt;File&gt; list = ...
 36  
  *       ((AbstractFileComparator) SizeFileComparator.SIZE_COMPARATOR).sort(list);
 37  
  * </pre>
 38  
  * <p>
 39  
  * Example of doing a <i>reverse</i> sort of an array of files using the
 40  
  * {@link #SIZE_REVERSE} singleton instance:
 41  
  * <pre>
 42  
  *       File[] array = ...
 43  
  *       ((AbstractFileComparator) SizeFileComparator.SIZE_REVERSE).sort(array);
 44  
  * </pre>
 45  
  * <p>
 46  
  * <strong>N.B.</strong> Directories are treated as <b>zero size</b> unless
 47  
  * <code>sumDirectoryContents</code> is {@code true}.
 48  
  *
 49  
  * @version $Id: SizeFileComparator.java 1469107 2013-04-17 23:45:53Z sebb $
 50  
  * @since 1.4
 51  
  */
 52  78
 public class SizeFileComparator extends AbstractFileComparator implements Serializable {
 53  
 
 54  
     /** Size comparator instance - directories are treated as zero size */
 55  4
     public static final Comparator<File> SIZE_COMPARATOR = new SizeFileComparator();
 56  
 
 57  
     /** Reverse size comparator instance - directories are treated as zero size */
 58  4
     public static final Comparator<File> SIZE_REVERSE = new ReverseComparator(SIZE_COMPARATOR);
 59  
 
 60  
     /**
 61  
      * Size comparator instance which sums the size of a directory's contents
 62  
      * using {@link FileUtils#sizeOfDirectory(File)}
 63  
      */
 64  4
     public static final Comparator<File> SIZE_SUMDIR_COMPARATOR = new SizeFileComparator(true);
 65  
 
 66  
     /**
 67  
      * Reverse size comparator instance which sums the size of a directory's contents
 68  
      * using {@link FileUtils#sizeOfDirectory(File)}
 69  
      */
 70  4
     public static final Comparator<File> SIZE_SUMDIR_REVERSE = new ReverseComparator(SIZE_SUMDIR_COMPARATOR);
 71  
 
 72  
     /** Whether the sum of the directory's contents should be calculated. */
 73  
     private final boolean sumDirectoryContents;
 74  
 
 75  
     /**
 76  
      * Construct a file size comparator instance (directories treated as zero size).
 77  
      */
 78  4
     public SizeFileComparator() {
 79  4
         this.sumDirectoryContents = false;
 80  4
     }
 81  
 
 82  
     /**
 83  
      * Construct a file size comparator instance specifying whether the size of
 84  
      * the directory contents should be aggregated.
 85  
      * <p>
 86  
      * If the <code>sumDirectoryContents</code> is {@code true} The size of
 87  
      * directories is calculated using  {@link FileUtils#sizeOfDirectory(File)}.
 88  
      *
 89  
      * @param sumDirectoryContents {@code true} if the sum of the directoryies contents
 90  
      *  should be calculated, otherwise {@code false} if directories should be treated
 91  
      *  as size zero (see {@link FileUtils#sizeOfDirectory(File)}).
 92  
      */
 93  4
     public SizeFileComparator(final boolean sumDirectoryContents) {
 94  4
         this.sumDirectoryContents = sumDirectoryContents;
 95  4
     }
 96  
 
 97  
     /**
 98  
      * Compare the length of two files.
 99  
      * 
 100  
      * @param file1 The first file to compare
 101  
      * @param file2 The second file to compare
 102  
      * @return a negative value if the first file's length
 103  
      * is less than the second, zero if the lengths are the
 104  
      * same and a positive value if the first files length
 105  
      * is greater than the second file.
 106  
      * 
 107  
      */
 108  
     public int compare(final File file1, final File file2) {
 109  70
         long size1 = 0;
 110  70
         if (file1.isDirectory()) {
 111  6
             size1 = sumDirectoryContents && file1.exists() ? FileUtils.sizeOfDirectory(file1) : 0;
 112  
         } else {
 113  64
             size1 = file1.length();
 114  
         }
 115  70
         long size2 = 0;
 116  70
         if (file2.isDirectory()) {
 117  6
             size2 = sumDirectoryContents && file2.exists() ? FileUtils.sizeOfDirectory(file2) : 0;
 118  
         } else {
 119  64
             size2 = file2.length();
 120  
         }
 121  70
         final long result = size1 - size2;
 122  70
         if (result < 0) {
 123  38
             return -1;
 124  32
         } else if (result > 0) {
 125  16
             return 1;
 126  
         } else {
 127  16
             return 0;
 128  
         }
 129  
     }
 130  
 
 131  
     /**
 132  
      * String representation of this file comparator.
 133  
      *
 134  
      * @return String representation of this file comparator
 135  
      */
 136  
     @Override
 137  
     public String toString() {
 138  8
         return super.toString() + "[sumDirectoryContents=" + sumDirectoryContents + "]";
 139  
     }
 140  
 }