| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CompositeFileComparator |
|
| 2.75;2.75 |
| 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.ArrayList; | |
| 22 | import java.util.Comparator; | |
| 23 | import java.util.List; | |
| 24 | ||
| 25 | /** | |
| 26 | * Compare two files using a set of delegate file {@link Comparator}. | |
| 27 | * <p> | |
| 28 | * This comparator can be used to sort lists or arrays of files | |
| 29 | * by combining a number other comparators. | |
| 30 | * <p> | |
| 31 | * Example of sorting a list of files by type (i.e. directory or file) | |
| 32 | * and then by name: | |
| 33 | * <pre> | |
| 34 | * CompositeFileComparator comparator = | |
| 35 | * new CompositeFileComparator( | |
| 36 | * (AbstractFileComparator) DirectoryFileComparator.DIRECTORY_COMPARATOR, | |
| 37 | * (AbstractFileComparator) NameFileComparator.NAME_COMPARATOR); | |
| 38 | * List<File> list = ... | |
| 39 | * comparator.sort(list); | |
| 40 | * </pre> | |
| 41 | * | |
| 42 | * @version $Id: CompositeFileComparator.java 1469107 2013-04-17 23:45:53Z sebb $ | |
| 43 | * @since 2.0 | |
| 44 | */ | |
| 45 | 25 | public class CompositeFileComparator extends AbstractFileComparator implements Serializable { |
| 46 | ||
| 47 | 1 | private static final Comparator<?>[] NO_COMPARATORS = {}; |
| 48 | private final Comparator<File>[] delegates; | |
| 49 | ||
| 50 | /** | |
| 51 | * Create a composite comparator for the set of delegate comparators. | |
| 52 | * | |
| 53 | * @param delegates The delegate file comparators | |
| 54 | */ | |
| 55 | @SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct | |
| 56 | 11 | public CompositeFileComparator(final Comparator<File>... delegates) { |
| 57 | 11 | if (delegates == null) { |
| 58 | 1 | this.delegates = (Comparator<File>[]) NO_COMPARATORS;//1 |
| 59 | } else { | |
| 60 | 10 | this.delegates = (Comparator<File>[]) new Comparator<?>[delegates.length];//2 |
| 61 | 10 | System.arraycopy(delegates, 0, this.delegates, 0, delegates.length); |
| 62 | } | |
| 63 | 11 | } |
| 64 | ||
| 65 | /** | |
| 66 | * Create a composite comparator for the set of delegate comparators. | |
| 67 | * | |
| 68 | * @param delegates The delegate file comparators | |
| 69 | */ | |
| 70 | @SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct | |
| 71 | 2 | public CompositeFileComparator(final Iterable<Comparator<File>> delegates) { |
| 72 | 2 | if (delegates == null) { |
| 73 | 1 | this.delegates = (Comparator<File>[]) NO_COMPARATORS; //1 |
| 74 | } else { | |
| 75 | 1 | final List<Comparator<File>> list = new ArrayList<Comparator<File>>(); |
| 76 | 1 | for (final Comparator<File> comparator : delegates) { |
| 77 | 2 | list.add(comparator); |
| 78 | 2 | } |
| 79 | 1 | this.delegates = (Comparator<File>[]) list.toArray(new Comparator<?>[list.size()]); //2 |
| 80 | } | |
| 81 | 2 | } |
| 82 | ||
| 83 | /** | |
| 84 | * Compare the two files using delegate comparators. | |
| 85 | * | |
| 86 | * @param file1 The first file to compare | |
| 87 | * @param file2 The second file to compare | |
| 88 | * @return the first non-zero result returned from | |
| 89 | * the delegate comparators or zero. | |
| 90 | */ | |
| 91 | public int compare(final File file1, final File file2) { | |
| 92 | 21 | int result = 0; |
| 93 | 29 | for (final Comparator<File> delegate : delegates) { |
| 94 | 22 | result = delegate.compare(file1, file2); |
| 95 | 22 | if (result != 0) { |
| 96 | 14 | break; |
| 97 | } | |
| 98 | } | |
| 99 | 21 | return result; |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * String representation of this file comparator. | |
| 104 | * | |
| 105 | * @return String representation of this file comparator | |
| 106 | */ | |
| 107 | @Override | |
| 108 | public String toString() { | |
| 109 | 4 | final StringBuilder builder = new StringBuilder(); |
| 110 | 4 | builder.append(super.toString()); |
| 111 | 4 | builder.append('{'); |
| 112 | 8 | for (int i = 0; i < delegates.length; i++) { |
| 113 | 4 | if (i > 0) { |
| 114 | 2 | builder.append(','); |
| 115 | } | |
| 116 | 4 | builder.append(delegates[i]); |
| 117 | } | |
| 118 | 4 | builder.append('}'); |
| 119 | 4 | return builder.toString(); |
| 120 | } | |
| 121 | } |