1 33 package net.sf.jga.algorithms; 34 35 import java.util.Collection ; 36 import java.util.Iterator ; 37 import net.sf.jga.fn.BinaryFunctor; 38 import net.sf.jga.fn.UnaryFunctor; 39 import net.sf.jga.fn.adaptor.ConditionalUnary; 40 import net.sf.jga.fn.adaptor.ConstantUnary; 41 import net.sf.jga.fn.adaptor.Identity; 42 import net.sf.jga.fn.comparison.EqualTo; 43 import net.sf.jga.util.LookAheadIterator; 44 45 import static net.sf.jga.util.ArrayUtils.*; 46 import static net.sf.jga.util.CollectionUtils.*; 47 48 56 public class Transform { 57 58 static private <T> UnaryFunctor<T,T> 59 conditional(UnaryFunctor<T,Boolean > test, UnaryFunctor<T,T> xform) 60 { 61 return new ConditionalUnary<T,T>(test, xform, new Identity<T>()); 62 } 63 64 68 72 static public <T> Iterable <T> replace(T[] ts, T value, T replacement) { 73 return transform(ts, conditional(new EqualTo<T>().bind2nd(value), 74 new ConstantUnary<T,T>(replacement))); 75 } 76 77 78 82 static public <T> Iterable <T> replace(T[] ts, UnaryFunctor<T,Boolean > test, T replacement) { 83 return transform(ts, conditional(test, new ConstantUnary<T,T>(replacement))); 84 } 85 86 87 92 static public <T> Iterable <T> 93 replace(T[] ts, UnaryFunctor<T,Boolean > test, UnaryFunctor<T,T> fn) 94 { 95 return transform(ts, conditional(test, fn)); 96 } 97 98 99 102 static public <T,R> Iterable <R> transform(T[] ts, UnaryFunctor<T,R> fn) { 103 return new TransformIterable<T,R>(iterable(ts), fn); 104 } 105 106 107 110 static public <T,R> Iterable <R> transform(T[] ts, BinaryFunctor<T,T,R> fn) { 111 return new AdjacentIterable<T,R>(iterable(ts), fn); 112 } 113 114 115 119 static public <T1,T2,R> Iterable <R> transform(T1[] ts1, T2[] ts2, BinaryFunctor<T1,T2,R> fn) { 120 return new BinaryIterable<T1,T2,R>(iterable(ts1), iterable(ts2), fn); 121 } 122 123 127 131 static public <T> Iterable <T> replace(Iterable <T> ts, T value, T replacement) { 132 return transform(ts, conditional(new EqualTo<T>().bind2nd(value), 133 new ConstantUnary<T,T>(replacement))); 134 } 135 136 137 141 static public <T> Iterable <T> 142 replace(Iterable <T> ts, UnaryFunctor<T,Boolean > test, T replacement) 143 { 144 return transform(ts, conditional(test, new ConstantUnary<T,T>(replacement))); 145 } 146 147 148 153 static public <T> Iterable <T> 154 replace(Iterable <T> ts, UnaryFunctor<T,Boolean > test, UnaryFunctor<T,T> fn) 155 { 156 return transform(ts, conditional(test, fn)); 157 } 158 159 160 163 static public <T,R> Iterable <R> transform(Iterable <? extends T> i, UnaryFunctor<T,R> fn) { 164 return new TransformIterable<T,R>(i, fn); 165 } 166 167 168 172 static public <T,R> Iterable <R> transform(Iterable <? extends T> i, BinaryFunctor<T,T,R> fn) { 173 return new AdjacentIterable<T,R>(i, fn); 174 } 175 176 177 181 static public <T1,T2,R> Iterable <R> 182 transform(Iterable <? extends T1> i1, Iterable <? extends T2> i2, BinaryFunctor<T1,T2,R> fn) 183 { 184 return new BinaryIterable<T1,T2,R>(i1, i2, fn); 185 } 186 187 191 195 static public <T> Iterator <T> replace(Iterator <? extends T> ts, T value, T replacement) { 196 return transform(ts, conditional(new EqualTo<T>().bind2nd(value), 197 new ConstantUnary<T,T>(replacement))); 198 } 199 200 201 205 static public <T> Iterator <T> 206 replace(Iterator <? extends T> ts, UnaryFunctor<T,Boolean > test, T replacement) 207 { 208 return transform(ts, conditional(test, new ConstantUnary<T,T>(replacement))); 209 } 210 211 212 217 static public <T> Iterator <T> 218 replace(Iterator <? extends T> ts, UnaryFunctor<T,Boolean > test, UnaryFunctor<T,T> fn) 219 { 220 return transform(ts, conditional(test, fn)); 221 } 222 223 226 static public <T,R> Iterator <R> transform(Iterator <? extends T> iter, UnaryFunctor<T,R> fn) { 227 return new net.sf.jga.util.TransformIterator<T,R>(iter, fn); 228 } 229 230 231 234 static public <T,R> Iterator <R> transform(Iterator <? extends T> iter, BinaryFunctor<T,T,R> fn) { 235 return new net.sf.jga.util.TransformAdjacentIterator<T,R>(iter, fn); 236 } 237 238 239 243 static public <T1,T2,R> Iterator <R> 244 transform(Iterator <? extends T1> iter1, Iterator <? extends T2> iter2, BinaryFunctor<T1,T2,R> fn) 245 { 246 return new net.sf.jga.util.TransformBinaryIterator<T1,T2,R>(iter1, iter2, fn); 247 } 248 249 253 257 static public <T, TCollection extends Collection <? super T>> TCollection 258 replace(Iterable <? extends T> cin, T value, T repl, TCollection cout) 259 { 260 return append(cout, replace(cin.iterator(), value, repl)); 261 } 262 263 264 268 static public <T, TCollection extends Collection <? super T>> TCollection 269 replace(Iterable <? extends T> cin, UnaryFunctor<T,Boolean > test, T repl, TCollection cout) 270 { 271 return append(cout, replace(cin.iterator(), test, repl)); 272 } 273 274 275 280 static public <T, TCollection extends Collection <? super T>> TCollection 281 replace(Iterable <? extends T> cin, UnaryFunctor<T,Boolean > test, UnaryFunctor<T,T> fn, TCollection cout) 282 { 283 return append(cout, replace(cin.iterator(), test, fn)); 284 } 285 286 287 290 static public <T, RCollection extends Collection <? super R>, R> RCollection 291 transform(Iterable <? extends T> cin, UnaryFunctor<T,R> fn, RCollection cout) { 292 return append(cout, transform(cin.iterator(), fn)); 293 } 294 295 296 299 static public <T, RCollection extends Collection <? super R>, R> RCollection 300 transform(Iterable <? extends T> cin, BinaryFunctor<T,T,R> fn, RCollection cout) { 301 return append(cout, transform(cin.iterator(), fn)); 302 } 303 304 305 309 static public <T1, T2, RCollection extends Collection <? super R>, R> RCollection 310 transform(Iterable <? extends T1> cin1, Iterable <? extends T2> cin2, BinaryFunctor<T1,T2,R> fn, RCollection cout) 311 { 312 return append(cout, transform(cin1.iterator(), cin2.iterator(), fn)); 313 } 314 315 316 322 323 static public class TransformIterable<T,R> implements Iterable <R> { 324 private Iterable <? extends T> _delegate; 326 327 private UnaryFunctor<T,R> _fn; 329 330 334 public TransformIterable(Iterable <? extends T> base, UnaryFunctor<T,R> process) { 335 _delegate = base; 336 _fn = process; 337 } 338 339 340 344 public Iterator <R> iterator() { 345 return new net.sf.jga.util.TransformIterator(_delegate.iterator(), _fn); 346 } 347 } 348 349 350 354 static public class TransformIterator<T,R> implements Iterator <R> { 355 356 private Iterator <? extends T> _base; 358 359 private UnaryFunctor<T,R> _fn; 361 362 366 public TransformIterator(Iterator <? extends T> iter, UnaryFunctor<T,R> fn) { 367 _base = iter; 368 _fn = fn; 369 } 370 371 375 public boolean hasNext(){ 376 return _base.hasNext(); 377 } 378 379 public R next() { 380 return _fn.fn(_base.next()); 381 } 382 383 public void remove() { throw new UnsupportedOperationException (); } 384 } 385 386 387 391 392 static public class AdjacentIterable<T,R> implements Iterable <R> { 393 private Iterable <? extends T> _delegate; 395 396 private BinaryFunctor<T,T,R> _fn; 398 399 403 public AdjacentIterable(Iterable <? extends T> base, BinaryFunctor<T,T,R> process) { 404 _delegate = base; 405 _fn = process; 406 } 407 408 412 public Iterator <R> iterator() { 413 return new net.sf.jga.util.TransformAdjacentIterator(_delegate.iterator(), _fn); 414 } 415 } 416 417 418 422 static public class AdjacentIterator<T,R> implements Iterator <R> { 423 424 private LookAheadIterator<? extends T> _base; 426 427 private BinaryFunctor<T,T,R> _fn; 429 430 434 public AdjacentIterator(Iterator <? extends T> iter, BinaryFunctor<T,T,R> fn) { 435 _base = new LookAheadIterator<T>(iter, 2); 436 _fn = fn; 437 } 438 439 443 public boolean hasNext(){ 444 return _base.hasNextPlus(2); 445 } 446 447 public R next() { 448 T next = _base.next(); 449 return _fn.fn(next,_base.peek(1)); 450 } 451 452 public void remove() { throw new UnsupportedOperationException (); } 453 } 454 455 456 460 461 static public class BinaryIterable<T1,T2,R> implements Iterable <R> { 462 private Iterable <? extends T1> _delegate1; 464 private Iterable <? extends T2> _delegate2; 465 466 private BinaryFunctor<T1,T2,R> _fn; 468 469 473 public BinaryIterable(Iterable <? extends T1> base1, Iterable <? extends T2> base2, 474 BinaryFunctor<T1,T2,R> process) 475 { 476 _delegate1 = base1; 477 _delegate2 = base2; 478 _fn = process; 479 } 480 481 485 public Iterator <R> iterator() { 486 return new net.sf.jga.util.TransformBinaryIterator(_delegate1.iterator(), 487 _delegate2.iterator(), _fn); 488 } 489 } 490 491 492 496 static public class BinaryIterator<T1,T2,R> implements Iterator <R> { 497 private Iterator <? extends T1> _i1; 499 private Iterator <? extends T2> _i2; 500 501 private BinaryFunctor<T1,T2,R> _bf; 503 504 508 public BinaryIterator(Iterator <? extends T1> i1, Iterator <? extends T2> i2, BinaryFunctor<T1,T2,R> bf) { 509 _i1 = i1; 510 _i2 = i2; 511 _bf = bf; 512 } 513 514 518 public boolean hasNext() { 519 return _i1.hasNext() && _i2.hasNext(); 520 } 521 522 public R next() { 523 return _bf.fn(_i1.next(), _i2.next()); 524 } 525 526 public void remove() { throw new UnsupportedOperationException (); } 527 } 528 } 529 | Popular Tags |