    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.comparator;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.util.Comparator;
    022
    023import org.apache.commons.io.FileUtils;
    024
    025/**
    026 * Compare the <b>length/size</b> of two files for order (see
    027 * {@link File#length()} and {@link FileUtils#sizeOfDirectory(File)}).
    028 * <p>
    029 * This comparator can be used to sort lists or arrays of files
    030 * by their length/size.
    031 * </p>
    032 * <p>
    033 * Example of sorting a list of files using the
    034 * {@link #SIZE_COMPARATOR} singleton instance:
    035 * </p>
    036 * <pre>
    037 *       List&lt;File&gt; list = ...
    038 *       ((AbstractFileComparator) SizeFileComparator.SIZE_COMPARATOR).sort(list);
    039 * </pre>
    040 * <p>
    041 * Example of doing a <i>reverse</i> sort of an array of files using the
    042 * {@link #SIZE_REVERSE} singleton instance:
    043 * </p>
    044 * <pre>
    045 *       File[] array = ...
    046 *       ((AbstractFileComparator) SizeFileComparator.SIZE_REVERSE).sort(array);
    047 * </pre>
    048 * <p>
    049 * <strong>N.B.</strong> Directories are treated as <b>zero size</b> unless
    050 * {@code sumDirectoryContents} is {@code true}.
    051 * </p>
    052 * <h2>Deprecating Serialization</h2>
    053 * <p>
    054 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    055 * </p>
    056 *
    057 * @since 1.4
    058 */
    059public class SizeFileComparator extends AbstractFileComparator implements Serializable {
    060
    061    private static final long serialVersionUID = -1201561106411416190L;
    062
    063    /** Size comparator instance - directories are treated as zero size */
    064    public static final Comparator<File> SIZE_COMPARATOR = new SizeFileComparator();
    065
    066    /** Reverse size comparator instance - directories are treated as zero size */
    067    public static final Comparator<File> SIZE_REVERSE = new ReverseFileComparator(SIZE_COMPARATOR);
    068
    069    /**
    070     * Size comparator instance which sums the size of a directory's contents
    071     * using {@link FileUtils#sizeOfDirectory(File)}
    072     */
    073    public static final Comparator<File> SIZE_SUMDIR_COMPARATOR = new SizeFileComparator(true);
    074
    075    /**
    076     * Reverse size comparator instance which sums the size of a directory's contents
    077     * using {@link FileUtils#sizeOfDirectory(File)}
    078     */
    079    public static final Comparator<File> SIZE_SUMDIR_REVERSE = new ReverseFileComparator(SIZE_SUMDIR_COMPARATOR);
    080
    081    /** Whether the sum of the directory's contents should be calculated. */
    082    private final boolean sumDirectoryContents;
    083
    084    /**
    085     * Constructs a file size comparator instance (directories treated as zero size).
    086     */
    087    public SizeFileComparator() {
    088        this.sumDirectoryContents = false;
    089    }
    090
    091    /**
    092     * Constructs a file size comparator instance specifying whether the size of
    093     * the directory contents should be aggregated.
    094     * <p>
    095     * If the {@code sumDirectoryContents} is {@code true} The size of
    096     * directories is calculated using  {@link FileUtils#sizeOfDirectory(File)}.
    097     * </p>
    098     *
    099     * @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     */
    118    @Override
    119    public int compare(final File file1, final File file2) {
    120        final long size1;
    121        if (file1.isDirectory()) {
    122            size1 = sumDirectoryContents && file1.exists() ? FileUtils.sizeOfDirectory(file1) : 0;
    123        } else {
    124            size1 = file1.length();
    125        }
    126        final long size2;
    127        if (file2.isDirectory()) {
    128            size2 = sumDirectoryContents && file2.exists() ? FileUtils.sizeOfDirectory(file2) : 0;
    129        } else {
    130            size2 = file2.length();
    131        }
    132        final long result = size1 - size2;
    133        if (result < 0) {
    134            return -1;
    135        }
    136        if (result > 0) {
    137            return 1;
    138        }
    139        return 0;
    140    }
    141
    142    /**
    143     * String representation of this file comparator.
    144     *
    145     * @return String representation of this file comparator
    146     */
    147    @Override
    148    public String toString() {
    149        return super.toString() + "[sumDirectoryContents=" + sumDirectoryContents + "]";
    150    }
    151}