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 org.apache.commons.collections.Predicate;
  19. /**
  20. * Predicate implementation that returns true if either of the predicates return true.
  21. *
  22. * @since Commons Collections 3.0
  23. * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $
  24. *
  25. * @author Stephen Colebourne
  26. */
  27. public final class OrPredicate implements Predicate, PredicateDecorator, Serializable {
  28. /** Serial version UID */
  29. static final long serialVersionUID = -8791518325735182855L;
  30. /** The array of predicates to call */
  31. private final Predicate iPredicate1;
  32. /** The array of predicates to call */
  33. private final Predicate iPredicate2;
  34. /**
  35. * Factory to create the predicate.
  36. *
  37. * @param predicate1 the first predicate to check, not null
  38. * @param predicate2 the second predicate to check, not null
  39. * @return the <code>and</code> predicate
  40. * @throws IllegalArgumentException if either predicate is null
  41. */
  42. public static Predicate getInstance(Predicate predicate1, Predicate predicate2) {
  43. if (predicate1 == null || predicate2 == null) {
  44. throw new IllegalArgumentException("Predicate must not be null");
  45. }
  46. return new OrPredicate(predicate1, predicate2);
  47. }
  48. /**
  49. * Constructor that performs no validation.
  50. * Use <code>getInstance</code> if you want that.
  51. *
  52. * @param predicate1 the first predicate to check, not null
  53. * @param predicate2 the second predicate to check, not null
  54. */
  55. public OrPredicate(Predicate predicate1, Predicate predicate2) {
  56. super();
  57. iPredicate1 = predicate1;
  58. iPredicate2 = predicate2;
  59. }
  60. /**
  61. * Evaluates the predicate returning true if either predicate returns true.
  62. *
  63. * @param object the input object
  64. * @return true if either decorated predicate returns true
  65. */
  66. public boolean evaluate(Object object) {
  67. return (iPredicate1.evaluate(object) || iPredicate2.evaluate(object));
  68. }
  69. /**
  70. * Gets the two predicates being decorated as an array.
  71. *
  72. * @return the predicates
  73. * @since Commons Collections 3.1
  74. */
  75. public Predicate[] getPredicates() {
  76. return new Predicate[] {iPredicate1, iPredicate2};
  77. }
  78. }