1 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 ; 13 import java.util.Iterator ; 14 import java.util.LinkedList ; 15 import java.util.List ; 16 17 20 public class DifferenceContext { 21 22 private final DifferenceContext previous; 23 private final String thisContext; 24 private final List differences; 25 private final Stringifier stringifier; 26 27 private DifferenceContext(DifferenceContext previous, String 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 (); 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 context) { 55 return new DifferenceContext(this, context); 56 } 57 58 61 Collection collection() { 62 return this.differences; 63 } 64 65 Stringifier stringifier() { 66 return this.stringifier; 67 } 68 69 String describe(Object 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 getDifferences() { 80 return this.differences.iterator(); 81 } 82 83 boolean hasDifferences() { 84 return this.differences.size() > 0; 85 } 86 87 90 int countDifferences() { 91 return this.differences.size(); 92 } 93 94 public String toString() { 95 if (this.previous != null) return this.previous.toString() + "/" + this.thisContext; 96 else return this.thisContext; 97 } 98 99 public boolean equals(Object 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 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 |