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 023import org.apache.commons.io.IOCase; 024 025/** 026 * Compare the <b>names</b> of two files for order (see {@link File#getName()}). 027 * <p> 028 * This comparator can be used to sort lists or arrays of files 029 * by their name either in a case-sensitive, case-insensitive or 030 * system dependent case sensitive way. A number of singleton instances 031 * are provided for the various case sensitivity options (using {@link IOCase}) 032 * and the reverse of those options. 033 * <p> 034 * Example of a <i>case-sensitive</i> file name sort using the 035 * {@link #NAME_COMPARATOR} singleton instance: 036 * <pre> 037 * List<File> list = ... 038 * ((AbstractFileComparator) NameFileComparator.NAME_COMPARATOR).sort(list); 039 * </pre> 040 * <p> 041 * Example of a <i>reverse case-insensitive</i> file name sort using the 042 * {@link #NAME_INSENSITIVE_REVERSE} singleton instance: 043 * <pre> 044 * File[] array = ... 045 * ((AbstractFileComparator) NameFileComparator.NAME_INSENSITIVE_REVERSE).sort(array); 046 * </pre> 047 * <p> 048 * 049 * @version $Id: NameFileComparator.java 1642757 2014-12-01 21:09:30Z sebb $ 050 * @since 1.4 051 */ 052public class NameFileComparator extends AbstractFileComparator implements Serializable { 053 054 private static final long serialVersionUID = 8397947749814525798L; 055 056 /** Case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */ 057 public static final Comparator<File> NAME_COMPARATOR = new NameFileComparator(); 058 059 /** Reverse case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */ 060 public static final Comparator<File> NAME_REVERSE = new ReverseComparator(NAME_COMPARATOR); 061 062 /** Case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */ 063 public static final Comparator<File> NAME_INSENSITIVE_COMPARATOR = new NameFileComparator(IOCase.INSENSITIVE); 064 065 /** Reverse case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */ 066 public static final Comparator<File> NAME_INSENSITIVE_REVERSE = new ReverseComparator(NAME_INSENSITIVE_COMPARATOR); 067 068 /** System sensitive name comparator instance (see {@link IOCase#SYSTEM}) */ 069 public static final Comparator<File> NAME_SYSTEM_COMPARATOR = new NameFileComparator(IOCase.SYSTEM); 070 071 /** Reverse system sensitive name comparator instance (see {@link IOCase#SYSTEM}) */ 072 public static final Comparator<File> NAME_SYSTEM_REVERSE = new ReverseComparator(NAME_SYSTEM_COMPARATOR); 073 074 /** Whether the comparison is case sensitive. */ 075 private final IOCase caseSensitivity; 076 077 /** 078 * Construct a case sensitive file name comparator instance. 079 */ 080 public NameFileComparator() { 081 this.caseSensitivity = IOCase.SENSITIVE; 082 } 083 084 /** 085 * Construct a file name comparator instance with the specified case-sensitivity. 086 * 087 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive 088 */ 089 public NameFileComparator(final IOCase caseSensitivity) { 090 this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; 091 } 092 093 /** 094 * Compare the names of two files with the specified case sensitivity. 095 * 096 * @param file1 The first file to compare 097 * @param file2 The second file to compare 098 * @return a negative value if the first file's name 099 * is less than the second, zero if the names are the 100 * same and a positive value if the first files name 101 * is greater than the second file. 102 */ 103 public int compare(final File file1, final File file2) { 104 return caseSensitivity.checkCompareTo(file1.getName(), file2.getName()); 105 } 106 107 /** 108 * String representation of this file comparator. 109 * 110 * @return String representation of this file comparator 111 */ 112 @Override 113 public String toString() { 114 return super.toString() + "[caseSensitivity=" + caseSensitivity + "]"; 115 } 116}