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  
  * @since 1.4
 50  
  */
 51  78
 public class SizeFileComparator extends AbstractFileComparator implements Serializable {
 52  
 
 53  
     private static final long serialVersionUID = -1201561106411416190L;
 54  
 
 55  
     /** Size comparator instance - directories are treated as zero size */
 56  4
     public static final Comparator<File> SIZE_COMPARATOR = new SizeFileComparator();
 57  
 
 58  
     /** Reverse size comparator instance - directories are treated as zero size */
 59  4
     public static final Comparator<File> SIZE_REVERSE = new ReverseComparator(SIZE_COMPARATOR);
 60  
 
 61  
     /**
 62  
      * Size comparator instance which sums the size of a directory's contents
 63  
      * using {@link FileUtils#sizeOfDirectory(File)}
 64  
      */
 65  4
     public static final Comparator<File> SIZE_SUMDIR_COMPARATOR = new SizeFileComparator(true);
 66  
 
 67  
     /**
 68  
      * Reverse size comparator instance which sums the size of a directory's contents
 69  
      * using {@link FileUtils#sizeOfDirectory(File)}
 70  
      */
 71  4
     public static final Comparator<File> SIZE_SUMDIR_REVERSE = new ReverseComparator(SIZE_SUMDIR_COMPARATOR);
 72  
 
 73  
     /** Whether the sum of the directory's contents should be calculated. */
 74  
     private final boolean sumDirectoryContents;
 75  
 
 76  
     /**
 77  
      * Construct a file size comparator instance (directories treated as zero size).
 78  
      */
 79  4
     public SizeFileComparator() {
 80  4
         this.sumDirectoryContents = false;
 81  4
     }
 82  
 
 83  
     /**
 84  
      * Construct a file size comparator instance specifying whether the size of
 85  
      * the directory contents should be aggregated.
 86  
      * <p>
 87  
      * If the <code>sumDirectoryContents</code> is {@code true} The size of
 88  
      * directories is calculated using  {@link FileUtils#sizeOfDirectory(File)}.
 89  
      *
 90  
      * @param sumDirectoryContents {@code true} if the sum of the directories' contents
 91  
      *  should be calculated, otherwise {@code false} if directories should be treated
 92  
      *  as size zero (see {@link FileUtils#sizeOfDirectory(File)}).
 93  
      */
 94  4
     public SizeFileComparator(final boolean sumDirectoryContents) {
 95  4
         this.sumDirectoryContents = sumDirectoryContents;
 96  4
     }
 97  
 
 98  
     /**
 99  
      * Compare the length of two files.
 100  
      * 
 101  
      * @param file1 The first file to compare
 102  
      * @param file2 The second file to compare
 103  
      * @return a negative value if the first file's length
 104  
      * is less than the second, zero if the lengths are the
 105  
      * same and a positive value if the first files length
 106  
      * is greater than the second file.
 107  
      * 
 108  
      */
 109  
     @Override
 110  
     public int compare(final File file1, final File file2) {
 111  70
         long size1 = 0;
 112  70
         if (file1.isDirectory()) {
 113  6
             size1 = sumDirectoryContents && file1.exists() ? FileUtils.sizeOfDirectory(file1) : 0;
 114  
         } else {
 115  64
             size1 = file1.length();
 116  
         }
 117  70
         long size2 = 0;
 118  70
         if (file2.isDirectory()) {
 119  6
             size2 = sumDirectoryContents && file2.exists() ? FileUtils.sizeOfDirectory(file2) : 0;
 120  
         } else {
 121  64
             size2 = file2.length();
 122  
         }
 123  70
         final long result = size1 - size2;
 124  70
         if (result < 0) {
 125  38
             return -1;
 126  32
         } else if (result > 0) {
 127  16
             return 1;
 128  
         } else {
 129  16
             return 0;
 130  
         }
 131  
     }
 132  
 
 133  
     /**
 134  
      * String representation of this file comparator.
 135  
      *
 136  
      * @return String representation of this file comparator
 137  
      */
 138  
     @Override
 139  
     public String toString() {
 140  8
         return super.toString() + "[sumDirectoryContents=" + sumDirectoryContents + "]";
 141  
     }
 142  
 }