AbstractFileComparator.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.util.Arrays;
  20. import java.util.Comparator;
  21. import java.util.List;

  22. /**
  23.  * Abstract file {@link Comparator} which provides sorting for file arrays and lists.
  24.  *
  25.  * @since 2.0
  26.  */
  27. abstract class AbstractFileComparator implements Comparator<File> {

  28.     /**
  29.      * Sorts an array of files.
  30.      * <p>
  31.      * This method uses {@link Arrays#sort(Object[], Comparator)} and returns the original array.
  32.      * </p>
  33.      *
  34.      * @param files The files to sort, may be null.
  35.      * @return The sorted array.
  36.      * @since 2.0
  37.      */
  38.     public File[] sort(final File... files) {
  39.         if (files != null) {
  40.             Arrays.sort(files, this);
  41.         }
  42.         return files;
  43.     }

  44.     /**
  45.      * Sorts a List of files.
  46.      * <p>
  47.      * This method uses {@link List#sort(Comparator)} and returns the original list.
  48.      * </p>
  49.      *
  50.      * @param files The files to sort, may be null.
  51.      * @return The sorted list.
  52.      * @since 2.0
  53.      */
  54.     public List<File> sort(final List<File> files) {
  55.         if (files != null) {
  56.             files.sort(this);
  57.         }
  58.         return files;
  59.     }

  60.     /**
  61.      * String representation of this file comparator.
  62.      *
  63.      * @return String representation of this file comparator.
  64.      */
  65.     @Override
  66.     public String toString() {
  67.         return getClass().getSimpleName();
  68.     }
  69. }