    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
    023/**
    024 * Compares two files using the <b>default</b> {@link File#compareTo(File)} method.
    025 * <p>
    026 * This comparator can be used to sort lists or arrays of files
    027 * by using the default file comparison.
    028 * </p>
    029 * <p>
    030 * Example of sorting a list of files using the
    031 * {@link #DEFAULT_COMPARATOR} singleton instance:
    032 * </p>
    033 * <pre>
    034 *       List&lt;File&gt; list = ...
    035 *       ((AbstractFileComparator) DefaultFileComparator.DEFAULT_COMPARATOR).sort(list);
    036 * </pre>
    037 * <p>
    038 * Example of doing a <i>reverse</i> sort of an array of files using the
    039 * {@link #DEFAULT_REVERSE} singleton instance:
    040 * </p>
    041 * <pre>
    042 *       File[] array = ...
    043 *       ((AbstractFileComparator) DefaultFileComparator.DEFAULT_REVERSE).sort(array);
    044 * </pre>
    045 * <h2>Deprecating Serialization</h2>
    046 * <p>
    047 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    048 * </p>
    049 *
    050 * @since 1.4
    051 */
    052public class DefaultFileComparator extends AbstractFileComparator implements Serializable {
    053
    054    private static final long serialVersionUID = 3260141861365313518L;
    055
    056    /** Singleton default comparator instance */
    057    public static final Comparator<File> DEFAULT_COMPARATOR = new DefaultFileComparator();
    058
    059    /** Singleton reverse default comparator instance */
    060    public static final Comparator<File> DEFAULT_REVERSE = new ReverseFileComparator(DEFAULT_COMPARATOR);
    061
    062    /**
    063     * Constructs a new instance.
    064     */
    065    public DefaultFileComparator() {
    066        // empty
    067    }
    068
    069    /**
    070     * Compares the two files using the {@link File#compareTo(File)} method.
    071     *
    072     * @param file1 The first file to compare
    073     * @param file2 The second file to compare
    074     * @return the result of calling file1's
    075     * {@link File#compareTo(File)} with file2 as the parameter.
    076     */
    077    @Override
    078    public int compare(final File file1, final File file2) {
    079        return file1.compareTo(file2);
    080    }
    081}