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. import java.util.Collection;
  18. import java.util.Iterator;
  19. import org.apache.commons.collections.Closure;
  20. import org.apache.commons.collections.Predicate;
  21. import org.apache.commons.collections.Transformer;
  22. /**
  23. * Internal utilities for functors.
  24. *
  25. * @since Commons Collections 3.0
  26. * @version $Revision: 1.7 $ $Date: 2004/05/16 11:47:38 $
  27. *
  28. * @author Stephen Colebourne
  29. */
  30. class FunctorUtils {
  31. /**
  32. * Restricted constructor.
  33. */
  34. private FunctorUtils() {
  35. super();
  36. }
  37. /**
  38. * Clone the predicates to ensure that the internal reference can't be messed with.
  39. *
  40. * @param predicates the predicates to copy
  41. * @return the cloned predicates
  42. */
  43. static Predicate[] copy(Predicate[] predicates) {
  44. if (predicates == null) {
  45. return null;
  46. }
  47. return (Predicate[]) predicates.clone();
  48. }
  49. /**
  50. * Validate the predicates to ensure that all is well.
  51. *
  52. * @param predicates the predicates to validate
  53. */
  54. static void validate(Predicate[] predicates) {
  55. if (predicates == null) {
  56. throw new IllegalArgumentException("The predicate array must not be null");
  57. }
  58. for (int i = 0; i < predicates.length; i++) {
  59. if (predicates[i] == null) {
  60. throw new IllegalArgumentException("The predicate array must not contain a null predicate, index " + i + " was null");
  61. }
  62. }
  63. }
  64. /**
  65. * Validate the predicates to ensure that all is well.
  66. *
  67. * @param predicates the predicates to validate
  68. */
  69. static void validateMin2(Predicate[] predicates) {
  70. if (predicates == null) {
  71. throw new IllegalArgumentException("The predicate array must not be null");
  72. }
  73. if (predicates.length < 2) {
  74. throw new IllegalArgumentException(
  75. "At least 2 predicates must be specified in the predicate array, size was " + predicates.length);
  76. }
  77. for (int i = 0; i < predicates.length; i++) {
  78. if (predicates[i] == null) {
  79. throw new IllegalArgumentException("The predicate array must not contain a null predicate, index " + i + " was null");
  80. }
  81. }
  82. }
  83. /**
  84. * Validate the predicates to ensure that all is well.
  85. *
  86. * @param predicates the predicates to validate
  87. * @return predicate array
  88. */
  89. static Predicate[] validate(Collection predicates) {
  90. if (predicates == null) {
  91. throw new IllegalArgumentException("The predicate collection must not be null");
  92. }
  93. if (predicates.size() < 2) {
  94. throw new IllegalArgumentException(
  95. "At least 2 predicates must be specified in the predicate collection, size was " + predicates.size());
  96. }
  97. // convert to array like this to guarantee iterator() ordering
  98. Predicate[] preds = new Predicate[predicates.size()];
  99. int i = 0;
  100. for (Iterator it = predicates.iterator(); it.hasNext();) {
  101. preds[i] = (Predicate) it.next();
  102. if (preds[i] == null) {
  103. throw new IllegalArgumentException("The predicate collection must not contain a null predicate, index " + i + " was null");
  104. }
  105. i++;
  106. }
  107. return preds;
  108. }
  109. /**
  110. * Clone the closures to ensure that the internal reference can't be messed with.
  111. *
  112. * @param closures the closures to copy
  113. * @return the cloned closures
  114. */
  115. static Closure[] copy(Closure[] closures) {
  116. if (closures == null) {
  117. return null;
  118. }
  119. return (Closure[]) closures.clone();
  120. }
  121. /**
  122. * Validate the closures to ensure that all is well.
  123. *
  124. * @param closures the closures to validate
  125. */
  126. static void validate(Closure[] closures) {
  127. if (closures == null) {
  128. throw new IllegalArgumentException("The closure array must not be null");
  129. }
  130. for (int i = 0; i < closures.length; i++) {
  131. if (closures[i] == null) {
  132. throw new IllegalArgumentException("The closure array must not contain a null closure, index " + i + " was null");
  133. }
  134. }
  135. }
  136. /**
  137. * Copy method
  138. *
  139. * @param transformers the transformers to copy
  140. * @return a clone of the transformers
  141. */
  142. static Transformer[] copy(Transformer[] transformers) {
  143. if (transformers == null) {
  144. return null;
  145. }
  146. return (Transformer[]) transformers.clone();
  147. }
  148. /**
  149. * Validate method
  150. *
  151. * @param transformers the transformers to validate
  152. */
  153. static void validate(Transformer[] transformers) {
  154. if (transformers == null) {
  155. throw new IllegalArgumentException("The transformer array must not be null");
  156. }
  157. for (int i = 0; i < transformers.length; i++) {
  158. if (transformers[i] == null) {
  159. throw new IllegalArgumentException(
  160. "The transformer array must not contain a null transformer, index " + i + " was null");
  161. }
  162. }
  163. }
  164. }