NameFileComparator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.comparator;

  18. import java.io.File;
  19. import java.io.Serializable;
  20. import java.util.Comparator;

  21. import org.apache.commons.io.IOCase;

  22. /**
  23.  * Compare the <strong>names</strong> of two files for order (see {@link File#getName()}).
  24.  * <p>
  25.  * This comparator can be used to sort lists or arrays of files
  26.  * by their name either in a case-sensitive, case-insensitive or
  27.  * system dependent case-sensitive way. A number of singleton instances
  28.  * are provided for the various case sensitivity options (using {@link IOCase})
  29.  * and the reverse of those options.
  30.  * </p>
  31.  * <p>
  32.  * Example of a <em>case-sensitive</em> file name sort using the
  33.  * {@link #NAME_COMPARATOR} singleton instance:
  34.  * </p>
  35.  * <pre>
  36.  *       List&lt;File&gt; list = ...
  37.  *       ((AbstractFileComparator) NameFileComparator.NAME_COMPARATOR).sort(list);
  38.  * </pre>
  39.  * <p>
  40.  * Example of a <em>reverse case-insensitive</em> file name sort using the
  41.  * {@link #NAME_INSENSITIVE_REVERSE} singleton instance:
  42.  * </p>
  43.  * <pre>
  44.  *       File[] array = ...
  45.  *       ((AbstractFileComparator) NameFileComparator.NAME_INSENSITIVE_REVERSE).sort(array);
  46.  * </pre>
  47.  * <h2>Deprecating Serialization</h2>
  48.  * <p>
  49.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  50.  * </p>
  51.  *
  52.  * @since 1.4
  53.  */
  54. public class NameFileComparator extends AbstractFileComparator implements Serializable {

  55.     private static final long serialVersionUID = 8397947749814525798L;

  56.     /** Case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */
  57.     public static final Comparator<File> NAME_COMPARATOR = new NameFileComparator();

  58.     /** Reverse case-sensitive name comparator instance (see {@link IOCase#SENSITIVE}) */
  59.     public static final Comparator<File> NAME_REVERSE = new ReverseFileComparator(NAME_COMPARATOR);

  60.     /** Case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */
  61.     public static final Comparator<File> NAME_INSENSITIVE_COMPARATOR = new NameFileComparator(IOCase.INSENSITIVE);

  62.     /** Reverse case-insensitive name comparator instance (see {@link IOCase#INSENSITIVE}) */
  63.     public static final Comparator<File> NAME_INSENSITIVE_REVERSE = new ReverseFileComparator(NAME_INSENSITIVE_COMPARATOR);

  64.     /** System sensitive name comparator instance (see {@link IOCase#SYSTEM}) */
  65.     public static final Comparator<File> NAME_SYSTEM_COMPARATOR = new NameFileComparator(IOCase.SYSTEM);

  66.     /** Reverse system sensitive name comparator instance (see {@link IOCase#SYSTEM}) */
  67.     public static final Comparator<File> NAME_SYSTEM_REVERSE = new ReverseFileComparator(NAME_SYSTEM_COMPARATOR);

  68.     /** Whether the comparison is case-sensitive. */
  69.     private final IOCase ioCase;

  70.     /**
  71.      * Constructs a case-sensitive file name comparator instance.
  72.      */
  73.     public NameFileComparator() {
  74.         this.ioCase = IOCase.SENSITIVE;
  75.     }

  76.     /**
  77.      * Constructs a file name comparator instance with the specified case-sensitivity.
  78.      *
  79.      * @param ioCase  how to handle case sensitivity, null means case-sensitive
  80.      */
  81.     public NameFileComparator(final IOCase ioCase) {
  82.         this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
  83.     }

  84.     /**
  85.      * Compares the names of two files with the specified case sensitivity.
  86.      *
  87.      * @param file1 The first file to compare
  88.      * @param file2 The second file to compare
  89.      * @return a negative value if the first file's name
  90.      * is less than the second, zero if the names are the
  91.      * same and a positive value if the first files name
  92.      * is greater than the second file.
  93.      */
  94.     @Override
  95.     public int compare(final File file1, final File file2) {
  96.         return ioCase.checkCompareTo(file1.getName(), file2.getName());
  97.     }

  98.     /**
  99.      * String representation of this file comparator.
  100.      *
  101.      * @return String representation of this file comparator
  102.      */
  103.     @Override
  104.     public String toString() {
  105.         return super.toString() + "[ioCase=" + ioCase + "]";
  106.     }
  107. }