KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > diff > DifferenceContext


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util.diff;
5
6 import org.apache.commons.lang.builder.EqualsBuilder;
7
8 import com.tc.util.Assert;
9 import com.tc.util.StandardStringifier;
10 import com.tc.util.Stringifier;
11
12 import java.util.Collection JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.LinkedList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Passed along among {@link Differenceable}objects in order to display where the differences are in an object tree.
19  */

20 public class DifferenceContext {
21
22   private final DifferenceContext previous;
23   private final String JavaDoc thisContext;
24   private final List JavaDoc differences;
25   private final Stringifier stringifier;
26
27   private DifferenceContext(DifferenceContext previous, String JavaDoc thisContext) {
28     Assert.assertNotNull(previous);
29     Assert.assertNotBlank(thisContext);
30
31     this.previous = previous;
32     this.thisContext = thisContext;
33     this.differences = this.previous.differences;
34     this.stringifier = this.previous.stringifier;
35   }
36
37   public DifferenceContext(Stringifier stringifier) {
38     Assert.assertNotNull(stringifier);
39     
40     this.previous = null;
41     this.thisContext = "";
42     this.differences = new LinkedList JavaDoc();
43     this.stringifier = stringifier;
44   }
45
46   public static DifferenceContext createInitial() {
47     return createInitial(StandardStringifier.INSTANCE);
48   }
49
50   public static DifferenceContext createInitial(Stringifier stringifier) {
51     return new DifferenceContext(stringifier);
52   }
53
54   public DifferenceContext sub(String JavaDoc context) {
55     return new DifferenceContext(this, context);
56   }
57
58   /**
59    * For <strong>TESTS ONLY </strong>.
60    */

61   Collection JavaDoc collection() {
62     return this.differences;
63   }
64
65   Stringifier stringifier() {
66     return this.stringifier;
67   }
68   
69   String JavaDoc describe(Object JavaDoc o) {
70     return this.stringifier.toString(o);
71   }
72
73   void addDifference(Difference difference) {
74     Assert.assertNotNull(difference);
75     Assert.eval(difference.where() == this);
76     this.differences.add(difference);
77   }
78
79   Iterator JavaDoc getDifferences() {
80     return this.differences.iterator();
81   }
82
83   boolean hasDifferences() {
84     return this.differences.size() > 0;
85   }
86
87   /**
88    * For <strong>TESTS ONLY </strong>.
89    */

90   int countDifferences() {
91     return this.differences.size();
92   }
93
94   public String JavaDoc toString() {
95     if (this.previous != null) return this.previous.toString() + "/" + this.thisContext;
96     else return this.thisContext;
97   }
98
99   public boolean equals(Object JavaDoc that) {
100     if (!this.rawEquals(that)) return false;
101
102     return new EqualsBuilder().append(this.differences, ((DifferenceContext) that).differences).isEquals();
103   }
104
105   boolean rawEquals(Object JavaDoc that) {
106     if (!(that instanceof DifferenceContext)) return false;
107
108     DifferenceContext diffThat = (DifferenceContext) that;
109
110     if ((this.previous == null) != (diffThat.previous == null)) return false;
111     if (this.previous != null && (!this.previous.rawEquals(diffThat.previous))) return false;
112
113     return new EqualsBuilder().append(this.thisContext, diffThat.thisContext).isEquals();
114   }
115
116 }
Popular Tags