]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/util/ArrayComparator.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / util / ArrayComparator.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2009, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.util;\r
8 import java.util.Comparator;\r
9 \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
14         } \r
15     };\r
16     private Comparator[] comparators;\r
17     private int[] reordering;\r
18     \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
26             }\r
27         } else {\r
28             if (this.reordering.length != this.comparators.length) {\r
29                 throw new IllegalArgumentException("comparator and reordering lengths must match");\r
30             }\r
31         }\r
32     }\r
33     \r
34     public ArrayComparator (Comparator... comparators) {\r
35         this(comparators,null);\r
36     }\r
37     \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
42      */\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
53             return -(i+1);\r
54         }\r
55         return 0;\r
56     }\r
57 \r
58     static class CatchExceptionComparator implements Comparator {\r
59         private Comparator other;\r
60         \r
61         public CatchExceptionComparator(Comparator other) {\r
62             this.other = other;\r
63         }\r
64 \r
65         public int compare(Object arg0, Object arg1) throws RuntimeException {\r
66             try {\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
71                 throw e;\r
72             }\r
73         }\r
74     }\r
75     \r
76 }