ExtensionFileComparator.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.FilenameUtils;
  22. import org.apache.commons.io.IOCase;

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

  57.     private static final long serialVersionUID = 1928235200184222815L;

  58.     /** Case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
  59.     public static final Comparator<File> EXTENSION_COMPARATOR = new ExtensionFileComparator();

  60.     /** Reverse case-sensitive extension comparator instance (see {@link IOCase#SENSITIVE}) */
  61.     public static final Comparator<File> EXTENSION_REVERSE = new ReverseFileComparator(EXTENSION_COMPARATOR);

  62.     /** Case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
  63.     public static final Comparator<File> EXTENSION_INSENSITIVE_COMPARATOR
  64.                                                 = new ExtensionFileComparator(IOCase.INSENSITIVE);

  65.     /** Reverse case-insensitive extension comparator instance (see {@link IOCase#INSENSITIVE}) */
  66.     public static final Comparator<File> EXTENSION_INSENSITIVE_REVERSE
  67.                                                 = new ReverseFileComparator(EXTENSION_INSENSITIVE_COMPARATOR);

  68.     /** System sensitive extension comparator instance (see {@link IOCase#SYSTEM}) */
  69.     public static final Comparator<File> EXTENSION_SYSTEM_COMPARATOR = new ExtensionFileComparator(IOCase.SYSTEM);

  70.     /** Reverse system sensitive path comparator instance (see {@link IOCase#SYSTEM}) */
  71.     public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseFileComparator(EXTENSION_SYSTEM_COMPARATOR);

  72.     /** Whether the comparison is case-sensitive. */
  73.     private final IOCase ioCase;

  74.     /**
  75.      * Constructs a case-sensitive file extension comparator instance.
  76.      */
  77.     public ExtensionFileComparator() {
  78.         this.ioCase = IOCase.SENSITIVE;
  79.     }

  80.     /**
  81.      * Constructs a file extension comparator instance with the specified case-sensitivity.
  82.      *
  83.      * @param ioCase how to handle case sensitivity, null means case-sensitive
  84.      */
  85.     public ExtensionFileComparator(final IOCase ioCase) {
  86.         this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
  87.     }

  88.     /**
  89.      * Compares the extensions of two files the specified case sensitivity.
  90.      *
  91.      * @param file1 The first file to compare
  92.      * @param file2 The second file to compare
  93.      * @return a negative value if the first file's extension
  94.      * is less than the second, zero if the extensions are the
  95.      * same and a positive value if the first files extension
  96.      * is greater than the second file.
  97.      */
  98.     @Override
  99.     public int compare(final File file1, final File file2) {
  100.         final String suffix1 = FilenameUtils.getExtension(file1.getName());
  101.         final String suffix2 = FilenameUtils.getExtension(file2.getName());
  102.         return ioCase.checkCompareTo(suffix1, suffix2);
  103.     }

  104.     /**
  105.      * String representation of this file comparator.
  106.      *
  107.      * @return String representation of this file comparator
  108.      */
  109.     @Override
  110.     public String toString() {
  111.         return super.toString() + "[ioCase=" + ioCase + "]";
  112.     }
  113. }