1 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 ; 11 import java.util.Collection ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 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 collectionOneObject, Object collectionTwoObject) { 36 Assert.assertNotNull(collectionOneObject); 37 Assert.assertNotNull(collectionTwoObject); 38 39 Object [] collectionOne = getCollection(collectionOneObject); 40 Object [] collectionTwo = getCollection(collectionTwoObject); 41 42 return doComparison(collectionOne, collectionTwo); 43 } 44 45 protected abstract CollectionMismatch[] doComparison(Object [] collectionOne, Object [] collectionTwo); 46 47 protected final Stringifier describer() { 48 return this.describer; 49 } 50 51 protected final boolean isEqual(Object firstObject, boolean firstIsOne, Object 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 throw new IllegalStateException ("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 [] getCollection(Object o) { 70 Assert.assertNotNull(o); 71 72 if (o instanceof Object []) return (Object []) 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 ) { 90 return getCollection((Iterator ) o); 91 } else if (o instanceof Collection ) { 92 return getCollection((Collection ) o); 93 } else { 94 throw new IllegalArgumentException ("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 [] getCollection(byte[] b) { 100 Object [] o = new Object [b.length]; 101 for (int i = 0; i < o.length; ++i) { 102 o[i] = new Byte (b[i]); 103 } 104 return o; 105 } 106 107 private Object [] getCollection(char[] c) { 108 Object [] o = new Object [c.length]; 109 for (int i = 0; i < o.length; ++i) { 110 o[i] = new Character (c[i]); 111 } 112 return o; 113 } 114 115 private Object [] getCollection(short[] s) { 116 Object [] o = new Object [s.length]; 117 for (int i = 0; i < o.length; ++i) { 118 o[i] = new Short (s[i]); 119 } 120 return o; 121 } 122 123 private Object [] getCollection(int[] iarr) { 124 Object [] o = new Object [iarr.length]; 125 for (int i = 0; i < o.length; ++i) { 126 o[i] = new Integer (iarr[i]); 127 } 128 return o; 129 } 130 131 private Object [] getCollection(long[] l) { 132 Object [] o = new Object [l.length]; 133 for (int i = 0; i < o.length; ++i) { 134 o[i] = new Long (l[i]); 135 } 136 return o; 137 } 138 139 private Object [] getCollection(boolean[] b) { 140 Object [] o = new Object [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 [] getCollection(float[] f) { 148 Object [] o = new Object [f.length]; 149 for (int i = 0; i < o.length; ++i) { 150 o[i] = new Float (f[i]); 151 } 152 return o; 153 } 154 155 private Object [] getCollection(double[] d) { 156 Object [] o = new Object [d.length]; 157 for (int i = 0; i < o.length; ++i) { 158 o[i] = new Double (d[i]); 159 } 160 return o; 161 } 162 163 private Object [] getCollection(Iterator i) { 164 List out = new ArrayList (); 165 while (i.hasNext()) 166 out.add(i.next()); 167 return out.toArray(new Object [out.size()]); 168 } 169 170 private Object [] getCollection(Collection c) { 171 return c.toArray(new Object [c.size()]); 172 } 173 174 } | Popular Tags |