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 <strong>names</strong> 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 * <p> 035 * Example of a <em>case-sensitive</em> file name sort using the 036 * {@link #NAME_COMPARATOR} singleton instance: 037 * </p> 038 * <pre> 039 * List<File> list = ... 040 * ((AbstractFileComparator) NameFileComparator.NAME_COMPARATOR).sort(list); 041 * </pre> 042 * <p> 043 * Example of a <em>reverse case-insensitive</em> file name sort using the 044 * {@link #NAME_INSENSITIVE_REVERSE} singleton instance: 045 * </p> 046 * <pre> 047 * File[] array = ... 048 * ((AbstractFileComparator) NameFileComparator.NAME_INSENSITIVE_REVERSE).sort(array); 049 * </pre> 050 * <h2>Deprecating Serialization</h2> 051 * <p> 052 * <em>Serialization is deprecated and will be removed in 3.0.</em> 053 * </p> 054 * 055 * @since 1.4 056 */ 057public class NameFileComparator extends AbstractFileComparator implements Serializable { 058 059 private static final long serialVersionUID = 8397947749814525798L; 060 061 /** Case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */ 062 public static final Comparator<File> NAME_COMPARATOR = new NameFileComparator(); 063 064 /** Reverse case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */ 065 public static final Comparator<File> NAME_REVERSE = new ReverseFileComparator(NAME_COMPARATOR); 066 067 /** Case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */ 068 public static final Comparator<File> NAME_INSENSITIVE_COMPARATOR = new NameFileComparator(IOCase.INSENSITIVE); 069 070 /** Reverse case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */ 071 public static final Comparator<File> NAME_INSENSITIVE_REVERSE = new ReverseFileComparator(NAME_INSENSITIVE_COMPARATOR); 072 073 /** System sensitive name comparator instance (see {@link IOCase#SYSTEM}) */ 074 public static final Comparator<File> NAME_SYSTEM_COMPARATOR = new NameFileComparator(IOCase.SYSTEM); 075 076 /** Reverse system sensitive name comparator instance (see {@link IOCase#SYSTEM}) */ 077 public static final Comparator<File> NAME_SYSTEM_REVERSE = new ReverseFileComparator(NAME_SYSTEM_COMPARATOR); 078 079 /** Whether the comparison is case-sensitive. */ 080 private final IOCase ioCase; 081 082 /** 083 * Constructs a case-sensitive file name comparator instance. 084 */ 085 public NameFileComparator() { 086 this.ioCase = IOCase.SENSITIVE; 087 } 088 089 /** 090 * Constructs a file name comparator instance with the specified case-sensitivity. 091 * 092 * @param ioCase how to handle case sensitivity, null means case-sensitive 093 */ 094 public NameFileComparator(final IOCase ioCase) { 095 this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE); 096 } 097 098 /** 099 * Compares the names of two files with the specified case sensitivity. 100 * 101 * @param file1 The first file to compare 102 * @param file2 The second file to compare 103 * @return a negative value if the first file's name 104 * is less than the second, zero if the names are the 105 * same and a positive value if the first files name 106 * is greater than the second file. 107 */ 108 @Override 109 public int compare(final File file1, final File file2) { 110 return ioCase.checkCompareTo(file1.getName(), file2.getName()); 111 } 112 113 /** 114 * String representation of this file comparator. 115 * 116 * @return String representation of this file comparator 117 */ 118 @Override 119 public String toString() { 120 return super.toString() + "[ioCase=" + ioCase + "]"; 121 } 122}