KickJava   Java API By Example, From Geeks To Geeks.

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


1 package uk.co.jezuk.mango;
2
3 /**
4  * The Mango Library.
5  *
6  * @author Jez Higgins, jez@jezuk.co.uk
7  * @version $Id: Mango.java 97 2004-05-26 08:35:52Z jez $
8  * @deprecated see {@link Algorithms}, {@link Iterators}, {@link Predicates}
9  */

10 public class Mango
11 {
12   /////////////////////////
13
// algorithms
14
/**
15    * The algorithm ForEach applies the function <code>fn</code> to
16    * each element in the <code>iterator</code> sequence.
17    */

18   static public void forEach(java.util.Collection JavaDoc collection, UnaryFunction fn) { uk.co.jezuk.mango.algorithms.ForEach.execute(collection.iterator(), fn); }
19   static public void forEach(java.util.Collection JavaDoc collection, int start, int end, UnaryFunction fn) { uk.co.jezuk.mango.algorithms.ForEach.execute(BoundedIterator(collection.iterator(), start, end), fn); }
20   static public void forEach(java.util.List JavaDoc list, int start, int end, UnaryFunction fn) { uk.co.jezuk.mango.algorithms.ForEach.execute(BoundedIterator(list, start, end), fn); }
21   static public void forEach(java.util.Iterator JavaDoc iterator, UnaryFunction fn) { uk.co.jezuk.mango.algorithms.ForEach.execute(iterator, fn); }
22
23   /**
24    * The algorithm Transform applies the function <code>fn</code> to
25    * each element in the <code>iterator</code> sequence.
26    * The return value of <code>fn</code> is added to the <code>results</code>
27    * collection. If the return value of <code>fn</code> is itself a
28    * collection, then each member of that collection is added to
29    * <code>results</code>.
30    */

31   static public void transform(java.util.Collection JavaDoc collection, UnaryFunction fn, java.util.Collection JavaDoc results) { uk.co.jezuk.mango.algorithms.Transform.execute(collection.iterator(), fn, results); }
32   static public void transform(java.util.Collection JavaDoc collection, int start, int end, UnaryFunction fn, java.util.Collection JavaDoc results) { uk.co.jezuk.mango.algorithms.Transform.execute(BoundedIterator(collection.iterator(), start, end), fn, results); }
33   static public void transform(java.util.List JavaDoc list, int start, int end, UnaryFunction fn, java.util.Collection JavaDoc results) { uk.co.jezuk.mango.algorithms.Transform.execute(BoundedIterator(list, start, end), fn, results); }
34   static public void transform(java.util.Iterator JavaDoc iterator, UnaryFunction fn, java.util.Collection JavaDoc results) { uk.co.jezuk.mango.algorithms.Transform.execute(iterator, fn, results); }
35
36   /**
37    * <code>Count</code> computes the number of elements in the sequence that
38    * are equal to <code>value</code>. <br>
39    * <code>value</code> may be <code>null</code>.<br>
40    * The objects in the sequence and <code>value</code> must be comparable using
41    * <code>Object.equals</code> (unless <code>value</code> is <code>null</code>).
42    */

43   static public int count(java.util.Collection JavaDoc collection, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Count.execute(collection.iterator(), value); }
44   static public int count(java.util.Collection JavaDoc collection, int start, int end, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Count.execute(BoundedIterator(collection.iterator(), start, end), value); }
45   static public int count(java.util.List JavaDoc list, int start, int end, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Count.execute(BoundedIterator(list, start, end), value); }
46   static public int count(java.util.Iterator JavaDoc iterator, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Count.execute(iterator, value); }
47   
48   /**
49    * <code>CountIf</code> is similar to <code>Count</code>, but more general.
50    * It computes the number of elements in the sequence which satisfy some condition.
51    * The condition is a described in the user-supplied <code>test</code> object, and
52    * <code>CountIf</code> computes the number of objects such that <code>test.test(o)</code>
53    * is <code>true</code>.
54    */

55   static public int countIf(java.util.Collection JavaDoc collection, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIf.execute(collection.iterator(), test); }
56   static public int countIf(java.util.Collection JavaDoc collection, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIf.execute(BoundedIterator(collection.iterator(), start, end), test); }
57   static public int countIf(java.util.List JavaDoc list, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIf.execute(BoundedIterator(list, start, end), test); }
58   static public int countIf(java.util.Iterator JavaDoc iterator, Predicate test) { return uk.co.jezuk.mango.algorithms.CountIf.execute(iterator, test); }
59   
60   /**
61    * Searchs the sequence travesed by the Iterator for the given value.
62    * Returns the <code>Object</code>, or <code>null</code> if the value
63    * is not found. The iterator will have been advanced to the next object
64    * in the sequence.
65    * The objects in the sequence and <code>value</code> must be comparable using
66    * <code>Object.equals</code> (unless <code>value</code> is <code>null</code>).
67    */

68   static public Object JavaDoc find(java.util.Collection JavaDoc collection, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Find.execute(collection.iterator(), value); }
69   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(BoundedIterator(collection.iterator(), start, end), value); }
70   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(BoundedIterator(list, start, end), value); }
71   static public Object JavaDoc find(java.util.Iterator JavaDoc iterator, Object JavaDoc value) { return uk.co.jezuk.mango.algorithms.Find.execute(iterator, value); }
72   
73   /**
74    * Searchs the sequence traversed by the Iterator and returns the first
75    * object encountered for which the Predicate returns <code>true</code>.
76    * Returns the <code>Object</code>, or <code>null</code> if the value
77    * is not found. The iterator will have been advanced to the next object
78    * in the sequence.
79    */

