View Javadoc
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  
19  import java.io.File;
20  import java.util.Arrays;
21  import java.util.Comparator;
22  import java.util.List;
23  
24  /**
25   * Abstract file {@link Comparator} which provides sorting for file arrays and lists.
26   *
27   * @since 2.0
28   */
29  abstract class AbstractFileComparator implements Comparator<File> {
30  
31      /**
32       * Sorts an array of files.
33       * <p>
34       * This method uses {@link Arrays#sort(Object[], Comparator)} and returns the original array.
35       * </p>
36       *
37       * @param files The files to sort, may be null.
38       * @return The sorted array.
39       * @since 2.0
40       */
41      public File[] sort(final File... files) {
42          if (files != null) {
43              Arrays.sort(files, this);
44          }
45          return files;
46      }
47  
48      /**
49       * Sorts a List of files.
50       * <p>
51       * This method uses {@link List#sort(Comparator)} and returns the original list.
52       * </p>
53       *
54       * @param files The files to sort, may be null.
55       * @return The sorted list.
56       * @since 2.0
57       */
58      public List<File> sort(final List<File> files) {
59          if (files != null) {
60              files.sort(this);
61          }
62          return files;
63      }
64  
65      /**
66       * String representation of this file comparator.
67       *
68       * @return String representation of this file comparator.
69       */
70      @Override
71      public String toString() {
72          return getClass().getSimpleName();
73      }
74  }