KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > algorithms > Summarize


1 // ============================================================================
2
// $Id: Summarize.java,v 1.5 2006/12/05 04:45:44 davidahall Exp $
3
// Copyright (c) 2006 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.algorithms;
34
35 import java.util.Collection JavaDoc;
36 import java.util.Comparator JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import net.sf.jga.fn.BinaryFunctor;
39 import net.sf.jga.fn.UnaryFunctor;
40 import net.sf.jga.fn.algorithm.Accumulate;
41 import net.sf.jga.fn.arithmetic.Arithmetic;
42 import net.sf.jga.fn.arithmetic.ArithmeticFactory;
43 import net.sf.jga.fn.arithmetic.Average;
44 import net.sf.jga.fn.arithmetic.Plus;
45 import net.sf.jga.fn.comparison.EqualTo;
46 import net.sf.jga.fn.comparison.Equality;
47 import net.sf.jga.fn.comparison.Max;
48 import net.sf.jga.fn.comparison.Min;
49 import net.sf.jga.util.ArrayUtils;
50
51
52 /**
53  * Algorithms that consume input and produce a single value. The input may be
54  * an array, collection, or iteration.
55  * <p>
56  * Copyright &copy; 2006 David A. Hall
57  */

58
59 public class Summarize {
60
61     // -------------------
62
// counting algorithms
63
// -------------------
64

65     /**
66      * Returns the number of times that the given value appears in the array
67      */

68     static public <T> long count(T[] ts, T value) {
69         return count(ArrayUtils.iterate(ts), new EqualTo<T>().bind2nd(value));
70     }
71
72     /**
73      * Returns the number of times that the given value appears in the array, using
74      * the given equality operator
75      */

76     static public <T> long count(T[] ts, Equality<T> eq, T value) {
77         return count(ArrayUtils.iterate(ts), eq.bind2nd(value));
78     }
79
80     /**
81      * Returns the number of elements in the array for which the predicate is true
82      */

83     static public <T> long count(T[] ts, UnaryFunctor<T,Boolean JavaDoc> pred) {
84         return count(ArrayUtils.iterate(ts), pred);
85     }
86
87     /**
88      * Returns the number of elements in the input.
89      */

90     static public <T> long count(Iterable JavaDoc<T> input) {
91         return count(input.iterator());
92     }
93
94     
95     /**
96      * Returns the number of times that the given value appears in the input.
97      */

98     static public <T> long count(Iterable JavaDoc<? extends T> input, T value) {
99         return count(input.iterator(), value);
100     }
101
102     
103     /**
104      * Returns the number of times that the given value appears in the input, using
105      * the given equality operator.
106      */

107     static public <T> long count(Iterable JavaDoc<? extends T> input, Equality<T> eq, T value) {
108         return count(input.iterator(), eq, value);
109     }
110
111     
112     /**
113      * Returns the number of elements in the input for which the predicate is true.
114      */

115     static public <T> long count(Iterable JavaDoc<? extends T> input, UnaryFunctor<T,Boolean JavaDoc> pred) {
116         return count(input.iterator(), pred);
117     }
118
119     /**
120      * Returns the number of elements in the iterator. The iterator is consumed entirely
121      * by this function.
122      */

123     static public <T> long count(Iterator JavaDoc<? extends T> iter) {
124         long ct = 0;
125         while(iter.hasNext()) {
126             iter.next();
127             ++ct;
128         }
129      
130         return ct;
131     }
132     
133
134     /**
135      * Returns the number of times that the given value appears in the iterator. The
136      * iterator is consumed entirely by this function.
137      */

138     static public <T> long count(Iterator JavaDoc<? extends T> iter, T value) {
139         return count(Filter.filter(iter, new EqualTo<T>().bind2nd(value)));
140     }
141
142     
143     /**
144      * Returns the number of times that the given value appears in the iterator, using
145      * the given equality operator. The iterator is consumed entirely by this function.
146      */

147     static public <T> long count(Iterator JavaDoc<? extends T> iter, Equality<T> eq, T value) {
148         return count(Filter.filter(iter, eq.bind2nd(value)));
149     }
150
151     
152     /**
153      * Returns the number of elements in the iterator for which the predicate is true. The
154      * iterator is consumed entirely by this function.
155      */

156     static public <T> long count(Iterator JavaDoc<? extends T> iter, UnaryFunctor<T,Boolean JavaDoc> pred) {
157         return count(Filter.filter(iter, pred));
158     }
159
160
161     // -------------------
162
// minimum value
163
// -------------------
164

165     /**
166      * Returns the smallest T value in the input, as determined by the given comparator.
167      */

168     static public <T> T min(T[] ts, Comparator JavaDoc<? super T> comp) {
169         return min(ArrayUtils.iterate(ts), new Min<T>(comp));
170     }
171
172
173     /**
174      * Returns the smallest T value in the input.
175      */

176     static public <T extends Comparable JavaDoc<? super T>> T min(T[] ts) {
177         return min(ArrayUtils.iterate(ts), new Min.Comparable<T>());
178     }
179
180
181     /**
182      * Returns the smallest T value in the input, as determined by the given functor.
183      * The functor is expected to return the smaller of its two arguments.
184      */

185     static public <T> T min(T[] ts, BinaryFunctor<T,T,T> fn) {
186         return min(ArrayUtils.iterate(ts), fn);
187     }
188
189
190     /**
191      * Returns the smallest T value in the input, as determined by the given comparator.
192      */

193     static public <T> T min(Iterable JavaDoc<? extends T> ts, Comparator JavaDoc<? super T> comp) {
194         return min(ts.iterator(), new Min<T>(comp));
195     }
196
197
198     /**
199      * Returns the smallest T value in the input.
200      */

201     static public <T extends Comparable JavaDoc<? super T>> T min(Iterable JavaDoc<? extends T> ts) {
202         return min(ts.iterator(), new Min.Comparable<T>());
203     }
204
205
206     /**
207      * Returns the smallest T value in the input, as determined by the given functor.
208      * The functor is expected to return the smaller of its two arguments.
209      */

210     static public <T> T min(Iterable JavaDoc<? extends T> ts, BinaryFunctor<T,T,T> fn) {
211         return min(ts.iterator(), fn);
212     }
213
214
215     /**
216      * Returns the smallest T value in the input, as determined by the given comparator.
217      * The input iterator is consumed.
218      */

219     static public <T> T min(Iterator JavaDoc<? extends T> ts, Comparator JavaDoc<? super T> comp) {
220         return min(ts, new Min<T>(comp));
221     }
222
223
224     /**
225      * Returns the smallest T value in the input. The input iterator is consumed.
226      */

227     static public <T extends Comparable JavaDoc<? super T>> T min(Iterator JavaDoc<? extends T> ts) {
228         return min(ts, new Min.Comparable<T>());
229     }
230
231
232     /**
233      * Returns the smallest T value in the input, as determined by the given functor.
234      * The functor is expected to return the smaller of its two arguments.
235      * The input iterator is consumed.
236      */

237     static public <T> T min(Iterator JavaDoc<? extends T> ts, BinaryFunctor<T,T,T> fn) {
238         return new Accumulate<T>(fn).fn(ts);
239     }
240
241     
242     // -------------------
243
// maximum value
244
// -------------------
245

246     
247     /**
248      * Returns the largest T value in the input, as determined by the given comparator.
249      */

250     static public <T> T max(T[] ts, Comparator JavaDoc<? super T> comp) {
251         return max(ArrayUtils.iterate(ts), new Max<T>(comp));
252     }
253
254
255     /**
256      * Returns the largest T value in the input.
257      */

258     static public <T extends Comparable JavaDoc<? super T>> T max(T[] ts) {
259         return max(ArrayUtils.iterate(ts), new Max.Comparable<T>());
260     }
261
262
263     /**
264      * Returns the largest T value in the input, as determined by the given functor.
265      * The functor is expected to return the smaller of its two arguments.
266      */

267     static public <T> T max(T[] ts, BinaryFunctor<T,T,T> fn) {
268         return max(ArrayUtils.iterate(ts), fn);
269     }
270
271
272     /**
273      * Returns the largest T value in the input, as determined by the given comparator.
274      */

275     static public <T> T max(Iterable JavaDoc<? extends T> ts, Comparator JavaDoc<? super T> comp) {
276         return max(ts.iterator(), new Max<T>(comp));
277     }
278
279
280     /**
281      * Returns the largest T value in the input.
282      */

283     static public <T extends Comparable JavaDoc<? super T>> T max(Iterable JavaDoc<? extends T> ts) {
284         return max(ts.iterator(), new Max.Comparable<T>());
285     }
286
287
288     /**
289      * Returns the largest T value in the input, as determined by the given functor.
290      * The functor is expected to return the smaller of its two arguments.
291      */

292     static public <T> T max(Iterable JavaDoc<? extends T> ts, BinaryFunctor<T,T,T> fn) {
293         return max(ts.iterator(), fn);
294     }
295
296
297     /**
298      * Returns the largest T value in the input, as determined by the given comparator.
299      * The input iterator is consumed.
300      */

301     static public <T> T max(Iterator JavaDoc<? extends T> ts, Comparator JavaDoc<? super T> comp) {
302         return max(ts, new Max<T>(comp));
303     }
304
305
306     /**
307      * Returns the largest T value in the input. The input iterator is consumed.
308      */

309     static public <T extends Comparable JavaDoc<? super T>> T max(Iterator JavaDoc<? extends T> ts) {
310         return max(ts, new Max.Comparable<T>());
311     }
312
313
314     /**
315      * Returns the largest T value in the input, as determined by the given functor.
316      * The functor is expected to return the smaller of its two arguments.
317      * The input iterator is consumed.
318      */

319     static public <T> T max(Iterator JavaDoc<? extends T> ts, BinaryFunctor<T,T,T> fn) {
320         return new Accumulate<T>(fn).fn(ts);
321     }
322
323
324     // -------------------
325
// total value
326
// -------------------
327

328
329     /**
330      * Returns the sum of the values
331      */

332     static public <T extends Number JavaDoc> T sum(T[] ts) {
333         return sum((Class JavaDoc<T>) ts.getClass().getComponentType(), ArrayUtils.iterate(ts));
334     }
335
336     /**
337      * Returns the sum of the values
338      */

339     static public <T extends Number JavaDoc> T sum(Class JavaDoc<T> type, T[] ts) {
340         return sum(type, ArrayUtils.iterate(ts));
341     }
342
343     /**
344      * Returns the sum of the values
345      */

346     static public <T extends Number JavaDoc> T sum(Class JavaDoc<T> type, Iterable JavaDoc<? extends T> ts) {
347         return sum(type, ts.iterator());
348     }
349
350
351     /**
352      * Returns the sum of the values. The iterator is entirely consumed.
353      */

354     static public <T extends Number JavaDoc> T sum(Class JavaDoc<T> type, Iterator JavaDoc<? extends T> ts) {
355         return accumulate(ts, new Plus<T>(type));
356     }
357
358
359     // -------------------
360
// accumulated value
361
// -------------------
362

363
364     static public <T> T accumulate(T[] ts, BinaryFunctor<T,T,T> fn) {
365         return accumulate(ArrayUtils.iterate(ts), fn);
366     }
367
368     
369     static public <T> T accumulate(T[] ts, T initial, BinaryFunctor<T,T,T> fn) {
370         return accumulate(ArrayUtils.iterate(ts), initial, fn);
371         
372     }
373     
374     
375     static public <T> T accumulate(Iterable JavaDoc<? extends T> values, BinaryFunctor<T,T,T> fn) {
376         return accumulate(values.iterator(), fn);
377     }
378
379     
380     static public <T> T accumulate(Iterable JavaDoc<? extends T> values, T initial, BinaryFunctor<T,T,T> fn) {
381         return accumulate(values.iterator(), initial, fn);
382     }
383     
384
385     static public <T> T accumulate(Iterator JavaDoc<? extends T> iterator, BinaryFunctor<T,T,T> fn) {
386         return new Accumulate<T>(fn).fn(iterator);
387     }
388
389     
390     static public <T> T accumulate(Iterator JavaDoc<? extends T> iterator, T initial, BinaryFunctor<T,T,T> fn) {
391         return new Accumulate<T>(initial, fn).fn(iterator);
392     }
393     
394
395     // --------------
396
// Average value
397
// --------------
398

399     /**
400      * Returns the average of the values
401      */

402     static public <T extends Number JavaDoc> T average(T[] ts) {
403         return average((Class JavaDoc<T>) ts.getClass().getComponentType(), ArrayUtils.iterate(ts));
404     }
405
406     /**
407      * Returns the average of the values
408      */

409     static public <T extends Number JavaDoc> T average(Class JavaDoc<T> type, T[] ts) {
410         return average(type, ArrayUtils.iterate(ts));
411     }
412
413     /**
414      * Returns the average of the values
415      */

416     static public <T extends Number JavaDoc> T average(Class JavaDoc<T> type, Iterable JavaDoc<T> ts) {
417         return average(type, ts.iterator());
418     }
419
420
421     /**
422      * Returns the average of the values. The iterator is entirely consumed.
423      */

424     static public <T extends Number JavaDoc> T average(Class JavaDoc<T> type, Iterator JavaDoc<T> ts) {
425         return new Average<T>(type).fn(ts);
426     }
427 }
428
Popular Tags