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.io.Serializable;
  18. import java.util.Collection;
  19. import org.apache.commons.collections.Predicate;
  20. /**
  21. * Predicate implementation that returns true if all the predicates return true.
  22. *
  23. * @since Commons Collections 3.0
  24. * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $
  25. *
  26. * @author Stephen Colebourne
  27. */
  28. public final class AllPredicate implements Predicate, PredicateDecorator, Serializable {
  29. /** Serial version UID */
  30. static final long serialVersionUID = -3094696765038308799L;
  31. /** The array of predicates to call */
  32. private final Predicate[] iPredicates;
  33. /**
  34. * Factory to create the predicate.
  35. *
  36. * @param predicates the predicates to check, cloned, not null
  37. * @return the <code>all</code> predicate
  38. * @throws IllegalArgumentException if the predicates array is null
  39. * @throws IllegalArgumentException if the predicates array has less than 2 elements
  40. * @throws IllegalArgumentException if any predicate in the array is null
  41. */
  42. public static Predicate getInstance(Predicate[] predicates) {
  43. FunctorUtils.validateMin2(predicates);
  44. predicates = FunctorUtils.copy(predicates);
  45. return new AllPredicate(predicates);
  46. }
  47. /**
  48. * Factory to create the predicate.
  49. *
  50. * @param predicates the predicates to check, cloned, not null
  51. * @return the <code>all</code> predicate
  52. * @throws IllegalArgumentException if the predicates array is null
  53. * @throws IllegalArgumentException if any predicate in the array is null
  54. * @throws IllegalArgumentException if the predicates array has less than 2 elements
  55. */
  56. public static Predicate getInstance(Collection predicates) {
  57. Predicate[] preds = FunctorUtils.validate(predicates);
  58. return new AllPredicate(preds);
  59. }
  60. /**
  61. * Constructor that performs no validation.
  62. * Use <code>getInstance</code> if you want that.
  63. *
  64. * @param predicates the predicates to check, not cloned, not null
  65. */
  66. public AllPredicate(Predicate[] predicates) {
  67. super();
  68. iPredicates = predicates;
  69. }
  70. /**
  71. * Evaluates the predicate returning true if all predicates return true.
  72. *
  73. * @param object the input object
  74. * @return true if all decorated predicates return true
  75. */
  76. public boolean evaluate(Object object) {
  77. for (int i = 0; i < iPredicates.length; i++) {
  78. if (iPredicates[i].evaluate(object) == false) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * Gets the predicates, do not modify the array.
  86. *
  87. * @return the predicates
  88. * @since Commons Collections 3.1
  89. */
  90. public Predicate[] getPredicates() {
  91. return iPredicates;
  92. }
  93. }