80   static public Object JavaDoc findIf(java.util.Collection JavaDoc collection, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIf.execute(collection.iterator(), test); }
81   static public Object JavaDoc findIf(java.util.Collection JavaDoc collection, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIf.execute(BoundedIterator(collection.iterator(), start, end), test); }
82   static public Object JavaDoc findIf(java.util.List JavaDoc list, int start, int end, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIf.execute(BoundedIterator(list, start, end), test); }
83   static public Object JavaDoc findIf(java.util.Iterator JavaDoc iterator, Predicate test) { return uk.co.jezuk.mango.algorithms.FindIf.execute(iterator, test); }
84
85   /**
86    * Removes objects equal to <code>value</code> from the sequence.
87    */

88   static public void remove(java.util.Collection JavaDoc collection, Object JavaDoc value) { uk.co.jezuk.mango.algorithms.Remove.execute(collection.iterator(), value); }
89   static public void remove(java.util.Collection JavaDoc collection, int start, int end, Object JavaDoc value) { uk.co.jezuk.mango.algorithms.Remove.execute(BoundedIterator(collection.iterator(), start, end), value); }
90   static public void remove(java.util.List JavaDoc list, int start, int end, Object JavaDoc value) { uk.co.jezuk.mango.algorithms.Remove.execute(BoundedIterator(list, start, end), value); }
91   static public void remove(java.util.Iterator JavaDoc iterator, Object JavaDoc value) { uk.co.jezuk.mango.algorithms.Remove.execute(iterator, value); }
92   
93   /**
94    * Removes objects which match <code>test</code> from the sequence.
95    */

96   static public void removeIf(java.util.Collection JavaDoc collection, Predicate pred) { uk.co.jezuk.mango.algorithms.RemoveIf.execute(collection.iterator(), pred); }
97   static public void removeIf(java.util.Collection JavaDoc collection, int start, int end, Predicate pred) { uk.co.jezuk.mango.algorithms.RemoveIf.execute(BoundedIterator(collection.iterator(), start, end), pred); }
98   static public void removeIf(java.util.List JavaDoc list, int start, int end, Predicate pred) { uk.co.jezuk.mango.algorithms.RemoveIf.execute(BoundedIterator(list, start, end), pred); }
99   static public void removeIf(java.util.Iterator JavaDoc iterator, Predicate pred) { uk.co.jezuk.mango.algorithms.RemoveIf.execute(iterator, pred); }
100   
101   //////////////////////////////////////////
102
// Iterators
103
/**
104    * A <code>BoundedIterator</code> enumerates of a subset of a collection, in the
105    * range [<code>start</code>, <code>end</code>).
106    * <p>
107    * A conventional <code>java.util.Iterator</code>, obtained by a call to say
108    * <code>java.util.List.iterator()</code>, travels the entire sequence of the
109    * <code>java.util.Collection</code> it points to. It starts at the beginning
110    * and keeps on going until you hit the end or get bored.
111    * <p>
112    * A BoundedIterator enumerates of a subset of a collection, in the range [start, end) -
113    * a normal <code>java.util.Iterator</code> traverses [0, collection.size()). A
114    * <code>BoundedIterator</code> therefore allows you to pick out a sub-set without
115    * using <code>list.subList()</code> or equivalent.
116    */

