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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.io.comparator;
20
import java.io.Serializable;
21
import java.util.Comparator;
23
import org.apache.commons.io.FileUtils;
26
* Compare the <b>length/size</b> of two files for order (see
27
* {@link File#length()} and {@link FileUtils#sizeOfDirectory(File)}).
29
* This comparator can be used to sort lists or arrays of files
30
* by their length/size.
32
* Example of sorting a list of files using the
33
* {@link #SIZE_COMPARATOR} singleton instance:
35
* List<File> list = ...
36
* Collections.sort(list, LengthFileComparator.LENGTH_COMPARATOR);
39
* Example of doing a <i>reverse</i> sort of an array of files using the
40
* {@link #SIZE_REVERSE} singleton instance:
43
* Arrays.sort(array, LengthFileComparator.LENGTH_REVERSE);
46
* <strong>N.B.</strong> Directories are treated as <b>zero size</b> unless
47
* <code>sumDirectoryContents</code> is <code>true</code>.
49
* @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $
50
* @since Commons IO 1.4
52
public class SizeFileComparator implements Comparator, Serializable {
54
/** Size comparator instance - directories are treated as zero size */
55
public static final Comparator SIZE_COMPARATOR = new SizeFileComparator();
57
/** Reverse size comparator instance - directories are treated as zero size */
58
public static final Comparator SIZE_REVERSE = new ReverseComparator(SIZE_COMPARATOR);
61
* Size comparator instance which sums the size of a directory's contents
62
* using {@link FileUtils#sizeOfDirectory(File)}
64
public static final Comparator SIZE_SUMDIR_COMPARATOR = new SizeFileComparator(true);
67
* Reverse size comparator instance which sums the size of a directory's contents
68
* using {@link FileUtils#sizeOfDirectory(File)}
70
public static final Comparator SIZE_SUMDIR_REVERSE = new ReverseComparator(SIZE_SUMDIR_COMPARATOR);
72
/** Whether the sum of the directory's contents should be calculated. */
73
private final boolean sumDirectoryContents;
76
* Construct a file size comparator instance (directories treated as zero size).
78
public SizeFileComparator() {
79
this.sumDirectoryContents = false;
83
* Construct a file size comparator instance specifying whether the size of
84
* the directory contents should be aggregated.
86
* If the <code>sumDirectoryContents</code> is <code>true</code> The size of
87
* directories is calculated using {@link FileUtils#sizeOfDirectory(File)}.
89
* @param sumDirectoryContents <code>true</code> if the sum of the directoryies contents
90
* should be calculated, otherwise <code>false</code> if directories should be treated
91
* as size zero (see {@link FileUtils#sizeOfDirectory(File)}).
93
public SizeFileComparator(boolean sumDirectoryContents) {
94
this.sumDirectoryContents = sumDirectoryContents;
98
* Compare the length of two files.
100
* @param obj1 The first file to compare
101
* @param obj2 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.
108
public int compare(Object obj1, Object obj2) {
109
File file1 = (File)obj1;
110
File file2 = (File)obj2;
112
if (file1.isDirectory()) {
113
size1 = sumDirectoryContents && file1.exists() ? FileUtils.sizeOfDirectory(file1) : 0;
115
size1 = file1.length();
118
if (file2.isDirectory()) {
119
size2 = sumDirectoryContents && file2.exists() ? FileUtils.sizeOfDirectory(file2) : 0;
121
size2 = file2.length();
123
long result = size1 - size2;
126
} else if (result > 0) {