DefaultFileComparator.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. /**
  22.  * Compares two files using the <strong>default</strong> {@link File#compareTo(File)} method.
  23.  * <p>
  24.  * This comparator can be used to sort lists or arrays of files
  25.  * by using the default file comparison.
  26.  * </p>
  27.  * <p>
  28.  * Example of sorting a list of files using the
  29.  * {@link #DEFAULT_COMPARATOR} singleton instance:
  30.  * </p>
  31.  * <pre>
  32.  *       List&lt;File&gt; list = ...
  33.  *       ((AbstractFileComparator) DefaultFileComparator.DEFAULT_COMPARATOR).sort(list);
  34.  * </pre>
  35.  * <p>
  36.  * Example of doing a <em>reverse</em> sort of an array of files using the
  37.  * {@link #DEFAULT_REVERSE} singleton instance:
  38.  * </p>
  39.  * <pre>
  40.  *       File[] array = ...
  41.  *       ((AbstractFileComparator) DefaultFileComparator.DEFAULT_REVERSE).sort(array);
  42.  * </pre>
  43.  * <h2>Deprecating Serialization</h2>
  44.  * <p>
  45.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  46.  * </p>
  47.  *
  48.  * @since 1.4
  49.  */
  50. public class DefaultFileComparator extends AbstractFileComparator implements Serializable {

  51.     private static final long serialVersionUID = 3260141861365313518L;

  52.     /** Singleton default comparator instance */
  53.     public static final Comparator<File> DEFAULT_COMPARATOR = new DefaultFileComparator();

  54.     /** Singleton reverse default comparator instance */
  55.     public static final Comparator<File> DEFAULT_REVERSE = new ReverseFileComparator(DEFAULT_COMPARATOR);

  56.     /**
  57.      * Constructs a new instance.
  58.      */
  59.     public DefaultFileComparator() {
  60.         // empty
  61.     }

  62.     /**
  63.      * Compares the two files using the {@link File#compareTo(File)} method.
  64.      *
  65.      * @param file1 The first file to compare
  66.      * @param file2 The second file to compare
  67.      * @return the result of calling file1's
  68.      * {@link File#compareTo(File)} with file2 as the parameter.
  69.      */
  70.     @Override
  71.     public int compare(final File file1, final File file2) {
  72.         return file1.compareTo(file2);
  73.     }
  74. }