1 16 package org.apache.commons.collections.comparators; 17 18 import java.io.Serializable ; 19 import java.util.Comparator ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 33 public class TestComparatorChain extends AbstractTestComparator { 34 35 public TestComparatorChain(String testName) { 36 super(testName); 37 } 38 39 public static Test suite() { 40 return new TestSuite(TestComparatorChain.class); 41 } 42 43 public Comparator makeComparator() { 44 ComparatorChain chain = new ComparatorChain(new ColumnComparator(0)); 45 chain.addComparator(new ColumnComparator(1),true); chain.addComparator(new ColumnComparator(2),false); 47 return chain; 48 } 49 50 public void testNoopComparatorChain() { 51 ComparatorChain chain = new ComparatorChain(); 52 Integer i1 = new Integer (4); 53 Integer i2 = new Integer (6); 54 chain.addComparator(new ComparableComparator()); 55 56 int correctValue = i1.compareTo(i2); 57 assertTrue("Comparison returns the right order",chain.compare(i1,i2) == correctValue); 58 } 59 60 public void testBadNoopComparatorChain() { 61 ComparatorChain chain = new ComparatorChain(); 62 Integer i1 = new Integer (4); 63 Integer i2 = new Integer (6); 64 try { 65 chain.compare(i1,i2); 66 fail("An exception should be thrown when a chain contains zero comparators."); 67 } catch (UnsupportedOperationException e) { 68 69 } 70 } 71 72 public void testListComparatorChain() { 73 List list = new LinkedList (); 74 list.add(new ComparableComparator()); 75 ComparatorChain chain = new ComparatorChain(list); 76 Integer i1 = new Integer (4); 77 Integer i2 = new Integer (6); 78 79 int correctValue = i1.compareTo(i2); 80 assertTrue("Comparison returns the right order",chain.compare(i1,i2) == correctValue); 81 } 82 83 public void testBadListComparatorChain() { 84 List list = new LinkedList (); 85 ComparatorChain chain = new ComparatorChain(list); 86 Integer i1 = new Integer (4); 87 Integer i2 = new Integer (6); 88 try { 89 chain.compare(i1,i2); 90 fail("An exception should be thrown when a chain contains zero comparators."); 91 } catch (UnsupportedOperationException e) { 92 93 } 94 } 95 96 97 public void testComparatorChainOnMinvaluedCompatator() { 98 ComparatorChain chain = new ComparatorChain(); 101 chain.addComparator( 102 new Comparator () { 103 public int compare(Object a, Object b) { 104 int result = ((Comparable )a).compareTo(b); 105 if(result < 0) { 106 return Integer.MIN_VALUE; 107 } else if(result > 0) { 108 return Integer.MAX_VALUE; 109 } else { 110 return 0; 111 } 112 } 113 }, true); 114 115 assertTrue(chain.compare(new Integer (4), new Integer (5)) > 0); 116 assertTrue(chain.compare(new Integer (5), new Integer (4)) < 0); 117 assertTrue(chain.compare(new Integer (4), new Integer (4)) == 0); 118 } 119 120 public List getComparableObjectsOrdered() { 121 List list = new LinkedList (); 122 list.add(new PseudoRow(1,2,3)); 125 list.add(new PseudoRow(2,3,5)); 126 list.add(new PseudoRow(2,2,4)); 127 list.add(new PseudoRow(2,2,8)); 128 list.add(new PseudoRow(3,1,0)); 129 list.add(new PseudoRow(4,4,4)); 130 list.add(new PseudoRow(4,4,7)); 131 return list; 132 } 133 134 public static class PseudoRow implements Serializable { 135 136 public int cols[] = new int[3]; 137 138 public PseudoRow(int col1, int col2, int col3) { 139 cols[0] = col1; 140 cols[1] = col2; 141 cols[2] = col3; 142 } 143 144 public int getColumn(int colIndex) { 145 return cols[colIndex]; 146 } 147 148 public String toString() { 149 StringBuffer buf = new StringBuffer (); 150 buf.append("["); 151 buf.append(cols[0]); 152 buf.append(","); 153 buf.append(cols[1]); 154 buf.append(","); 155 buf.append(cols[2]); 156 buf.append("]"); 157 return buf.toString(); 158 } 159 160 public boolean equals(Object o) { 161 if (!(o instanceof PseudoRow)) { 162 return false; 163 } 164 165 PseudoRow row = (PseudoRow) o; 166 if (getColumn(0) != row.getColumn(0)) { 167 return false; 168 } 169 170 if (getColumn(1) != row.getColumn(1)) { 171 return false; 172 } 173 174 if (getColumn(2) != row.getColumn(2)) { 175 return false; 176 } 177 178 return true; 179 } 180 181 } 182 183 public static class ColumnComparator implements Comparator ,Serializable { 184 185 protected int colIndex = 0; 186 187 public ColumnComparator(int colIndex) { 188 this.colIndex = colIndex; 189 } 190 191 public int compare(Object o1, Object o2) { 192 193 int col1 = ( (PseudoRow) o1).getColumn(colIndex); 194 int col2 = ( (PseudoRow) o2).getColumn(colIndex); 195 196 if (col1 > col2) { 197 return 1; 198 } else if (col1 < col2) { 199 return -1; 200 } 201 202 return 0; 203 } 204 205 public int hashCode() { 206 return colIndex; 207 } 208 209 public boolean equals(Object that) { 210 if(that instanceof ColumnComparator) { 211 return colIndex == ((ColumnComparator)that).colIndex; 212 } else { 213 return false; 214 } 215 } 216 217 private static final long serialVersionUID = -2284880866328872105L; 218 } 219 } 220 | Popular Tags |