001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */package com.tc.test.collections;
004:
005: import org.apache.commons.lang.builder.EqualsBuilder;
006:
007: import com.tc.util.Assert;
008: import com.tc.util.Stringifier;
009:
010: /**
011: * Represents a single mismatch among collections.
012: */
013: public abstract class CollectionMismatch {
014: private final Object originating;
015: private final Object comparedAgainst;
016: private final boolean originatingIsInCollectionOne;
017:
018: private final int originatingIndex;
019: private final int comparedAgainstIndex;
020:
021: private final Stringifier describer;
022:
023: protected CollectionMismatch(Object originating,
024: Object comparedAgainst,
025: boolean originatingIsInCollectionOne, int originatingIndex,
026: int comparedAgainstIndex, Stringifier describer) {
027: Assert.eval(originatingIndex >= 0);
028: Assert.assertNotNull(describer);
029:
030: this .originating = originating;
031: this .comparedAgainst = comparedAgainst;
032: this .originatingIsInCollectionOne = originatingIsInCollectionOne;
033:
034: this .originatingIndex = originatingIndex;
035: this .comparedAgainstIndex = comparedAgainstIndex;
036:
037: this .describer = describer;
038: }
039:
040: protected final Object originating() {
041: return this .originating;
042: }
043:
044: protected final Object comparedAgainst() {
045: return this .comparedAgainst;
046: }
047:
048: protected final Stringifier describer() {
049: return this .describer;
050: }
051:
052: public boolean originatingIsInCollectionOne() {
053: return this .originatingIsInCollectionOne;
054: }
055:
056: protected final String originatingCollection() {
057: return "collection "
058: + (this .originatingIsInCollectionOne ? "one" : "two");
059: }
060:
061: protected final String originatingString() {
062: return "(" + originatingCollection() + ", index "
063: + this .originatingIndex + "): " + describeOriginating();
064: }
065:
066: protected final String describeOriginating() {
067: return this .describer.toString(this .originating);
068: }
069:
070: protected final String comparedAgainstCollection() {
071: return "collection "
072: + (this .originatingIsInCollectionOne ? "two" : "one");
073: }
074:
075: protected final String comparedAgainstString() {
076: return "(" + comparedAgainstCollection() + ", index "
077: + this .comparedAgainstIndex + "): "
078: + describeComparedAgainst();
079: }
080:
081: protected final String describeComparedAgainst() {
082: return this .describer.toString(this .comparedAgainst);
083: }
084:
085: public abstract String toString();
086:
087: public boolean equals(Object that) {
088: if (!(that instanceof CollectionMismatch))
089: return false;
090:
091: CollectionMismatch misThat = (CollectionMismatch) that;
092:
093: return new EqualsBuilder().append(this.originating,
094: misThat.originating).append(this.comparedAgainst,
095: misThat.comparedAgainst).append(
096: this.originatingIsInCollectionOne,
097: misThat.originatingIsInCollectionOne).append(
098: this.originatingIndex, misThat.originatingIndex)
099: .append(this.comparedAgainstIndex,
100: misThat.comparedAgainstIndex).isEquals();
101: }
102: }
|