1 25 39 package org.jgrapht.experimental.equivalence; 40 41 import java.util.*; 42 43 44 71 public class EquivalenceComparatorChainBase<E, C> 72 implements EquivalenceComparatorChain<E, C> 73 { 74 75 77 private List<EquivalenceComparator<? super E, ? super C>> chain; 78 79 81 83 public EquivalenceComparatorChainBase( 84 EquivalenceComparator<E, C> firstComaparator) 85 { 86 this.chain = 87 new LinkedList<EquivalenceComparator<? super E, ? super C>>(); 88 this.chain.add(firstComaparator); 89 } 90 91 93 102 @SuppressWarnings ("unchecked") 103 public void appendComparator(EquivalenceComparator comparatorAfter) 104 { 105 if (comparatorAfter != null) { 106 this.chain.add(comparatorAfter); 107 } 108 } 109 110 118 public boolean equivalenceCompare( 119 E arg1, 120 E arg2, 121 C context1, 122 C context2) 123 { 124 for ( 125 EquivalenceComparator<? super E, ? super C> currentComparator 126 : this.chain) { 127 if ( 128 !currentComparator.equivalenceCompare( 129 arg1, 130 arg2, 131 context1, 132 context2)) { 133 return false; 134 } 135 } 136 return true; 137 } 138 139 144 public int equivalenceHashcode(E arg1, C context) 145 { 146 StringBuffer hashStringBuffer = new StringBuffer (); 147 for ( 148 ListIterator<EquivalenceComparator<? super E, ? super C>> iter = 149 this.chain.listIterator(); 150 iter.hasNext();) { 151 EquivalenceComparator<? super E, ? super C> currentComparator = 152 iter.next(); 153 int currentHashCode = 154 currentComparator.equivalenceHashcode(arg1, context); 155 hashStringBuffer.append(currentHashCode); 156 157 if (iter.hasNext()) { 159 hashStringBuffer.append('+'); 160 } 161 } 162 return hashStringBuffer.toString().hashCode(); 163 } 164 } 165 | Popular Tags |