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: */
004: package com.tc.util.diff;
005:
006: import org.apache.commons.lang.builder.EqualsBuilder;
007:
008: import com.tc.util.Assert;
009: import com.tc.util.StandardStringifier;
010: import com.tc.util.Stringifier;
011:
012: import java.util.Collection;
013: import java.util.Iterator;
014: import java.util.LinkedList;
015: import java.util.List;
016:
017: /**
018: * Passed along among {@link Differenceable}objects in order to display where the differences are in an object tree.
019: */
020: public class DifferenceContext {
021:
022: private final DifferenceContext previous;
023: private final String this Context;
024: private final List differences;
025: private final Stringifier stringifier;
026:
027: private DifferenceContext(DifferenceContext previous,
028: String this Context) {
029: Assert.assertNotNull(previous);
030: Assert.assertNotBlank(this Context);
031:
032: this .previous = previous;
033: this .this Context = this Context;
034: this .differences = this .previous.differences;
035: this .stringifier = this .previous.stringifier;
036: }
037:
038: public DifferenceContext(Stringifier stringifier) {
039: Assert.assertNotNull(stringifier);
040:
041: this .previous = null;
042: this .this Context = "";
043: this .differences = new LinkedList();
044: this .stringifier = stringifier;
045: }
046:
047: public static DifferenceContext createInitial() {
048: return createInitial(StandardStringifier.INSTANCE);
049: }
050:
051: public static DifferenceContext createInitial(
052: Stringifier stringifier) {
053: return new DifferenceContext(stringifier);
054: }
055:
056: public DifferenceContext sub(String context) {
057: return new DifferenceContext(this , context);
058: }
059:
060: /**
061: * For <strong>TESTS ONLY </strong>.
062: */
063: Collection collection() {
064: return this .differences;
065: }
066:
067: Stringifier stringifier() {
068: return this .stringifier;
069: }
070:
071: String describe(Object o) {
072: return this .stringifier.toString(o);
073: }
074:
075: void addDifference(Difference difference) {
076: Assert.assertNotNull(difference);
077: Assert.eval(difference.where() == this );
078: this .differences.add(difference);
079: }
080:
081: Iterator getDifferences() {
082: return this .differences.iterator();
083: }
084:
085: boolean hasDifferences() {
086: return this .differences.size() > 0;
087: }
088:
089: /**
090: * For <strong>TESTS ONLY </strong>.
091: */
092: int countDifferences() {
093: return this .differences.size();
094: }
095:
096: public String toString() {
097: if (this .previous != null)
098: return this .previous.toString() + "/" + this .this Context;
099: else
100: return this .this Context;
101: }
102:
103: public boolean equals(Object that) {
104: if (!this .rawEquals(that))
105: return false;
106:
107: return new EqualsBuilder().append(this .differences,
108: ((DifferenceContext) that).differences).isEquals();
109: }
110:
111: boolean rawEquals(Object that) {
112: if (!(that instanceof DifferenceContext))
113: return false;
114:
115: DifferenceContext diffThat = (DifferenceContext) that;
116:
117: if ((this .previous == null) != (diffThat.previous == null))
118: return false;
119: if (this .previous != null
120: && (!this .previous.rawEquals(diffThat.previous)))
121: return false;
122:
123: return new EqualsBuilder().append(this.thisContext,
124: diffThat.thisContext).isEquals();
125: }
126:
127: }
|