KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > functors > FunctorUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.functors;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.commons.collections.Closure;
22 import org.apache.commons.collections.Predicate;
23 import org.apache.commons.collections.Transformer;
24
25 /**
26  * Internal utilities for functors.
27  *
28  * @since Commons Collections 3.0
29  * @version $Revision: 1.7 $ $Date: 2004/05/16 11:47:38 $
30  *
31  * @author Stephen Colebourne
32  */

33 class FunctorUtils {
34     
35     /**
36      * Restricted constructor.
37      */

38     private FunctorUtils() {
39         super();
40     }
41     
42     /**
43      * Clone the predicates to ensure that the internal reference can't be messed with.
44      *
45      * @param predicates the predicates to copy
46      * @return the cloned predicates
47      */

48     static Predicate[] copy(Predicate[] predicates) {
49         if (predicates == null) {
50             return null;
51         }
52         return (Predicate[]) predicates.clone();
53     }
54     
55     /**
56      * Validate the predicates to ensure that all is well.
57      *
58      * @param predicates the predicates to validate
59      */

60     static void validate(Predicate[] predicates) {
61         if (predicates == null) {
62             throw new IllegalArgumentException JavaDoc("The predicate array must not be null");
63         }
64         for (int i = 0; i < predicates.length; i++) {
65             if (predicates[i] == null) {
66                 throw new IllegalArgumentException JavaDoc("The predicate array must not contain a null predicate, index " + i + " was null");
67             }
68         }
69     }
70     
71     /**
72      * Validate the predicates to ensure that all is well.
73      *
74      * @param predicates the predicates to validate
75      */

76     static void validateMin2(Predicate[] predicates) {
77         if (predicates == null) {
78             throw new IllegalArgumentException JavaDoc("The predicate array must not be null");
79         }
80         if (predicates.length < 2) {
81             throw new IllegalArgumentException JavaDoc(
82                 "At least 2 predicates must be specified in the predicate array, size was " + predicates.length);
83         }
84         for (int i = 0; i < predicates.length; i++) {
85             if (predicates[i] == null) {
86                 throw new IllegalArgumentException JavaDoc("The predicate array must not contain a null predicate, index " + i + " was null");
87             }
88         }
89     }
90
91     /**
92      * Validate the predicates to ensure that all is well.
93      *
94      * @param predicates the predicates to validate
95      * @return predicate array
96      */

97     static Predicate[] validate(Collection JavaDoc predicates) {
98         if (predicates == null) {
99             throw new IllegalArgumentException JavaDoc("The predicate collection must not be null");
100         }
101         if (predicates.size() < 2) {
102             throw new IllegalArgumentException JavaDoc(
103                 "At least 2 predicates must be specified in the predicate collection, size was " + predicates.size());
104         }
105         // convert to array like this to guarantee iterator() ordering
106
Predicate[] preds = new Predicate[predicates.size()];
107         int i = 0;
108         for (Iterator JavaDoc it = predicates.iterator(); it.hasNext();) {
109             preds[i] = (Predicate) it.next();
110             if (preds[i] == null) {
111                 throw new IllegalArgumentException JavaDoc("The predicate collection must not contain a null predicate, index " + i + " was null");
112             }
113             i++;
114         }
115         return preds;
116     }
117     
118     /**
119      * Clone the closures to ensure that the internal reference can't be messed with.
120      *
121      * @param closures the closures to copy
122      * @return the cloned closures
123      */

124     static Closure[] copy(Closure[] closures) {
125         if (closures == null) {
126             return null;
127         }
128         return (Closure[]) closures.clone();
129     }
130     
131     /**
132      * Validate the closures to ensure that all is well.
133      *
134      * @param closures the closures to validate
135      */

136     static void validate(Closure[] closures) {
137         if (closures == null) {
138             throw new IllegalArgumentException JavaDoc("The closure array must not be null");
139         }
140         for (int i = 0; i < closures.length; i++) {
141             if (closures[i] == null) {
142                 throw new IllegalArgumentException JavaDoc("The closure array must not contain a null closure, index " + i + " was null");
143             }
144         }
145     }
146
147     /**
148      * Copy method
149      *
150      * @param transformers the transformers to copy
151      * @return a clone of the transformers
152      */

153     static Transformer[] copy(Transformer[] transformers) {
154         if (transformers == null) {
155             return null;
156         }
157         return (Transformer[]) transformers.clone();
158     }
159     
160     /**
161      * Validate method
162      *
163      * @param transformers the transformers to validate
164      */

165     static void validate(Transformer[] transformers) {
166         if (transformers == null) {
167             throw new IllegalArgumentException JavaDoc("The transformer array must not be null");
168         }
169         for (int i = 0; i < transformers.length; i++) {
170             if (transformers[i] == null) {
171                 throw new IllegalArgumentException JavaDoc(
172                     "The transformer array must not contain a null transformer, index " + i + " was null");
173             }
174         }
175     }
176
177 }
178
Popular Tags