1 4 package com.tc.test.collections; 5 6 import org.apache.commons.lang.builder.EqualsBuilder; 7 8 import com.tc.util.Assert; 9 import com.tc.util.Stringifier; 10 11 14 public abstract class CollectionMismatch { 15 private final Object originating; 16 private final Object comparedAgainst; 17 private final boolean originatingIsInCollectionOne; 18 19 private final int originatingIndex; 20 private final int comparedAgainstIndex; 21 22 private final Stringifier describer; 23 24 protected CollectionMismatch(Object originating, Object comparedAgainst, boolean originatingIsInCollectionOne, 25 int originatingIndex, int comparedAgainstIndex, Stringifier describer) { 26 Assert.eval(originatingIndex >= 0); 27 Assert.assertNotNull(describer); 28 29 this.originating = originating; 30 this.comparedAgainst = comparedAgainst; 31 this.originatingIsInCollectionOne = originatingIsInCollectionOne; 32 33 this.originatingIndex = originatingIndex; 34 this.comparedAgainstIndex = comparedAgainstIndex; 35 36 this.describer = describer; 37 } 38 39 protected final Object originating() { 40 return this.originating; 41 } 42 43 protected final Object comparedAgainst() { 44 return this.comparedAgainst; 45 } 46 47 protected final Stringifier describer() { 48 return this.describer; 49 } 50 51 public boolean originatingIsInCollectionOne() { 52 return this.originatingIsInCollectionOne; 53 } 54 55 protected final String originatingCollection() { 56 return "collection " + (this.originatingIsInCollectionOne ? "one" : "two"); 57 } 58 59 protected final String originatingString() { 60 return "(" + originatingCollection() + ", index " + this.originatingIndex + "): " + describeOriginating(); 61 } 62 63 protected final String describeOriginating() { 64 return this.describer.toString(this.originating); 65 } 66 67 protected final String comparedAgainstCollection() { 68 return "collection " + (this.originatingIsInCollectionOne ? "two" : "one"); 69 } 70 71 protected final String comparedAgainstString() { 72 return "(" + comparedAgainstCollection() + ", index " + this.comparedAgainstIndex + "): " 73 + describeComparedAgainst(); 74 } 75 76 protected final String describeComparedAgainst() { 77 return this.describer.toString(this.comparedAgainst); 78 } 79 80 public abstract String toString(); 81 82 public boolean equals(Object that) { 83 if (!(that instanceof CollectionMismatch)) return false; 84 85 CollectionMismatch misThat = (CollectionMismatch) that; 86 87 return new EqualsBuilder().append(this.originating, misThat.originating).append(this.comparedAgainst, 88 misThat.comparedAgainst) 89 .append(this.originatingIsInCollectionOne, misThat.originatingIsInCollectionOne) 90 .append(this.originatingIndex, misThat.originatingIndex).append(this.comparedAgainstIndex, 91 misThat.comparedAgainstIndex).isEquals(); 92 } 93 } | Popular Tags |