1 25 38 package org.jgrapht.experimental.equivalence; 39 40 import java.util.*; 41 42 43 60 public class EquivalenceSet<E, C> 61 { 62 63 65 68 protected EquivalenceComparator<? super E, ? super C> eqComparator; 69 protected C comparatorContext; 70 71 74 protected Set<E> elementsSet; 75 76 78 83 private EquivalenceSet() 84 { 85 } 86 87 91 public EquivalenceSet( 92 E aElement, 93 EquivalenceComparator<? super E, ? super C> aEqComparator, 94 C aComparatorContext) 95 { 96 this.eqComparator = aEqComparator; 97 this.comparatorContext = aComparatorContext; 98 99 this.elementsSet = new HashSet<E>(); 100 this.elementsSet.add(aElement); 101 } 102 103 105 110 public E getRepresentative() 111 { 112 return elementsSet.iterator().next(); 113 } 114 115 public C getContext() 116 { 117 return this.comparatorContext; 118 } 119 120 public int size() 121 { 122 return elementsSet.size(); 123 } 124 125 129 public void add(E element) 130 { 131 this.elementsSet.add(element); 132 } 133 134 public boolean equivalentTo(E aOther, C aOtherContext) 135 { 136 boolean result = 137 this.eqComparator.equivalenceCompare( 138 this.getRepresentative(), 139 aOther, 140 this.comparatorContext, 141 aOtherContext); 142 return result; 143 } 144 145 151 162 @SuppressWarnings ("unchecked") 163 public boolean equals(Object other) 164 { 165 E otherRepresentative = null; 166 C otherContext = null; 167 if (other instanceof EquivalenceSet) { 168 otherRepresentative = 169 ((EquivalenceSet<E, C>) other).getRepresentative(); 170 otherContext = ((EquivalenceSet<E, C>) other).getContext(); 171 } else { 172 throw new ClassCastException ( 173 "can check equal() only of EqualityGroup"); 174 } 175 176 boolean result = 177 this.eqComparator.equivalenceCompare( 178 this.getRepresentative(), 179 otherRepresentative, 180 this.comparatorContext, 181 otherContext); 182 return result; 183 } 184 185 191 public int hashCode() 192 { 193 int result = 194 this.eqComparator.equivalenceHashcode( 195 this.getRepresentative(), 196 this.comparatorContext); 197 return result; 198 } 199 200 public String toString() 201 { 202 return "Eq.Group=" + this.elementsSet.toString(); 203 } 204 205 210 public Object [] toArray() 211 { 212 return this.elementsSet.toArray(); 213 } 214 } 215 | Popular Tags |