1 /* 2 ******************************************************************************* 3 * Copyright (C) 2009-2015, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ******************************************************************************* 6 */ 7 package org.unicode.cldr.util; 8 9 import java.util.Comparator; 10 11 public class MultiComparator<T> implements Comparator<T> { 12 private Comparator<T>[] comparators; 13 14 @SuppressWarnings("unchecked") // See ticket #11395, this is safe. 15 public MultiComparator(Comparator<T>... comparators) { 16 this.comparators = comparators; 17 } 18 19 /* Lexigraphic compare. Returns the first difference 20 * @return zero if equal. Otherwise +/- (i+1) 21 * where i is the index of the first comparator finding a difference 22 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 23 */ 24 public int compare(T arg0, T arg1) { 25 for (int i = 0; i < comparators.length; ++i) { 26 int result = comparators[i].compare(arg0, arg1); 27 if (result == 0) { 28 continue; 29 } 30 if (result > 0) { 31 return i + 1; 32 } 33 return -(i + 1); 34 } 35 return 0; 36 } 37 } 38