1 package net.sf.jga.util; 33 34 import java.util.Collection ; 35 import java.util.Comparator ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.ListIterator ; 39 import net.sf.jga.algorithms.Find; 40 import net.sf.jga.algorithms.Filter; 41 import net.sf.jga.algorithms.Merge; 42 import net.sf.jga.algorithms.Summarize; 43 import net.sf.jga.algorithms.Transform; 44 import net.sf.jga.algorithms.Unique; 45 import net.sf.jga.fn.BinaryFunctor; 46 import net.sf.jga.fn.UnaryFunctor; 47 import net.sf.jga.fn.algorithm.ForEach; 48 import net.sf.jga.fn.arithmetic.Arithmetic; 49 import net.sf.jga.fn.arithmetic.ArithmeticFactory; 50 import net.sf.jga.fn.arithmetic.Plus; 51 import net.sf.jga.fn.comparison.EqualTo; 52 import net.sf.jga.fn.comparison.Equality; 53 import net.sf.jga.fn.comparison.NotEqualTo; 54 import net.sf.jga.fn.logical.BinaryNegate; 55 import net.sf.jga.fn.logical.UnaryNegate; 56 57 81 82 public class Algorithms { 83 84 88 95 static public <T> Iterator <T> find(Collection <? extends T> collection, T value) { 96 return Find.find(collection, value); 97 } 98 99 107 static public <T> Iterator <T> find(Collection <? extends T> collection, T value, Equality<T> eq){ 108 return Find.find(collection, value, eq); 109 } 110 111 112 119 static public <T> Iterator <T> 120 find(Collection <? extends T> collection, UnaryFunctor<T,Boolean > fn) 121 { 122 return Find.find(collection, fn); 123 } 124 125 129 135 static public <T> long count(Collection <? extends T> collection, T value) { 136 return Summarize.count(collection, value); 137 } 138 139 145 static public <T> long count(Collection <? extends T> collection, Equality<T> eq, T value) { 146 return Summarize.count(collection,eq,value); 147 } 148 149 150 156 static public <T> long count(Collection <? extends T> collection, UnaryFunctor<T,Boolean > eq) { 157 return Summarize.count(collection,eq); 158 } 159 160 164 173 static public <T> Iterator <T> findAdjacent(Collection <? extends T> collection) { 174 return Find.findAdjacent(collection); 175 } 176 177 178 187 static public <T> Iterator <T> 188 findAdjacent(Collection <? extends T> c, BinaryFunctor<T,T,Boolean > bf) 189 { 190 return Find.findAdjacent(c, bf); 191 } 192 193 197 204 static public <T> Iterator <T> 205 findElement(Collection <? extends T> c, Collection <? extends T> desired) 206 { 207 return Find.findElement(c, desired); 208 } 209 210 211 222 static public <T> Iterator <T> 223 findElement(Collection <? extends T> c, Collection <? extends T> desired, 224 BinaryFunctor<T,T,Boolean > fn) 225 { 226 return Find.findElement(c, desired, fn); 227 } 228 229 233 243 static public <T> Iterator <T> match(Collection <? extends T> c, Collection <? extends T> pattern) { 244 return Find.findSequence(c, pattern); 245 } 246 247 248 259 static public <T> Iterator <T> 260 match(Collection <? extends T> c, Collection <? extends T> pattern, BinaryFunctor<T,T,Boolean > eq){ 261 return Find.findSequence(c, pattern, eq); 262 } 263 264 265 275 static public <T> Iterator <T> 276 mismatch(Collection <? extends T> c, Collection <? extends T> pattern) 277 { 278 return Find.findMismatch(c, pattern); 279 } 280 281 282 293 static public <T> Iterator <T> 294 mismatch(Collection <? extends T> c, Collection <? extends T> pattern, 295 BinaryFunctor<T,T,Boolean > neq) 296 { 297 return Find.findMismatch(c, pattern, neq); 298 } 299 300 304 314 static public <T> Iterator <T> findRepeated(Collection <? extends T> c, int n, T value) { 315 return Find.findRepeated(c, n, value); 316 } 317 318 319 329 static public <T> Iterator <T> 330 findRepeated(Collection <? extends T> c, int n, T value, Equality<T> eq) 331 { 332 return Find.findRepeated(c, n, value, eq); 333 } 334 335 336 346 static public <T> Iterator <T> 347 findRepeated(Collection <? extends T> c, int n, UnaryFunctor<T, Boolean > eq) 348 { 349 return Find.findRepeated(c, n, eq); 350 } 351 352 356 362 static public <T,R> UnaryFunctor<T,R> forEach(Collection <? extends T> c,UnaryFunctor<T,R> fn) { 363 new ForEach<T,R>(fn).fn(c.iterator()); 364 return fn; 365 } 366 367 368 372 376 static public <T> boolean equal(Collection <? extends T> c1, Collection <? extends T> c2) { 377 return new EqualTo<Collection <? extends T>>().p(c1,c2); 378 } 379 380 381 386 static public <T> boolean 387 equal(Collection <? extends T> c1, Collection <? extends T> c2, Comparator <T> comp) { 388 return Iterators.equal(c1.iterator(), c2.iterator(), comp); 389 } 390 391 392 400 static public <T> boolean 401 equal(Collection <? extends T> c1, Collection <? extends T> c2, BinaryFunctor<T,T,Boolean > eq) { 402 return Iterators.equal(c1.iterator(), c2.iterator(), eq); 403 } 404 405 409 415 static public <T extends Comparable <? super T>> boolean 416 lessThan(Collection <? extends T> c1, Collection <? extends T> c2) { 417 return Iterators.lessThan(c1.iterator(), c2.iterator()); 418 } 419 420 421 426 static public <T> boolean 427 lessThan(Collection <? extends T> c1, Collection <? extends T> c2, Comparator <T> comp) { 428 return Iterators.lessThan(c1.iterator(), c2.iterator(), comp); 429 } 430 431 432 439 static public <T> boolean 440 lessThan(Collection <? extends T> c1, Collection <? extends T> c2, BinaryFunctor<T,T,Boolean > lt) { 441 return Iterators.lessThan(c1.iterator(), c2.iterator(), lt); 442 } 443 444 448 456 static public <T extends Comparable <? super T>> Iterator <T> 457 minimum(Collection <? extends T> c) 458 { 459 return Find.find(c, Summarize.min(c)); 460 } 461 462 463 471 static public <T> Iterator <T> minimum(Collection <? extends T> c, Comparator <T> comp) { 472 return Find.find(c, Summarize.min(c,comp)); 473 } 474 475 476 485 static public <T> Iterator <T> minimum(Collection <? extends T> c, BinaryFunctor<T,T,T> bf) { 486 return Find.find(c, Summarize.min(c,bf)); 487 } 488 489 490 496 static public <T extends Comparable <? super T>> T 497 minimumValue(Collection <? extends T> c) 498 { 499 return Summarize.min(c); 500 } 501 502 503 508 static public <T> T minimumValue(Collection <? extends T> c, Comparator <T> comp) { 509 return Summarize.min(c,comp); 510 } 511 512 513 520 static public <T> T minimumValue(Collection <? extends T> c, BinaryFunctor<T,T,T> bf) { 521 return Summarize.min(c,bf); 522 } 523 524 525 533 static public <T extends Comparable <? super T>> Iterator <T> 534 maximum(Collection <? extends T> c) 535 { 536 return Find.find(c, Summarize.max(c)); 537 } 538 539 540 548 static public <T > Iterator <T> maximum(Collection <? extends T> c, Comparator <T> comp) { 549 return Find.find(c, Summarize.max(c,comp)); 550 } 551 552 553 562 static public <T> Iterator <T> maximum(Collection <? extends T> c, BinaryFunctor<T,T,T> bf) { 563 return Find.find(c, Summarize.max(c, bf)); 564 } 565 566 567 573 static public <T extends Comparable <? super T>> T 574 maximumValue(Collection <? extends T> c) 575 { 576 return Summarize.max(c); 577 } 578 579 580 585 static public <T> T maximumValue(Collection <? extends T> c, Comparator <T> comp) { 586 return Summarize.max(c,comp); 587 } 588 589 590 597 static public <T> T maximumValue(Collection <? extends T> c, BinaryFunctor<T,T,T> fn) { 598 return Summarize.max(c,fn); 599 } 600 601 605 611 static public <T extends Number > T accumulate(Class <T> type, Collection <T> c) { 612 return Summarize.sum(type, c); 613 } 614 615 616 627 static public <T extends Number > T 628 accumulate(Class <T> type, Collection <T> c, BinaryFunctor<T,T,T> fn) 629 { 630 Arithmetic<T> _math = ArithmeticFactory.getArithmetic(type); 631 if (_math == null) { 632 throw new IllegalArgumentException (); 633 } 634 635 return Summarize.accumulate(c, _math.zero(), fn); 636 } 637 638 639 649 static public <T> T accumulate(Collection <T> c, T initial, BinaryFunctor<T,T,T> fn) { 650 return Summarize.accumulate(c, initial, fn); 651 } 652 653 657 663 static public <T> void transform(List <T> lin, UnaryFunctor<T,T> uf) { 664 ListIterator <T> liter = lin.listIterator(); 667 while(liter.hasNext()) { 668 liter.set(uf.fn(liter.next())); 669 } 670 } 671 672 673 681 static public <T,R> Collection <? super R> 682 transformCopy(Collection <? extends T> cin, Collection <? super R> cout, UnaryFunctor<T,R> uf) 683 { 684 return Transform.transform( cin, uf, cout); 685 } 686 687 688 696 static public <T1,T2,R> Collection <? super R> 697 transformCopy(Collection <? extends T1> c1, Collection <? extends T2> c2, 698 Collection <? super R> cout, BinaryFunctor<T1,T2,R> bf) 699 { 700 return Transform.transform((Collection <T1>)c1, (Collection <T2>)c2, bf, cout); 701 } 702 703 707 713 static public <T> void replaceAll(List <T> lin, UnaryFunctor<T,Boolean > uf, T value) { 714 ListIterator <T> liter = lin.listIterator(); 717 while(liter.hasNext()) { 718 if (uf.fn(liter.next()).booleanValue()) { 719 liter.set(value); 720 } 721 } 722 } 723 724 725 731 static public <T,R> Collection <? super T> 732 replaceAllCopy(Collection <? extends T> cin, Collection <? super T> cout, 733 UnaryFunctor<T,Boolean > test,T value) 734 { 735 return Transform.replace(cin, test, value, cout); 736 } 737 738 742 745 static public <T> void removeAll(List <? extends T> lin, T value) { 746 removeAll(lin, new EqualTo<T>().bind2nd(value)); 747 } 748 749 750 753 static public <T> void removeAll(List <? extends T> lin, T value, Equality<T> eq) { 754 removeAll(lin, eq.bind2nd(value)); 755 } 756 757 758 761 static public <T> void removeAll(List <? extends T> lin, UnaryFunctor<T,Boolean > uf) { 762 ListIterator <? extends T> liter = lin.listIterator(); 763 while (liter.hasNext()) { 764 if (uf.fn(liter.next()).booleanValue()) { 765 liter.remove(); 766 } 767 } 768 } 769 770 771 776 static public <T> Collection <? super T> 777 removeAllCopy(Collection <? extends T> cin, Collection <? super T> cout, T value) { 778 return Filter.remove(cin, value, cout); 779 } 780 781 782 787 static public <T> Collection <? super T> 788 removeAllCopy(Collection <? extends T> cin, Collection <? super T> cout, T value, Equality<T> eq) { 789 return Filter.remove(cin, value, eq, cout); 790 } 791 792 793 798 static public <T> Collection <? super T> 799 removeAllCopy(Collection <? extends T> cin, Collection <? super T> cout, 800 UnaryFunctor<T,Boolean > pred) 801 { 802 return Filter.remove(cin, pred, cout); 803 } 804 805 809 813 static public <T> void unique(List <? extends T> lin) { 814 unique(lin, new EqualTo<T>()); 815 } 816 817 818 822 static public <T> void unique(List <? extends T> lin, BinaryFunctor<T,T,Boolean > eq) { 823 ListIterator <? extends T> liter = lin.listIterator(); 824 825 T last = null; 826 if (liter.hasNext()) 828 last = liter.next(); 829 830 while (liter.hasNext()) { 831 T next = liter.next(); 832 if (eq.fn(last, next).booleanValue()) { 833 liter.remove(); 834 } 835 else { 836 last = next; 837 } 838 } 839 } 840 841 842 847 static public <T, CT extends Collection <? super T>> CT 848 uniqueCopy(Collection <? extends T> cin, CT cout) { 849 return Unique.unique(cin, cout); 850 } 851 852 853 858 static public <T, CT extends Collection <? super T>> CT 859 uniqueCopy (Collection <? extends T> cin, CT cout, BinaryFunctor<T,T,Boolean > eq) { 860 return Unique.unique(cin, eq, cout); 861 } 862 863 867 871 static public <T extends Comparable <? super T>> Collection <? super T> 872 mergeCopy (Collection <? extends T> cin1, Collection <? extends T> cin2, 873 Collection <? super T> cout) 874 { 875 return Merge.merge(cin1, cin2, cout); 876 } 877 878 879 884 static public <T> Collection <? super T> 885 mergeCopy (Collection <? extends T> cin1, Collection <? extends T> cin2, 886 Collection <? super T> cout, Comparator <T> comp) 887 { 888 return Merge.merge(cin1, cin2, comp, cout); 889 } 890 891 892 896 904 static public <T> boolean addAll(Collection <? super T> cout, Iterator <T> iter) { 905 return CollectionUtils.addAll(cout, iter); 906 } 907 908 914 static public <T,TCollection extends Collection <? super T>> TCollection 915 append(TCollection cout, Iterator <T> iter) 916 { 917 return CollectionUtils.append(cout, iter); 918 } 919 920 921 928 static public <T,TCollection extends Collection <? super T>> TCollection 929 append(TCollection cout, T... values) 930 { 931 return CollectionUtils.append(cout, values); 932 } 933 } 934 | Popular Tags |