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<File> 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 * @version $Id: DefaultFileComparator.java 1642757 2014-12-01 21:09:30Z sebb $ 045 * @since 1.4 046 */ 047public class DefaultFileComparator extends AbstractFileComparator implements Serializable { 048 049 private static final long serialVersionUID = 3260141861365313518L; 050 051 /** Singleton default comparator instance */ 052 public static final Comparator<File> DEFAULT_COMPARATOR = new DefaultFileComparator(); 053 054 /** Singleton reverse default comparator instance */ 055 public static final Comparator<File> DEFAULT_REVERSE = new ReverseComparator(DEFAULT_COMPARATOR); 056 057 /** 058 * Compare the two files using the {@link File#compareTo(File)} method. 059 * 060 * @param file1 The first file to compare 061 * @param file2 The second file to compare 062 * @return the result of calling file1's 063 * {@link File#compareTo(File)} with file2 as the parameter. 064 */ 065 public int compare(final File file1, final File file2) { 066 return file1.compareTo(file2); 067 } 068}