2 *******************************************************************************
\r
3 * Copyright (C) 2002-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\r
8 import java.util.Comparator;
\r
10 public class ArrayComparator implements Comparator {
\r
11 public static final Comparator COMPARABLE = new Comparator() {
\r
12 public int compare(Object o1, Object o2) {
\r
13 return ((Comparable)o1).compareTo(o2);
\r
16 private Comparator[] comparators;
\r
17 private int[] reordering;
\r
19 public ArrayComparator (Comparator[] comparators, int[] reordering) {
\r
20 this.comparators = comparators;
\r
21 this.reordering = reordering;
\r
22 if (this.reordering == null) {
\r
23 this.reordering = new int[comparators.length];
\r
24 for (int i = 0; i < this.reordering.length; ++i) {
\r
25 this.reordering[i] = i;
\r
28 if (this.reordering.length != this.comparators.length) {
\r
29 throw new IllegalArgumentException("comparator and reordering lengths must match");
\r
34 public ArrayComparator (Comparator... comparators) {
\r
35 this(comparators,null);
\r
38 /* Lexigraphic compare. Returns the first difference
\r
39 * @return zero if equal. Otherwise +/- (i+1)
\r
40 * where i is the index of the first comparator finding a difference
\r
41 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
\r
43 public int compare(Object a0, Object a1) {
\r
44 Object[] arg0 = (Object[]) a0;
\r
45 Object[] arg1 = (Object[]) a1;
\r
46 for (int j = 0; j < comparators.length; ++j) {
\r
47 int i = reordering[j];
\r
48 Comparator comp = comparators[i];
\r
49 if (comp == null) continue;
\r
50 int result = comp.compare(arg0[i], arg1[i]);
\r
51 if (result == 0) continue;
\r
52 if (result > 0) return i+1;
\r
58 static class CatchExceptionComparator implements Comparator {
\r
59 private Comparator other;
\r
61 public CatchExceptionComparator(Comparator other) {
\r
65 public int compare(Object arg0, Object arg1) throws RuntimeException {
\r
67 return other.compare(arg0, arg1);
\r
68 } catch (RuntimeException e) {
\r
69 System.out.println("Arg0: " + arg0);
\r
70 System.out.println("Arg1: " + arg1);
\r