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 only one of 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 OnePredicate implements Predicate, PredicateDecorator, Serializable {
  29. /** Serial version UID */
  30. static final long serialVersionUID = -8125389089924745785L;
  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>any</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 OnePredicate(predicates);
  46. }
  47. /**
  48. * Factory to create the predicate.
  49. *
  50. * @param predicates the predicates to check, cloned, not null
  51. * @return the <code>one</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 OnePredicate(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 OnePredicate(Predicate[] predicates) {
  67. super();
  68. iPredicates = predicates;
  69. }
  70. /**
  71. * Evaluates the predicate returning true if only one decorated predicate
  72. * returns true.
  73. *
  74. * @param object the input object
  75. * @return true if only one decorated predicate returns true
  76. */
  77. public boolean evaluate(Object object) {
  78. boolean match = false;
  79. for (int i = 0; i < iPredicates.length; i++) {
  80. if (iPredicates[i].evaluate(object)) {
  81. if (match) {
  82. return false;
  83. }
  84. match = true;
  85. }
  86. }
  87. return match;
  88. }
  89. /**
  90. * Gets the predicates, do not modify the array.
  91. *
  92. * @return the predicates
  93. * @since Commons Collections 3.1
  94. */
  95. public Predicate[] getPredicates() {
  96. return iPredicates;
  97. }
  98. }