KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > collections > CollectionComparer


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test.collections;
5
6 import com.tc.util.Assert;
7 import com.tc.util.EqualityComparator;
8 import com.tc.util.Stringifier;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * An object that knows how to compare collections of objects for equality. Unlike using the simple
17  * {@link Collection.equals(Object)}method, this class (or subclasses) can compare collections under various
18  * constraints (e.g., must be the same, but without regard to order; must have the same objects, but number of
19  * repetitions of those objects is irrelevant; etc.). Also, this class reports mismatches between collections in a
20  * uniform, object-oriented way, such that they can be easily printed out, compared themselves, and so on.
21  */

22 public abstract class CollectionComparer {
23
24   private final EqualityComparator comparator;
25   private final Stringifier describer;
26
27   public CollectionComparer(EqualityComparator comparator, Stringifier describer) {
28     Assert.assertNotNull(comparator);
29     Assert.assertNotNull(describer);
30
31     this.comparator = comparator;
32     this.describer = describer;
33   }
34
35   public CollectionMismatch[] getMismatches(Object JavaDoc collectionOneObject, Object JavaDoc collectionTwoObject) {
36     Assert.assertNotNull(collectionOneObject);
37     Assert.assertNotNull(collectionTwoObject);
38
39     Object JavaDoc[] collectionOne = getCollection(collectionOneObject);
40     Object JavaDoc[] collectionTwo = getCollection(collectionTwoObject);
41
42     return doComparison(collectionOne, collectionTwo);
43   }
44
45   protected abstract CollectionMismatch[] doComparison(Object JavaDoc[] collectionOne, Object JavaDoc[] collectionTwo);
46
47   protected final Stringifier describer() {
48     return this.describer;
49   }
50
51   protected final boolean isEqual(Object JavaDoc firstObject, boolean firstIsOne, Object JavaDoc secondObject,
52                                   boolean secondIsOne, int oneIndex, int twoIndex) {
53     boolean isEqual = this.comparator.isEquals(firstObject, secondObject);
54     boolean flipEqual = this.comparator.isEquals(secondObject, firstObject);
55
56     if (isEqual != flipEqual) {
57       // formatting
58
throw new IllegalStateException JavaDoc("Your comparator is broken; it claimed that collection "
59                                       + (firstIsOne ? "one" : "two") + ", index " + oneIndex + " ("
60                                       + this.describer.toString(firstObject) + ") was " + (isEqual ? "" : "not ")
61                                       + "equal to collection " + (secondIsOne ? "one" : "two") + ", index " + twoIndex
62                                       + " (" + (this.describer.toString(secondObject)) + "), but the reverse "
63                                       + "comparison gave the opposite result.");
64     }
65
66     return isEqual;
67   }
68
69   private Object JavaDoc[] getCollection(Object JavaDoc o) {
70     Assert.assertNotNull(o);
71
72     if (o instanceof Object JavaDoc[]) return (Object JavaDoc[]) o;
73     else if (o instanceof byte[]) {
74       return getCollection((byte[]) o);
75     } else if (o instanceof char[]) {
76       return getCollection((char[]) o);
77     } else if (o instanceof short[]) {
78       return getCollection((short[]) o);
79     } else if (o instanceof int[]) {
80       return getCollection((int[]) o);
81     } else if (o instanceof long[]) {
82       return getCollection((long[]) o);
83     } else if (o instanceof boolean[]) {
84       return getCollection((boolean[]) o);
85     } else if (o instanceof float[]) {
86       return getCollection((float[]) o);
87     } else if (o instanceof double[]) {
88       return getCollection((double[]) o);
89     } else if (o instanceof Iterator JavaDoc) {
90       return getCollection((Iterator JavaDoc) o);
91     } else if (o instanceof Collection JavaDoc) {
92       return getCollection((Collection JavaDoc) o);
93     } else {
94       throw new IllegalArgumentException JavaDoc("This object, " + o + ", is of class '" + o.getClass().getName() + "'; "
95                                          + "that is not a recognized type of collection or other aggregate.");
96     }
97   }
98
99   private Object JavaDoc[] getCollection(byte[] b) {
100     Object JavaDoc[] o = new Object JavaDoc[b.length];
101     for (int i = 0; i < o.length; ++i) {
102       o[i] = new Byte JavaDoc(b[i]);
103     }
104     return o;
105   }
106
107   private Object JavaDoc[] getCollection(char[] c) {
108     Object JavaDoc[] o = new Object JavaDoc[c.length];
109     for (int i = 0; i < o.length; ++i) {
110       o[i] = new Character JavaDoc(c[i]);
111     }
112     return o;
113   }
114
115   private Object JavaDoc[] getCollection(short[] s) {
116     Object JavaDoc[] o = new Object JavaDoc[s.length];
117     for (int i = 0; i < o.length; ++i) {
118       o[i] = new Short JavaDoc(s[i]);
119     }
120     return o;
121   }
122
123   private Object JavaDoc[] getCollection(int[] iarr) {
124     Object JavaDoc[] o = new Object JavaDoc[iarr.length];
125     for (int i = 0; i < o.length; ++i) {
126       o[i] = new Integer JavaDoc(iarr[i]);
127     }
128     return o;
129   }
130
131   private Object JavaDoc[] getCollection(long[] l) {
132     Object JavaDoc[] o = new Object JavaDoc[l.length];
133     for (int i = 0; i < o.length; ++i) {
134       o[i] = new Long JavaDoc(l[i]);
135     }
136     return o;
137   }
138
139   private Object JavaDoc[] getCollection(boolean[] b) {
140     Object JavaDoc[] o = new Object JavaDoc[b.length];
141     for (int i = 0; i < o.length; ++i) {
142       o[i] = b[i] ? Boolean.TRUE : Boolean.FALSE;
143     }
144     return o;
145   }
146
147   private Object JavaDoc[] getCollection(float[] f) {
148     Object JavaDoc[] o = new Object JavaDoc[f.length];
149     for (int i = 0; i < o.length; ++i) {
150       o[i] = new Float JavaDoc(f[i]);
151     }
152     return o;
153   }
154
155   private Object JavaDoc[] getCollection(double[] d) {
156     Object JavaDoc[] o = new Object JavaDoc[d.length];
157     for (int i = 0; i < o.length; ++i) {
158       o[i] = new Double JavaDoc(d[i]);
159     }
160     return o;
161   }
162
163   private Object JavaDoc[] getCollection(Iterator JavaDoc i) {
164     List JavaDoc out = new ArrayList JavaDoc();
165     while (i.hasNext())
166       out.add(i.next());
167     return out.toArray(new Object JavaDoc[out.size()]);
168   }
169
170   private Object JavaDoc[] getCollection(Collection JavaDoc c) {
171     return c.toArray(new Object JavaDoc[c.size()]);
172   }
173
174 }
Popular Tags