SizeFileComparator.java

  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. import java.io.File;
  19. import java.io.Serializable;
  20. import java.util.Comparator;

  21. import org.apache.commons.io.FileUtils;

  22. /**
  23.  * Compare the <strong>length/size</strong> of two files for order (see
  24.  * {@link File#length()} and {@link FileUtils#sizeOfDirectory(File)}).
  25.  * <p>
  26.  * This comparator can be used to sort lists or arrays of files
  27.  * by their length/size.
  28.  * </p>
  29.  * <p>
  30.  * Example of sorting a list of files using the
  31.  * {@link #SIZE_COMPARATOR} singleton instance:
  32.  * </p>
  33.  * <pre>
  34.  *       List&lt;File&gt; list = ...
  35.  *       ((AbstractFileComparator) SizeFileComparator.SIZE_COMPARATOR).sort(list);
  36.  * </pre>
  37.  * <p>
  38.  * Example of doing a <em>reverse</em> sort of an array of files using the
  39.  * {@link #SIZE_REVERSE} singleton instance:
  40.  * </p>
  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 <strong>zero size</strong> unless
  47.  * {@code sumDirectoryContents} is {@code true}.
  48.  * </p>
  49.  * <h2>Deprecating Serialization</h2>
  50.  * <p>
  51.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  52.  * </p>
  53.  *
  54.  * @since 1.4
  55.  */
  56. public class SizeFileComparator extends AbstractFileComparator implements Serializable {

  57.     private static final long serialVersionUID = -1201561106411416190L;

  58.     /** Size comparator instance - directories are treated as zero size */
  59.     public static final Comparator<File> SIZE_COMPARATOR = new SizeFileComparator();

  60.     /** Reverse size comparator instance - directories are treated as zero size */
  61.     public static final Comparator<File> SIZE_REVERSE = new ReverseFileComparator(SIZE_COMPARATOR);

  62.     /**
  63.      * Size comparator instance which sums the size of a directory's contents
  64.      * using {@link FileUtils#sizeOfDirectory(File)}
  65.      */
  66.     public static final Comparator<File> SIZE_SUMDIR_COMPARATOR = new SizeFileComparator(true);

  67.     /**
  68.      * Reverse size comparator instance which sums the size of a directory's contents
  69.      * using {@link FileUtils#sizeOfDirectory(File)}
  70.      */
  71.     public static final Comparator<File> SIZE_SUMDIR_REVERSE = new ReverseFileComparator(SIZE_SUMDIR_COMPARATOR);

  72.     /** Whether the sum of the directory's contents should be calculated. */
  73.     private final boolean sumDirectoryContents;

  74.     /**
  75.      * Constructs a file size comparator instance (directories treated as zero size).
  76.      */
  77.     public SizeFileComparator() {
  78.         this.sumDirectoryContents = false;
  79.     }

  80.     /**
  81.      * Constructs a file size comparator instance specifying whether the size of
  82.      * the directory contents should be aggregated.
  83.      * <p>
  84.      * If the {@code sumDirectoryContents} is {@code true} The size of
  85.      * directories is calculated using  {@link FileUtils#sizeOfDirectory(File)}.
  86.      * </p>
  87.      *
  88.      * @param sumDirectoryContents {@code true} if the sum of the directories' contents
  89.      *  should be calculated, otherwise {@code false} if directories should be treated
  90.      *  as size zero (see {@link FileUtils#sizeOfDirectory(File)}).
  91.      */
  92.     public SizeFileComparator(final boolean sumDirectoryContents) {
  93.         this.sumDirectoryContents = sumDirectoryContents;
  94.     }

  95.     /**
  96.      * Compares the length of two files.
  97.      *
  98.      * @param file1 The first file to compare
  99.      * @param file2 The second file to compare
  100.      * @return a negative value if the first file's length
  101.      * is less than the second, zero if the lengths are the
  102.      * same and a positive value if the first files length
  103.      * is greater than the second file.
  104.      */
  105.     @Override
  106.     public int compare(final File file1, final File file2) {
  107.         final long size1;
  108.         if (file1.isDirectory()) {
  109.             size1 = sumDirectoryContents && file1.exists() ? FileUtils.sizeOfDirectory(file1) : 0;
  110.         } else {
  111.             size1 = file1.length();
  112.         }
  113.         final long size2;
  114.         if (file2.isDirectory()) {
  115.             size2 = sumDirectoryContents && file2.exists() ? FileUtils.sizeOfDirectory(file2) : 0;
  116.         } else {
  117.             size2 = file2.length();
  118.         }
  119.         final long result = size1 - size2;
  120.         if (result < 0) {
  121.             return -1;
  122.         }
  123.         if (result > 0) {
  124.             return 1;
  125.         }
  126.         return 0;
  127.     }

  128.     /**
  129.      * String representation of this file comparator.
  130.      *
  131.      * @return String representation of this file comparator
  132.      */
  133.     @Override
  134.     public String toString() {
  135.         return super.toString() + "[sumDirectoryContents=" + sumDirectoryContents + "]";
  136.     }
  137. }