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 import org.apache.commons.io.FilenameUtils; 024 import org.apache.commons.io.IOCase; 025 026 /** 027 * Compare the file name <b>extensions</b> for order 028 * (see {@link FilenameUtils#getExtension(String)}). 029 * <p> 030 * This comparator can be used to sort lists or arrays of files 031 * by their file extension either in a case-sensitive, case-insensitive or 032 * system dependant case sensitive way. A number of singleton instances 033 * are provided for the various case sensitivity options (using {@link IOCase}) 034 * and the reverse of those options. 035 * <p> 036 * Example of a <i>case-sensitive</i> file extension sort using the 037 * {@link #EXTENSION_COMPARATOR} singleton instance: 038 * <pre> 039 * List<File> list = ... 040 * Collections.sort(list, ExtensionFileComparator.EXTENSION_COMPARATOR); 041 * </pre> 042 * <p> 043 * Example of a <i>reverse case-insensitive</i> file extension sort using the 044 * {@link #EXTENSION_INSENSITIVE_REVERSE} singleton instance: 045 * <pre> 046 * File[] array = ... 047 * Arrays.sort(array, ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE); 048 * </pre> 049 * <p> 050 * 051 * @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $ 052 * @since Commons IO 1.4 053 */ 054 public class ExtensionFileComparator implements Comparator, Serializable { 055 056 /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */ 057 public static final Comparator EXTENSION_COMPARATOR = new ExtensionFileComparator(); 058 059 /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */ 060 public static final Comparator EXTENSION_REVERSE = new ReverseComparator(EXTENSION_COMPARATOR); 061 062 /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */ 063 public static final Comparator EXTENSION_INSENSITIVE_COMPARATOR = new ExtensionFileComparator(IOCase.INSENSITIVE); 064 065 /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */ 066 public static final Comparator EXTENSION_INSENSITIVE_REVERSE 067 = new ReverseComparator(EXTENSION_INSENSITIVE_COMPARATOR); 068 069 /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */ 070 public static final Comparator EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM); 071 072 /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */ 073 public static final Comparator EXTENSION_SYSTEM_REVERSE = new ReverseComparator(EXTENSION_SYSTEM_COMPARATOR); 074 075 /** Whether the comparison is case sensitive. */ 076 private final IOCase caseSensitivity; 077 078 /** 079 * Construct a case sensitive file extension comparator instance. 080 */ 081 public ExtensionFileComparator() { 082 this.caseSensitivity = IOCase.SENSITIVE; 083 } 084 085 /** 086 * Construct a file extension comparator instance with the specified case-sensitivity. 087 * 088 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive 089 */ 090 public ExtensionFileComparator(IOCase caseSensitivity) { 091 this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; 092 } 093 094 /** 095 * Compare the extensions of two files the specified case sensitivity. 096 * 097 * @param obj1 The first file to compare 098 * @param obj2 The second file to compare 099 * @return a negative value if the first file's extension 100 * is less than the second, zero if the extensions are the 101 * same and a positive value if the first files extension 102 * is greater than the second file. 103 * 104 */ 105 public int compare(Object obj1, Object obj2) { 106 File file1 = (File)obj1; 107 File file2 = (File)obj2; 108 String suffix1 = FilenameUtils.getExtension(file1.getName()); 109 String suffix2 = FilenameUtils.getExtension(file2.getName()); 110 return caseSensitivity.checkCompareTo(suffix1, suffix2); 111 } 112 }