117   static public java.util.Iterator JavaDoc BoundedIterator(java.util.Iterator JavaDoc iterator, int start, int end) { return new uk.co.jezuk.mango.iterators.BoundedIterator(iterator, start, end); }
118   static public java.util.Iterator JavaDoc BoundedIterator(java.util.List JavaDoc list, int start, int end) { return new uk.co.jezuk.mango.iterators.BoundedIterator(list, start, end); }
119
120   /**
121    * A <code>PredicatedIterator</code> enumerates only those elements of a collection
122    * that match the supplied <code>Predicate</code>.
123    * <p>
124    * It takes a {@link Predicate} which encapsulates some test, and only
125    * returns those Objects in the sequence which pass the test.
126    * <p>
127    * Say you have a list of <code>String</code>s, myStringList and you're only
128    * interested in those that begin with 'S'. What you need is
129    *
130    * <pre>
131 Iterator iter = Mango.PredicatedIterator(myStringList.iterator(),
132                                        new {@link Predicate}() {
133                                            boolean test(Object o) {
134                                              String s = (String)o;
135                                              return s.charAt(0) == 'S';
136                                            }
137                                        });</pre>
138    * <p>
139    * A <code>PredicatedIterator</code> implements the <code>java.util.Iterator</code> interface,
140    * and is constructed by wrapping around an existing iterator.
141    */

142   static public java.util.Iterator JavaDoc PredicatedIterator(java.util.Iterator JavaDoc iterator, Predicate predicate) { return new uk.co.jezuk.mango.iterators.SelectingIterator(iterator, predicate); }
143
144   /**
145    * Iterates over an array of objects.
146    * <p>
147    * An <code>ArrayIterator</code> puts a <code>java.util.Iterator</code> face on an
148    * object array, allowing it be treated as you would a <code>java.util.Collection.</code>
149    */

150   static public java.util.Iterator JavaDoc ArrayIterator(Object JavaDoc[] array) { return new uk.co.jezuk.mango.iterators.ArrayIterator(array); }
151
152   /**
153    * Iterates over a single object.
154    * <p>
155    * Usually an iterator moves over some sequence. A <code>SingletonIterator</code> treats a
156    * single object as it if it were a list containing one object. Since <code>SingletonIterator</code>
157    * implements the <code>java.util.Iterator</code> interface, it provides a convienent way of
158    * passing a single object to an algorithm or other iterator consumer.
159    */

160   static public java.util.Iterator JavaDoc SingletonIterator(Object JavaDoc object) { return new uk.co.jezuk.mango.iterators.SingletonIterator(object); }
161
162   /**
163    * A <code>TransfromIterator</code> applies a <code>{@link UnaryFunction}</code> to
164    * each element in the sequence, returning the the function result at each step.
165    * <p>
166    * Say you have a list of some complex type, and you want to find on by name.
167    * You could (caution! trivial example follows)
168    * <pre>
169      Iterator i = list.iterator();
170      while(i.hasNext()) {
171        MyComplexObject mco = (MyComplexObject)i.next();
172        if(mco.GetName().equals(theSearchName))
173          .. do something
174      }
175      // did I find it or not - do the right thing here
176      </pre>
177    * or you could
178    * <pre>
179      MyComplexObject mco = (MyComplexObject)Mango.find(
180                                 Mango.TransformIterator(list.iterator(),
181                                                         Adapt.ArgumentMethod("GetName"),
182                                 theSearchName);
183      if(mco != null)
184        ... found!
185      else
186        ...
187      </pre>
188    */

189   static public java.util.Iterator JavaDoc TransformIterator(java.util.Iterator JavaDoc iterator, UnaryFunction transform) { return new uk.co.jezuk.mango.iterators.TransformIterator(iterator, transform); }
190
191   ///////////////////////////////////////////////////////
192
// Unary Predicates
193
/**
194    * A <code>Predicate</code> which always returns <code>true</code>
195    */

196   static public Predicate True() { return new uk.co.jezuk.mango.unarypredicates.True(); }
197   /**
198    * A <code>Predicate</code> which always returns <code>false</code>
199    */

200   static public Predicate False() { return new uk.co.jezuk.mango.unarypredicates.False(); }
201   /**
202    * A <code>Predicate</code> which is the logical negation of some other <code>Predicate</code>. If <code>n</code>
203    * is a <code>Not</code> object, and <code>pred</code> is the <code>Predicate</code> it was constructed with,
204    * then <code>n.test(x)</code> returns <code>!pred.test(x)</code>.
205    */

206   static public Predicate Not(Predicate pred) { return new uk.co.jezuk.mango.unarypredicates.Not(pred); }
207   /**
208    * A <code>Predicate</code> which returns the logical AND of two other <code>Predicate</code>. If <code>a</code>
209    * is an <code>And</code> object, constructed with <code>pred1</code> and <code>pred2</code>, then
210    * <code>a.test(x)</code> returns <code>pred1.test(x) && pred2.test(x)</code>
211    */

