KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > co > jezuk > mango > Algorithms


1 package uk.co.jezuk.mango;
2
3 /**
4  * The Mango Algorithms Library.
5  *
6  * @author Jez Higgins, jez@jezuk.co.uk
7  * @version $Id: Algorithms.java 115 2006-09-28 21:18:09Z jez $
8  */

9 public class Algorithms
10 {
11     /**
12    * Algorithm intersection finds the common elements in both collections
13      * See http://en.wikipedia.org/wiki/Intersection_(set_theory)
14      */

15     static public java.util.Collection JavaDoc intersection(java.util.Collection JavaDoc coll1, java.util.Collection JavaDoc coll2, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Intersection.execute(coll1.iterator(), coll2, results); }
16     static public java.util.Collection JavaDoc intersection(java.util.Iterator JavaDoc iter1, java.util.Collection JavaDoc coll2, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Intersection.execute(iter1, coll2, results); }
17     static public java.util.Collection JavaDoc intersection(java.util.Iterator JavaDoc iter1, java.util.Iterator JavaDoc iter2, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Intersection.execute(iter1, iter2, results); }
18
19     /**
20    * Algorithm symmetricDifference returns the elements that are on coll1
21      * and not in coll2, and those elements in coll2 that are not in coll1.
22      * See http://en.wikipedia.org/wiki/Symmetric_difference
23      */

24     static public java.util.Collection JavaDoc symmetricDifference(java.util.Collection JavaDoc coll1, java.util.Collection JavaDoc coll2, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.SymmetricDifference.execute(coll1.iterator(), coll2, results); }
25     static public java.util.Collection JavaDoc symmetricDifference(java.util.Iterator JavaDoc iter1, java.util.Collection JavaDoc coll2, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.SymmetricDifference.execute(iter1, coll2, results); }
26     static public java.util.Collection JavaDoc symmetricDifference(java.util.Iterator JavaDoc iter1, java.util.Iterator JavaDoc iter2, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.SymmetricDifference.execute(iter1, iter2, results); }
27
28
29   /**
30    * The algorithm ForEach applies the function <code>fn</code> to
31    * each element in the <code>iterator</code> sequence.
32    */

33   static public void forEach(java.util.Collection JavaDoc collection, UnaryFunction fn) { uk.co.jezuk.mango.algorithms.ForEach.execute(collection.iterator(), fn); }
34   static public void forEach(java.util.Collection JavaDoc collection, int start, int end, UnaryFunction fn) { uk.co.jezuk.mango.algorithms.ForEach.execute(Iterators.BoundedIterator(collection.iterator(), start, end), fn); }
35   static public void forEach(java.util.List JavaDoc list, int start, int end, UnaryFunction fn) { uk.co.jezuk.mango.algorithms.ForEach.execute(Iterators.BoundedIterator(list, start, end), fn); }
36   static public void forEach(java.util.Iterator JavaDoc iterator, UnaryFunction fn) { uk.co.jezuk.mango.algorithms.ForEach.execute(iterator, fn); }
37
38   /**
39    * The algorithm Transform applies the function <code>fn</code> to
40    * each element in the <code>iterator</code> sequence.
41    * The return value of <code>fn</code> is added to the <code>results</code>
42    * collection. If the return value of <code>fn</code> is itself a
43    * collection, then each member of that collection is added to
44    * <code>results</code>.
45    */

46   static public java.util.Collection JavaDoc transform(java.util.Collection JavaDoc collection, UnaryFunction fn, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Transform.execute(collection.iterator(), fn, results); }
47   static public java.util.Collection JavaDoc transform(java.util.Collection JavaDoc collection, int start, int end, UnaryFunction fn, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Transform.execute(Iterators.BoundedIterator(collection.iterator(), start, end), fn, results); }
48   static public java.util.Collection JavaDoc transform(java.util.List JavaDoc list, int start, int end, UnaryFunction fn, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Transform.execute(Iterators.BoundedIterator(list, start, end), fn, results); }
49   static public java.util.Collection JavaDoc transform(java.util.Iterator JavaDoc iterator, UnaryFunction fn, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Transform.execute(iterator, fn, results); }
50
51   /**
52    * <code>Count</code> computes the number of elements in the sequence that
53    * are equal to <code>value</code>. <br>
54    * <code>value</code> may be <code>null</code>.<br>
55    * The objects in the sequence and <code>value</code> must be comparable using
56    * <code>Object.equals</code> (unless <code>value</code> is <code>null</code>).
57    */

58   static public int count(java.util.Collection JavaDoc collection, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Count.execute(collection.iterator(), value); }
59   static public int count(java.util.Collection JavaDoc collection, int start, int end, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Count.execute(Iterators.BoundedIterator(collection.iterator(), start, end), value); }
60   static public int count(java.util.List JavaDoc list, int start, int end, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Count.execute(Iterators.BoundedIterator(list, start, end), value); }
61   static public int count(java.util.Iterator JavaDoc iterator, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Count.execute(iterator, value); }
62   
63   /**
64    * <code>CountIf</code> is similar to <code>Count</code>, but more general.
65    * It computes the number of elements in the sequence which satisfy some condition.
66    * The condition is a described in the user-supplied <code>test</code> object, and
67    * <code>CountIf</code> computes the number of objects such that <code>test.test(o)</code>
68    * is <code>true</code>.
69    */

70   static public int countIf(java.util.Collection JavaDoc collection, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIf.execute(collection.iterator(), test); }
71   static public int countIf(java.util.Collection JavaDoc collection, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIf.execute(Iterators.BoundedIterator(collection.iterator(), start, end), test); }
72   static public int countIf(java.util.List JavaDoc list, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIf.execute(Iterators.BoundedIterator(list, start, end), test); }
73   static public int countIf(java.util.Iterator JavaDoc iterator, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIf.execute(iterator, test); }
74   
75   /**
76    * <code>CountIfNot</code> is the complement of <code>CountIf</code>.
77    * It counts the number of elements in the sequence which fail some condition.
78    * The condition is a described in the user-supplied <code>test</code> object, and
79    * <code>CountIfNot</code> computes the number of objects such that <code>test.test(o)</code>
80    * is <code>false</code>.
81    */

82   static public int countIfNot(java.util.Collection JavaDoc collection, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIfNot.execute(collection.iterator(), test); }
83   static public int countIfNot(java.util.Collection JavaDoc collection, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIfNot.execute(Iterators.BoundedIterator(collection.iterator(), start, end), test); }
84   static public int countIfNot(java.util.List JavaDoc list, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIfNot.execute(Iterators.BoundedIterator(list, start, end), test); }
85   static public int countIfNot(java.util.Iterator JavaDoc iterator, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIfNot.execute(iterator, test); }
86   
87   /**
88    * Searchs the sequence travesed by the Iterator for the given value.
89    * Returns the <code>Object</code>, or <code>null</code> if the value
90    * is not found. The iterator will have been advanced to the next object
91    * in the sequence.
92    * The objects in the sequence and <code>value</code> must be comparable using
93    * <code>Object.equals</code> (unless <code>value</code> is <code>null</code>).
94    */

95   static public Object JavaDoc find(java.util.Collection JavaDoc collection, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Find.execute(collection.iterator(), value); }
96   static public Object JavaDoc find(java.util.Collection JavaDoc collection, int start, int end, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Find.execute(Iterators.BoundedIterator(collection.iterator(), start, end), value); }
97   static public Object JavaDoc find(java.util.List JavaDoc list, int start, int end, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Find.execute(Iterators.BoundedIterator(list, start, end), value); }
98   static public Object JavaDoc find(java.util.Iterator JavaDoc iterator, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Find.execute(iterator, value); }
99   
100   /**
101    * Searchs the sequence travesed by the Iterator for the given value.
102    * Returns the index of the value in the collection, or <code>-1</code>
103      * if the value is not found. The iterator will have been advanced to
104      * the next object in the sequence.
105    * The objects in the sequence and <code>value</code> must be comparable using
106    * <code>Object.equals</code> (unless <code>value</code> is <code>null</code>).
107    */

108   static public int findPosition(java.util.Collection JavaDoc collection, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.FindPosition.execute(collection.iterator(), value); }
109   static public int findPosition(java.util.Collection JavaDoc collection, int start, int end, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.FindPosition.execute(Iterators.BoundedIterator(collection.iterator(), start, end), value); }
110   static public int findPosition(java.util.List JavaDoc list, int start, int end, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.FindPosition.execute(Iterators.BoundedIterator(list, start, end), value); }
111   static public int findPosition(java.util.Iterator JavaDoc iterator, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.FindPosition.execute(iterator, value); }
112   static public int findPositionIf(java.util.Collection JavaDoc collection, Predicate pred) { return uk.co.jezuk.mango.algorithms.FindPositionIf.execute(collection.iterator(), pred); }
113   static public int findPositionIf(java.util.Collection JavaDoc collection, int start, int end, Predicate pred) { return uk.co.jezuk.mango.algorithms.FindPositionIf.execute(Iterators.BoundedIterator(collection.iterator(), start, end), pred); }
114   static public int findPositionIf(java.util.List JavaDoc list, int start, int end, Predicate pred) { return uk.co.jezuk.mango.algorithms.FindPositionIf.execute(Iterators.BoundedIterator(list, start, end), pred); }
115   static public int findPositionIf(java.util.Iterator JavaDoc iterator, Predicate pred) { return uk.co.jezuk.mango.algorithms.FindPositionIf.execute(iterator, pred); }
116   
117   /**
118    * Searchs the sequence traversed by the Iterator and returns the first
119    * object encountered for which the Predicate returns <code>true</code>.
120    * Returns the <code>Object</code>, or <code>null</code> if the value
121    * is not found. The iterator will have been advanced to the next object
122    * in the sequence.
123    */

124   static public Object JavaDoc findIf(java.util.Collection JavaDoc collection, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIf.execute(collection.iterator(), test); }
125   static public Object JavaDoc findIf(java.util.Collection JavaDoc collection, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIf.execute(Iterators.BoundedIterator(collection.iterator(), start, end), test); }
126   static public Object JavaDoc findIf(java.util.List JavaDoc list, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIf.execute(Iterators.BoundedIterator(list, start, end), test); }
127   static public Object JavaDoc findIf(java.util.Iterator JavaDoc iterator, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIf.execute(iterator, test); }
128
129   /**
130    * Searchs the sequence traversed by the Iterator and returns the first
131    * object encountered for which the Predicate returns <code>false</code>.
132    * The iterator will have been advanced to the next object
133    * in the sequence.
134    */

135   static public Object JavaDoc findIfNot(java.util.Collection JavaDoc collection, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIfNot.execute(collection.iterator(), test); }
136   static public Object JavaDoc findIfNot(java.util.Collection JavaDoc collection, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIfNot.execute(Iterators.BoundedIterator(collection.iterator(), start, end), test); }
137   static public Object JavaDoc findIfNot(java.util.List JavaDoc list, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIfNot.execute(Iterators.BoundedIterator(list, start, end), test); }
138   static public Object JavaDoc findIfNot(java.util.Iterator JavaDoc iterator, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIfNot.execute(iterator, test); }
139
140   /**
141    * Removes objects equal to <code>value</code> from the sequence.
142    */

143   static public void remove(java.util.Collection JavaDoc collection, Object JavaDoc value) { uk.co.jezuk.mango.algorithms.Remove.execute(collection.iterator(), value); }
144   static public void remove(java.util.Collection JavaDoc collection, int start, int end, Object JavaDoc value) { uk.co.jezuk.mango.algorithms.Remove.execute(Iterators.BoundedIterator(collection.iterator(), start, end), value); }
145   static public void remove(java.util.List JavaDoc list, int start, int end, Object JavaDoc value) { uk.co.jezuk.mango.algorithms.Remove.execute(Iterators.BoundedIterator(list, start, end), value); }
146   static public void remove(java.util.Iterator JavaDoc iterator, Object JavaDoc value) { uk.co.jezuk.mango.algorithms.Remove.execute(iterator, value); }
147   
148   /**
149    * Removes objects which match <code>test</code> from the sequence.
150    */

151   static public void removeIf(java.util.Collection JavaDoc collection, Predicate pred) { uk.co.jezuk.mango.algorithms.RemoveIf.execute(collection.iterator(), pred); }
152   static public void removeIf(java.util.Collection JavaDoc collection, int start, int end, Predicate pred) { uk.co.jezuk.mango.algorithms.RemoveIf.execute(Iterators.BoundedIterator(collection.iterator(), start, end), pred); }
153   static public void removeIf(java.util.List JavaDoc list, int start, int end, Predicate pred) { uk.co.jezuk.mango.algorithms.RemoveIf.execute(Iterators.BoundedIterator(list, start, end), pred); }
154   static public void removeIf(java.util.Iterator JavaDoc iterator, Predicate pred) { uk.co.jezuk.mango.algorithms.RemoveIf.execute(iterator, pred); }
155  
156  /**
157    * Partitions the supplied collections into two. Objects matching the Predicate
158      * are removed from the collection and added to the results Collection.
159      * Returns the result collection.
160    */

161   static public java.util.Collection JavaDoc partition(java.util.Collection JavaDoc collection, Predicate pred, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Partition.execute(collection.iterator(), pred, results); }
162   static public java.util.Collection JavaDoc partition(java.util.Collection JavaDoc collection, int start, int end, Predicate pred, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Partition.execute(Iterators.BoundedIterator(collection.iterator(), start, end), pred, results); }
163   static public java.util.Collection JavaDoc partition(java.util.List JavaDoc list, int start, int end, Predicate pred, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Partition.execute(Iterators.BoundedIterator(list, start, end), pred, results); }
164   static public java.util.Collection JavaDoc partition(java.util.Iterator JavaDoc iterator, Predicate pred, java.util.Collection JavaDoc results) { return uk.co.jezuk.mango.algorithms.Partition.execute(iterator, pred, results); }
165
166   /**
167    * Removes duplicate elements. Whenever a consecutive groups of duplicate objects
168    * occur in the sequence, <code>unique</code> removes all but the first objects
169    * in each group.
170    * <code>iterator</code> must support the <code>remove</code> method.
171    * @see java.util.Iterator
172    * @see java.util.Comparator
173    */

174     static public void unique(java.util.Collection JavaDoc collection) { uk.co.jezuk.mango.algorithms.Unique.execute(collection.iterator(), null); }
175     static public void unique(java.util.Collection JavaDoc collection, int start, int end) { uk.co.jezuk.mango.algorithms.Unique.execute(Iterators.BoundedIterator(collection.iterator(), start, end), null); }
176     static public void unique(java.util.List JavaDoc list, int start, int end) { uk.co.jezuk.mango.algorithms.Unique.execute(Iterators.BoundedIterator(list, start, end), null); }
177     static public void unique(java.util.Iterator JavaDoc iterator) { uk.co.jezuk.mango.algorithms.Unique.execute(iterator, null); }
178     static public void unique(java.util.Collection JavaDoc collection, java.util.Comparator JavaDoc comparator) { uk.co.jezuk.mango.algorithms.Unique.execute(collection.iterator(), comparator); }
179     static public void unique(java.util.Collection JavaDoc collection, int start, int end, java.util.Comparator JavaDoc comparator) { uk.co.jezuk.mango.algorithms.Unique.execute(Iterators.BoundedIterator(collection.iterator(), start, end), comparator); }
180     static public void unique(java.util.List JavaDoc list, int start, int end, java.util.Comparator JavaDoc comparator) { uk.co.jezuk.mango.algorithms.Unique.execute(Iterators.BoundedIterator(list, start, end), comparator); }
181     static public void unique(java.util.Iterator JavaDoc iterator, java.util.Comparator JavaDoc comparator) { uk.co.jezuk.mango.algorithms.Unique.execute(iterator, comparator); }
182   
183   private Algorithms() { } // prevent instantiation
184
} // public class Algorithms
185
Popular Tags