1 34 package net.sf.jga.algorithms; 35 36 import java.util.Collection ; 37 import java.util.Comparator ; 38 import java.util.Iterator ; 39 import java.util.NoSuchElementException ; 40 import net.sf.jga.fn.BinaryFunctor; 41 import net.sf.jga.fn.comparison.EqualTo; 42 import net.sf.jga.util.ArrayUtils; 43 44 import static net.sf.jga.util.ArrayUtils.*; 45 import static net.sf.jga.util.CollectionUtils.*; 46 47 53 54 public class Unique { 55 56 60 64 static public <T> Iterable <T> unique(T[] ts) { 65 return new UniqueIterable<T>(iterable(ts)); 66 } 67 68 69 73 static public <T> Iterable <T> unique(T[] ts, Comparator <? super T> comp) { 74 return new UniqueIterable<T>(iterable(ts), comp); 75 } 76 77 78 82 static public <T> Iterable <T> unique(T[] ts, BinaryFunctor<T,T,Boolean > eq) { 83 return new UniqueIterable<T>(iterable(ts), eq); 84 } 85 86 90 94 static public <T> Iterable <T> unique(Iterable <? extends T> i) { 95 return new UniqueIterable<T>(i); 96 } 97 98 99 103 static public <T> Iterable <T> unique(Iterable <? extends T> i, Comparator <? super T> comp) { 104 return new UniqueIterable<T>(i, comp); 105 } 106 107 108 112 static public <T> Iterable <T> unique(Iterable <? extends T> i, BinaryFunctor<T,T,Boolean > eq) { 113 return new UniqueIterable<T>(i, eq); 114 } 115 116 120 124 125 static public <T> Iterator <T> unique(Iterator <? extends T> iterator) { 126 return new net.sf.jga.util.UniqueIterator<T>(iterator); 127 } 128 129 130 134 135 static public <T> Iterator <T> 136 unique(Iterator <? extends T> iterator, Comparator <? super T> comp) { 137 return new net.sf.jga.util.UniqueIterator<T>(iterator, comp); 138 } 139 140 141 145 146 static public <T> Iterator <T> 147 unique(Iterator <? extends T> iterator, BinaryFunctor<T,T,Boolean > eq) { 148 return new net.sf.jga.util.UniqueIterator<T>(iterator, eq); 149 } 150 151 155 159 160 static public <T, TCollection extends Collection <? super T>> TCollection 161 unique(Iterable <? extends T> lin, TCollection cout) { 162 return append(cout, unique(lin.iterator())); 163 } 164 165 166 170 171 static public <T, TCollection extends Collection <? super T>> TCollection 172 unique(Iterable <? extends T> lin, Comparator <? super T> comp, TCollection cout) { 173 return append(cout, unique(lin.iterator(), comp)); 174 } 175 176 177 181 182 static public <T, TCollection extends Collection <? super T>> TCollection 183 unique(Iterable <? extends T> lin, BinaryFunctor<T,T,Boolean > eq, TCollection cout) { 184 return append(cout, unique(lin.iterator(), eq)); 185 } 186 187 188 193 194 static public class UniqueIterable<T> implements Iterable <T> { 195 196 private Iterable <? extends T> _delegate; 198 199 private BinaryFunctor<T,T,Boolean > _pred; 201 202 207 public UniqueIterable(Iterable <? extends T> base) { 208 this(base, new EqualTo<T>()); 209 } 210 211 215 public UniqueIterable(Iterable <? extends T> base, Comparator <? super T> comp) { 216 this(base, new EqualTo<T>(comp)); 217 } 218 219 220 226 227 public UniqueIterable(Iterable <? extends T> base, BinaryFunctor<T,T,Boolean > pred) { 228 if (base == null) 229 throw new IllegalArgumentException ( "a base iterable is required"); 230 231 _delegate = base; 232 _pred = pred; 233 } 234 235 239 public Iterator <T> iterator() { 240 return new net.sf.jga.util.UniqueIterator<T>(_delegate.iterator(), _pred); } 241 242 } 243 244 245 248 249 static public class UniqueIterator<T> implements Iterator <T> { 250 private Iterator <? extends T> _base; 252 253 private BinaryFunctor<T,T,Boolean > _eq; 255 256 private Boolean _testedNext; 258 259 private boolean _hasNext; 261 262 private T _lastValue; 264 265 private T _nextValue; 267 268 272 273 public UniqueIterator(Iterator <? extends T> base) { 274 this(base, new EqualTo<T>()); 275 } 276 277 280 281 public UniqueIterator(Iterator <? extends T> base, Comparator <? super T> comp) { 282 this(base, new EqualTo<T>(comp)); 283 } 284 285 291 292 public UniqueIterator(Iterator <? extends T> base, BinaryFunctor<T,T,Boolean > eq) { 293 if (base == null) 294 throw new IllegalArgumentException ("base iterator is required"); 295 296 if (eq == null) 297 throw new IllegalArgumentException ("functor is required"); 298 299 _base = base; 300 _eq = eq; 301 } 302 303 307 public boolean hasNext() { 308 if (_testedNext != null) { 310 311 if (!_testedNext.booleanValue()) { 314 _testedNext = Boolean.TRUE; 316 _hasNext = false; while (_base.hasNext()) { 318 _nextValue = _base.next(); 319 if (_eq.fn(_lastValue, _nextValue).booleanValue()) { continue; } 322 323 _hasNext = true; 324 break; 325 } 326 } 327 } 328 else { 329 333 _testedNext = Boolean.TRUE; 334 _hasNext = _base.hasNext(); 335 if (_hasNext) { 336 _nextValue = _base.next(); 337 } 338 } 339 340 return _hasNext; 341 342 } 343 344 public T next() { 345 if (_testedNext == null || !_testedNext.booleanValue()) { hasNext(); 348 } 349 350 if (!_hasNext) 352 throw new NoSuchElementException (); 353 354 _testedNext = Boolean.FALSE; 356 _lastValue = _nextValue; 357 return _nextValue; 358 } 359 360 public void remove() { throw new UnsupportedOperationException (); } 361 } 362 } 363 | Popular Tags |