212   static public Predicate And(Predicate pred1, Predicate pred2) { return new uk.co.jezuk.mango.unarypredicates.And(pred1, pred2); }
213   /**
214    * A <code>Predicate</code> which returns the logical OR of two other <code>Predicate</code>. If <code>a</code>
215    * is an <code>Or</code> object, constructed with <code>pred1</code> and <code>pred2</code>, then
216    * <code>a.test(x)</code> returns <code>pred1.test(x) || pred2.test(x)</code>
217    */

218   static public Predicate Or(Predicate pred1, Predicate pred2) { return new uk.co.jezuk.mango.unarypredicates.Or(pred1, pred2); }
219
220   /////////////////////////////////////////////////
221
// Binary Predicates
222
/**
223    * <code>BinaryPredicate</code> testing for equality.
224    * <code>true</code> if <code>x.equals(y)</code> or <code>(x == null && y == null)</code>
225    */

226   static public BinaryPredicate EqualTo() { return new uk.co.jezuk.mango.binarypredicates.EqualTo(); }
227   /**
228    * <code>BinaryPredicate</code> that returns true if <code>x</code> is greater than <code>y</code>.
229    * <code>x</code> and <code>y</code> must implement the <code>java.lang.Comparable<code> interface.
230    */

231   static public BinaryPredicate GreaterThan() { return new uk.co.jezuk.mango.binarypredicates.GreaterThan(); }
232   /**
233    * <code>BinaryPredicate</code> that returns true if <code>x</code> is greater than or equal to <code>y</code>.
234    * <code>x</code> and <code>y</code> must implement the <code>java.lang.Comparable<code> interface.
235    */

236   static public BinaryPredicate GreaterThanEquals() { return new uk.co.jezuk.mango.binarypredicates.GreaterThanEquals(); }
237   /**
238    * <code>BinaryPredicate</code> that returns true if <code>x</code> is less than <code>y</code>.
239    * <code>x</code> and <code>y</code> must implement the <code>java.lang.Comparable<code> interface.
240    */

241   static public BinaryPredicate LessThan() { return new uk.co.jezuk.mango.binarypredicates.LessThan(); }
242   /**
243    * <code>BinaryPredicate</code> that returns true if <code>x</code> is less than or equal to <code>y</code>.
244    * <code>x</code> and <code>y</code> must implement the <code>java.lang.Comparable<code> interface.
245    */

246   static public BinaryPredicate LessThanEquals() { return new uk.co.jezuk.mango.binarypredicates.LessThanEquals(); }
247   /**
248    * <code>true</code> if <code>not(x.equals(y))</code>, <code>(x == null) && not(y == null)</code> or <code>not(x == null) && (y == null)</code>
249    */

250   static public BinaryPredicate NotEqualTo() { return new uk.co.jezuk.mango.binarypredicates.NotEqualTo(); }
251   /**
252    * A <code>BinaryPredicate</code> which is the logical negation of some other <code>BinaryPredicate</code>. If <code>n</code>
253    * is a <code>Not</code> object, and <code>pred</code> is the <code>Predicate</code> it was constructed with,
254    * then <code>n.test(x,y)</code> returns <code>!pred.test(x,y)</code>.
255    */

256   static public BinaryPredicate Not(BinaryPredicate pred) { return new uk.co.jezuk.mango.binarypredicates.Not(pred); }
257   /**
258    * A <code>BinaryPredicate</code> which returns the logical AND of two other <code>BinaryPredicate</code>. If <code>a</code>
259    * is an <code>And</code> object, constructed with <code>pred1</code> and <code>pred2</code>, then
260    * <code>a.test(x,y)</code> returns <code>pred1.test(x,y) && pred2.test(x,y)</code>
261    */

262   static public BinaryPredicate And(BinaryPredicate pred1, BinaryPredicate pred2) { return new uk.co.jezuk.mango.binarypredicates.And(pred1, pred2); }
263   /**
264    * A <code>BinaryPredicate</code> which returns the logical OR of two other <code>BinaryPredicate</code>. If <code>a</code>
265    * is an <code>Or</code> object, constructed with <code>pred1</code> and <code>pred2</code>, then
266    * <code>a.test(x,y)</code> returns <code>pred1.test(x,y) || pred2.test(x,y)</code>
267    */

268   static public BinaryPredicate Or(BinaryPredicate pred1, BinaryPredicate pred2) { return new uk.co.jezuk.mango.binarypredicates.Or(pred1, pred2); }
269
270   //////////////////////////////////
271
private Mango() { }
272 } // Mango
273
Popular Tags