01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.util;
28:
29: import java.util.Collection;
30: import java.util.Iterator;
31: import java.util.List;
32:
33: /**
34: * Collectors is a class of static methods for applying a Thunk
35: * to various sorts of collection-like data structures.
36: */
37:
38: public final class Collectors {
39:
40: /** apply the Thunk to each element of the collection. **/
41: public static void apply(Thunk t, Collection c) {
42: // If the Collection happens to be a List (List value stored in
43: // Collection variable), use the more effient form of this method.
44: if (c instanceof List) {
45: apply(t, (List) c);
46: } else {
47: // Otherwise we need to iterate through the collection
48: for (Iterator i = c.iterator(); i.hasNext();) {
49: t.apply(i.next());
50: }
51: }
52: }
53:
54: /** apply the Thunk to each element of the list. **/
55: public static void apply(Thunk t, List l) {
56: // optimize for List
57: int listSize = l.size();
58: for (int index = 0; index < listSize; index++) {
59: t.apply(l.get(index));
60: }
61: }
62:
63: /** apply the Thunk to each element from an Iterator. **/
64: public static void apply(Thunk t, Iterator i) {
65: while (i.hasNext()) {
66: t.apply(i.next());
67: }
68: }
69:
70: /** apply the Thunk to each element of the Array **/
71: public static void apply(Thunk t, Object[] a) {
72: int l = a.length;
73: for (int i = 0; i < l; i++) {
74: t.apply(a[i]);
75: }
76: }
77:
78: /** apply the Thunk to each element of the Array.
79: * Thunk will be applied to the first length elements.
80: **/
81: public static void apply(Thunk t, Object[] a, int length) {
82: for (int i = 0; i < length; i++) {
83: t.apply(a[i]);
84: }
85: }
86:
87: /** apply the Thunk to each element of the Array
88: * t will be applied to the length elements starting at position start.
89: **/
90: public static void apply(Thunk t, Object[] a, int start, int length) {
91: int i = 0;
92: int j = start;
93: while (i < length) {
94: t.apply(a[j]);
95: i++;
96: j++;
97: }
98: }
99: }
|