    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 * Compare 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 * Example of sorting a list of files using the
    030 * {@link #DEFAULT_COMPARATOR} singleton instance:
    031 * <pre>
    032 *       List&lt;File&gt; list = ...
    033 *       ((AbstractFileComparator) DefaultFileComparator.DEFAULT_COMPARATOR).sort(list);
    034 * </pre>
    035 * <p>
    036 * Example of doing a <i>reverse</i> sort of an array of files using the
    037 * {@link #DEFAULT_REVERSE} singleton instance:
    038 * <pre>
    039 *       File[] array = ...
    040 *       ((AbstractFileComparator) DefaultFileComparator.DEFAULT_REVERSE).sort(array);
    041 * </pre>
    042 * <p>
    043 *
    044 * @since 1.4
    045 */
    046public class DefaultFileComparator extends AbstractFileComparator implements Serializable {
    047
    048    private static final long serialVersionUID = 3260141861365313518L;
    049
    050    /** Singleton default comparator instance */
    051    public static final Comparator<File> DEFAULT_COMPARATOR = new DefaultFileComparator();
    052
    053    /** Singleton reverse default comparator instance */
    054    public static final Comparator<File> DEFAULT_REVERSE = new ReverseFileComparator(DEFAULT_COMPARATOR);
    055
    056    /**
    057     * Compare the two files using the {@link File#compareTo(File)} method.
    058     *
    059     * @param file1 The first file to compare
    060     * @param file2 The second file to compare
    061     * @return the result of calling file1's
    062     * {@link File#compareTo(File)} with file2 as the parameter.
    063     */
    064    @Override
    065    public int compare(final File file1, final File file2) {
    066        return file1.compareTo(file2);
    067    }
    068}