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 */
017 package org.apache.commons.io.comparator;
018
019 import java.io.File;
020 import java.io.Serializable;
021 import 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<File> list = ...
033 * 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 * DefaultFileComparator.DEFAULT_REVERSE.sort(array);
041 * </pre>
042 * <p>
043 *
044 * @version $Id: DefaultFileComparator.java 1304052 2012-03-22 20:55:29Z ggregory $
045 * @since 1.4
046 */
047 public class DefaultFileComparator extends AbstractFileComparator implements Serializable {
048
049 /** Singleton default comparator instance */
050 public static final Comparator<File> DEFAULT_COMPARATOR = new DefaultFileComparator();
051
052 /** Singleton reverse default comparator instance */
053 public static final Comparator<File> DEFAULT_REVERSE = new ReverseComparator(DEFAULT_COMPARATOR);
054
055 /**
056 * Compare the two files using the {@link File#compareTo(File)} method.
057 *
058 * @param file1 The first file to compare
059 * @param file2 The second file to compare
060 * @return the result of calling file1's
061 * {@link File#compareTo(File)} with file2 as the parameter.
062 */
063 public int compare(File file1, File file2) {
064 return file1.compareTo(file2);
065 }
066 }