14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/* 24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more 34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements. See the NOTICE file distributed with 44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership. 54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0 64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with 74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License. You may obtain a copy of the License at 84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * http://www.apache.org/licenses/LICENSE-2.0 104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software 124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS, 134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and 154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License. 164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.comparator; 184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.File; 204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.Serializable; 214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.util.Comparator; 224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.commons.io.IOCase; 244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/** 264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Compare the <b>names</b> of two files for order (see {@link File#getName()}). 274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p> 284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * This comparator can be used to sort lists or arrays of files 294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * by their name either in a case-sensitive, case-insensitive or 304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * system dependant case sensitive way. A number of singleton instances 314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * are provided for the various case sensitivity options (using {@link IOCase}) 324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * and the reverse of those options. 334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p> 344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Example of a <i>case-sensitive</i> file name sort using the 354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * {@link #NAME_COMPARATOR} singleton instance: 364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <pre> 374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * List<File> list = ... 384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Collections.sort(list, NameFileComparator.NAME_COMPARATOR); 394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * </pre> 404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p> 414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Example of a <i>reverse case-insensitive</i> file name sort using the 424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * {@link #NAME_INSENSITIVE_REVERSE} singleton instance: 434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <pre> 444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * File[] array = ... 454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Arrays.sort(array, NameFileComparator.NAME_INSENSITIVE_REVERSE); 464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * </pre> 474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p> 484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $ 504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @since Commons IO 1.4 514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class NameFileComparator implements Comparator<File>, Serializable { 534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** Case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */ 554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public static final Comparator<File> NAME_COMPARATOR = new NameFileComparator(); 564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** Reverse case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */ 584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public static final Comparator<File> NAME_REVERSE = new ReverseComparator<File>(NAME_COMPARATOR); 594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** Case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */ 614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public static final Comparator<File> NAME_INSENSITIVE_COMPARATOR = new NameFileComparator(IOCase.INSENSITIVE); 624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** Reverse case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */ 644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public static final Comparator<File> NAME_INSENSITIVE_REVERSE = new ReverseComparator<File>(NAME_INSENSITIVE_COMPARATOR); 654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** System sensitive name comparator instance (see {@link IOCase#SYSTEM}) */ 674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public static final Comparator<File> NAME_SYSTEM_COMPARATOR = new NameFileComparator(IOCase.SYSTEM); 684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** Reverse system sensitive name comparator instance (see {@link IOCase#SYSTEM}) */ 704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public static final Comparator<File> NAME_SYSTEM_REVERSE = new ReverseComparator<File>(NAME_SYSTEM_COMPARATOR); 714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** Whether the comparison is case sensitive. */ 734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy private final IOCase caseSensitivity; 744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Construct a case sensitive file name comparator instance. 774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public NameFileComparator() { 794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.caseSensitivity = IOCase.SENSITIVE; 804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Construct a file name comparator instance with the specified case-sensitivity. 844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param caseSensitivity how to handle case sensitivity, null means case-sensitive 864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public NameFileComparator(IOCase caseSensitivity) { 884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; 894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Compare the names of two files with the specified case sensitivity. 934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param obj1 The first file to compare 954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param obj2 The second file to compare 964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @return a negative value if the first file's name 974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * is less than the second, zero if the names are the 984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * same and a positive value if the first files name 994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * is greater than the second file. 1004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 1014fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public int compare(File file1, File file2) { 1024fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy return caseSensitivity.checkCompareTo(file1.getName(), file2.getName()); 1034fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 1044fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy} 105