    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.comparator;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.util.Comparator;
    022import java.util.stream.Stream;
    023import java.util.stream.StreamSupport;
    024
    025/**
    026 * Compare two files using a set of delegate file {@link Comparator}.
    027 * <p>
    028 * This comparator can be used to sort lists or arrays of files by combining a number of other comparators.
    029 * <p>
    030 * Example of sorting a list of files by type (i.e. directory or file) and then by name:
    031 *
    032 * <pre>
    033 *       CompositeFileComparator comparator = new CompositeFileComparator(
    034 *           DirectoryFileComparator.DIRECTORY_COMPARATOR,
    035 *           NameFileComparator.NAME_COMPARATOR);
    036 *       List&lt;File&gt; list = ...
    037 *       comparator.sort(list);
    038 * </pre>
    039 * <h2>Deprecating Serialization</h2>
    040 * <p>
    041 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    042 * </p>
    043 *
    044 * @since 2.0
    045 */
    046public class CompositeFileComparator extends AbstractFileComparator implements Serializable {
    047
    048    private static final Comparator<?>[] EMPTY_COMPARATOR_ARRAY = {};
    049    private static final long serialVersionUID = -2224170307287243428L;
    050
    051    private final Comparator<File>[] delegates;
    052
    053    /**
    054     * Constructs a composite comparator for the set of delegate comparators.
    055     *
    056     * @param delegates The delegate file comparators
    057     */
    058    public CompositeFileComparator(@SuppressWarnings("unchecked") final Comparator<File>... delegates) {
    059        this.delegates = delegates == null ? emptyArray() : delegates.clone();
    060    }
    061
    062    /**
    063     * Constructs a composite comparator for the set of delegate comparators.
    064     *
    065     * @param delegates The delegate file comparators
    066     */
    067    public CompositeFileComparator(final Iterable<Comparator<File>> delegates) {
    068        this.delegates = delegates == null ? emptyArray() : StreamSupport.stream(delegates.spliterator(), false).toArray(Comparator[]::new);
    069    }
    070
    071    /**
    072     * Compares the two files using delegate comparators.
    073     *
    074     * @param file1 The first file to compare
    075     * @param file2 The second file to compare
    076     * @return the first non-zero result returned from the delegate comparators or zero.
    077     */
    078    @Override
    079    public int compare(final File file1, final File file2) {
    080        return Stream.of(delegates).map(delegate -> delegate.compare(file1, file2)).filter(r -> r != 0).findFirst().orElse(0);
    081    }
    082
    083    @SuppressWarnings("unchecked") // types are already correct
    084    private Comparator<File>[] emptyArray() {
    085        return (Comparator<File>[]) EMPTY_COMPARATOR_ARRAY;
    086    }
    087
    088    /**
    089     * String representation of this file comparator.
    090     *
    091     * @return String representation of this file comparator
    092     */
    093    @Override
    094    public String toString() {
    095        final StringBuilder builder = new StringBuilder();
    096        builder.append(super.toString());
    097        builder.append('{');
    098        for (int i = 0; i < delegates.length; i++) {
    099            if (i > 0) {
    100                builder.append(',');
    101            }
    102            builder.append(delegates[i]);
    103        }
    104        builder.append('}');
    105        return builder.toString();
    106    }
    107}