001: /*
002: * Copyright 2001-2004,2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.functors;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020:
021: import org.apache.commons.collections.Closure;
022: import org.apache.commons.collections.Predicate;
023: import org.apache.commons.collections.Transformer;
024:
025: /**
026: * Internal utilities for functors.
027: *
028: * @since Commons Collections 3.0
029: * @version $Revision: 377508 $ $Date: 2006-02-13 22:12:33 +0000 (Mon, 13 Feb 2006) $
030: *
031: * @author Stephen Colebourne
032: * @author Matt Benson
033: */
034: class FunctorUtils {
035:
036: /**
037: * Restricted constructor.
038: */
039: private FunctorUtils() {
040: super ();
041: }
042:
043: /**
044: * Clone the predicates to ensure that the internal reference can't be messed with.
045: *
046: * @param predicates the predicates to copy
047: * @return the cloned predicates
048: */
049: static Predicate[] copy(Predicate[] predicates) {
050: if (predicates == null) {
051: return null;
052: }
053: return (Predicate[]) predicates.clone();
054: }
055:
056: /**
057: * Validate the predicates to ensure that all is well.
058: *
059: * @param predicates the predicates to validate
060: */
061: static void validate(Predicate[] predicates) {
062: if (predicates == null) {
063: throw new IllegalArgumentException(
064: "The predicate array must not be null");
065: }
066: for (int i = 0; i < predicates.length; i++) {
067: if (predicates[i] == null) {
068: throw new IllegalArgumentException(
069: "The predicate array must not contain a null predicate, index "
070: + i + " was null");
071: }
072: }
073: }
074:
075: /**
076: * Validate the predicates to ensure that all is well.
077: *
078: * @param predicates the predicates to validate
079: * @return predicate array
080: */
081: static Predicate[] validate(Collection predicates) {
082: if (predicates == null) {
083: throw new IllegalArgumentException(
084: "The predicate collection must not be null");
085: }
086: // convert to array like this to guarantee iterator() ordering
087: Predicate[] preds = new Predicate[predicates.size()];
088: int i = 0;
089: for (Iterator it = predicates.iterator(); it.hasNext();) {
090: preds[i] = (Predicate) it.next();
091: if (preds[i] == null) {
092: throw new IllegalArgumentException(
093: "The predicate collection must not contain a null predicate, index "
094: + i + " was null");
095: }
096: i++;
097: }
098: return preds;
099: }
100:
101: /**
102: * Clone the closures to ensure that the internal reference can't be messed with.
103: *
104: * @param closures the closures to copy
105: * @return the cloned closures
106: */
107: static Closure[] copy(Closure[] closures) {
108: if (closures == null) {
109: return null;
110: }
111: return (Closure[]) closures.clone();
112: }
113:
114: /**
115: * Validate the closures to ensure that all is well.
116: *
117: * @param closures the closures to validate
118: */
119: static void validate(Closure[] closures) {
120: if (closures == null) {
121: throw new IllegalArgumentException(
122: "The closure array must not be null");
123: }
124: for (int i = 0; i < closures.length; i++) {
125: if (closures[i] == null) {
126: throw new IllegalArgumentException(
127: "The closure array must not contain a null closure, index "
128: + i + " was null");
129: }
130: }
131: }
132:
133: /**
134: * Copy method
135: *
136: * @param transformers the transformers to copy
137: * @return a clone of the transformers
138: */
139: static Transformer[] copy(Transformer[] transformers) {
140: if (transformers == null) {
141: return null;
142: }
143: return (Transformer[]) transformers.clone();
144: }
145:
146: /**
147: * Validate method
148: *
149: * @param transformers the transformers to validate
150: */
151: static void validate(Transformer[] transformers) {
152: if (transformers == null) {
153: throw new IllegalArgumentException(
154: "The transformer array must not be null");
155: }
156: for (int i = 0; i < transformers.length; i++) {
157: if (transformers[i] == null) {
158: throw new IllegalArgumentException(
159: "The transformer array must not contain a null transformer, index "
160: + i + " was null");
161: }
162: }
163: }
164:
165: }
|