View Javadoc
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   * <p>
33   * Example of sorting a list of files using the
34   * {@link #SIZE_COMPARATOR} singleton instance:
35   * </p>
36   * <pre>
37   *       List&lt;File&gt; list = ...
38   *       ((AbstractFileComparator) SizeFileComparator.SIZE_COMPARATOR).sort(list);
39   * </pre>
40   * <p>
41   * Example of doing a <i>reverse</i> sort of an array of files using the
42   * {@link #SIZE_REVERSE} singleton instance:
43   * </p>
44   * <pre>
45   *       File[] array = ...
46   *       ((AbstractFileComparator) SizeFileComparator.SIZE_REVERSE).sort(array);
47   * </pre>
48   * <p>
49   * <strong>N.B.</strong> Directories are treated as <b>zero size</b> unless
50   * {@code sumDirectoryContents} is {@code true}.
51   * </p>
52   * <h2>Deprecating Serialization</h2>
53   * <p>
54   * <em>Serialization is deprecated and will be removed in 3.0.</em>
55   * </p>
56   *
57   * @since 1.4
58   */
59  public class SizeFileComparator extends AbstractFileComparator implements Serializable {
60  
61      private static final long serialVersionUID = -1201561106411416190L;
62  
63      /** Size comparator instance - directories are treated as zero size */
64      public static final Comparator<File> SIZE_COMPARATOR = new SizeFileComparator();
65  
66      /** Reverse size comparator instance - directories are treated as zero size */
67      public static final Comparator<File> SIZE_REVERSE = new ReverseFileComparator(SIZE_COMPARATOR);
68  
69      /**
70       * Size comparator instance which sums the size of a directory's contents
71       * using {@link FileUtils#sizeOfDirectory(File)}
72       */
73      public static final Comparator<File> SIZE_SUMDIR_COMPARATOR = new SizeFileComparator(true);
74  
75      /**
76       * Reverse size comparator instance which sums the size of a directory's contents
77       * using {@link FileUtils#sizeOfDirectory(File)}
78       */
79      public static final Comparator<File> SIZE_SUMDIR_REVERSE = new ReverseFileComparator(SIZE_SUMDIR_COMPARATOR);
80  
81      /** Whether the sum of the directory's contents should be calculated. */
82      private final boolean sumDirectoryContents;
83  
84      /**
85       * Constructs a file size comparator instance (directories treated as zero size).
86       */
87      public SizeFileComparator() {
88          this.sumDirectoryContents = false;
89      }
90  
91      /**
92       * Constructs a file size comparator instance specifying whether the size of
93       * the directory contents should be aggregated.
94       * <p>
95       * If the {@code sumDirectoryContents} is {@code true} The size of
96       * directories is calculated using  {@link FileUtils#sizeOfDirectory(File)}.
97       * </p>
98       *
99       * @param sumDirectoryContents {@code true} if the sum of the directories' contents
100      *  should be calculated, otherwise {@code false} if directories should be treated
101      *  as size zero (see {@link FileUtils#sizeOfDirectory(File)}).
102      */
103     public SizeFileComparator(final boolean sumDirectoryContents) {
104         this.sumDirectoryContents = sumDirectoryContents;
105     }
106 
107     /**
108      * Compares the length of two files.
109      *
110      * @param file1 The first file to compare
111      * @param file2 The second file to compare
112      * @return a negative value if the first file's length
113      * is less than the second, zero if the lengths are the
114      * same and a positive value if the first files length
115      * is greater than the second file.
116      */
117     @Override
118     public int compare(final File file1, final File file2) {
119         final long size1;
120         if (file1.isDirectory()) {
121             size1 = sumDirectoryContents && file1.exists() ? FileUtils.sizeOfDirectory(file1) : 0;
122         } else {
123             size1 = file1.length();
124         }
125         final long size2;
126         if (file2.isDirectory()) {
127             size2 = sumDirectoryContents && file2.exists() ? FileUtils.sizeOfDirectory(file2) : 0;
128         } else {
129             size2 = file2.length();
130         }
131         final long result = size1 - size2;
132         if (result < 0) {
133             return -1;
134         }
135         if (result > 0) {
136             return 1;
137         }
138         return 0;
139     }
140 
141     /**
142      * String representation of this file comparator.
143      *
144      * @return String representation of this file comparator
145      */
146     @Override
147     public String toString() {
148         return super.toString() + "[sumDirectoryContents=" + sumDirectoryContents + "]";
149     }
150 }