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.io.Serializable;
21  import java.util.Comparator;
22  import java.util.function.IntFunction;
23  import java.util.stream.Stream;
24  import java.util.stream.StreamSupport;
25  
26  /**
27   * Compare two files using a set of delegate file {@link Comparator}.
28   * <p>
29   * This comparator can be used to sort lists or arrays of files by combining a number of other comparators.
30   * <p>
31   * Example of sorting a list of files by type (i.e. directory or file) and then by name:
32   *
33   * <pre>
34   *       CompositeFileComparator comparator = new CompositeFileComparator(
35   *           DirectoryFileComparator.DIRECTORY_COMPARATOR,
36   *           NameFileComparator.NAME_COMPARATOR);
37   *       List&lt;File&gt; list = ...
38   *       comparator.sort(list);
39   * </pre>
40   * <h2>Deprecating Serialization</h2>
41   * <p>
42   * <em>Serialization is deprecated and will be removed in 3.0.</em>
43   * </p>
44   *
45   * @since 2.0
46   */
47  public class CompositeFileComparator extends AbstractFileComparator implements Serializable {
48  
49      private static final Comparator<?>[] EMPTY_COMPARATOR_ARRAY = {};
50      private static final long serialVersionUID = -2224170307287243428L;
51  
52      /**
53       * Delegates.
54       */
55      private final Comparator<File>[] delegates;
56  
57      /**
58       * Constructs a composite comparator for the set of delegate comparators.
59       *
60       * @param delegates The delegate file comparators
61       */
62      public CompositeFileComparator(@SuppressWarnings("unchecked") final Comparator<File>... delegates) {
63          this.delegates = delegates == null ? emptyArray() : delegates.clone();
64      }
65  
66      /**
67       * Constructs a composite comparator for the set of delegate comparators.
68       *
69       * @param delegates The delegate file comparators
70       */
71      public CompositeFileComparator(final Iterable<Comparator<File>> delegates) {
72          this.delegates = delegates == null ? emptyArray()
73                  : StreamSupport.stream(delegates.spliterator(), false).toArray((IntFunction<Comparator<File>[]>) Comparator[]::new);
74      }
75  
76      /**
77       * Compares the two files using delegate comparators.
78       *
79       * @param file1 The first file to compare
80       * @param file2 The second file to compare
81       * @return the first non-zero result returned from the delegate comparators or zero.
82       */
83      @Override
84      public int compare(final File file1, final File file2) {
85          return Stream.of(delegates).map(delegate -> delegate.compare(file1, file2)).filter(r -> r != 0).findFirst().orElse(0);
86      }
87  
88      @SuppressWarnings("unchecked") // types are already correct
89      private Comparator<File>[] emptyArray() {
90          return (Comparator<File>[]) EMPTY_COMPARATOR_ARRAY;
91      }
92  
93      /**
94       * String representation of this file comparator.
95       *
96       * @return String representation of this file comparator
97       */
98      @Override
99      public String toString() {
100         final StringBuilder builder = new StringBuilder();
101         builder.append(super.toString());
102         builder.append('{');
103         for (int i = 0; i < delegates.length; i++) {
104             if (i > 0) {
105                 builder.append(',');
106             }
107             builder.append(delegates[i]);
108         }
109         builder.append('}');
110         return builder.toString();
111     }
112 }