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.Closure;
  19. import org.apache.commons.collections.Predicate;
  20. /**
  21. * Closure implementation acts as an if statement calling one or other closure
  22. * based on a predicate.
  23. *
  24. * @since Commons Collections 3.0
  25. * @version $Revision: 1.6 $ $Date: 2004/05/16 11:47:38 $
  26. *
  27. * @author Stephen Colebourne
  28. */
  29. public class IfClosure implements Closure, Serializable {
  30. /** Serial version UID */
  31. static final long serialVersionUID = 3518477308466486130L;
  32. /** The test */
  33. private final Predicate iPredicate;
  34. /** The closure to use if true */
  35. private final Closure iTrueClosure;
  36. /** The closure to use if false */
  37. private final Closure iFalseClosure;
  38. /**
  39. * Factory method that performs validation.
  40. *
  41. * @param predicate predicate to switch on
  42. * @param trueClosure closure used if true
  43. * @param falseClosure closure used if false
  44. * @return the <code>if</code> closure
  45. * @throws IllegalArgumentException if any argument is null
  46. */
  47. public static Closure getInstance(Predicate predicate, Closure trueClosure, Closure falseClosure) {
  48. if (predicate == null) {
  49. throw new IllegalArgumentException("Predicate must not be null");
  50. }
  51. if (trueClosure == null || falseClosure == null) {
  52. throw new IllegalArgumentException("Closures must not be null");
  53. }
  54. return new IfClosure(predicate, trueClosure, falseClosure);
  55. }
  56. /**
  57. * Constructor that performs no validation.
  58. * Use <code>getInstance</code> if you want that.
  59. *
  60. * @param predicate predicate to switch on, not null
  61. * @param trueClosure closure used if true, not null
  62. * @param falseClosure closure used if false, not null
  63. */
  64. public IfClosure(Predicate predicate, Closure trueClosure, Closure falseClosure) {
  65. super();
  66. iPredicate = predicate;
  67. iTrueClosure = trueClosure;
  68. iFalseClosure = falseClosure;
  69. }
  70. /**
  71. * Executes the true or false closure accoring to the result of the predicate.
  72. *
  73. * @param input the input object
  74. */
  75. public void execute(Object input) {
  76. if (iPredicate.evaluate(input) == true) {
  77. iTrueClosure.execute(input);
  78. } else {
  79. iFalseClosure.execute(input);
  80. }
  81. }
  82. /**
  83. * Gets the predicate.
  84. *
  85. * @return the predicate
  86. * @since Commons Collections 3.1
  87. */
  88. public Predicate getPredicate() {
  89. return iPredicate;
  90. }
  91. /**
  92. * Gets the closure called when true.
  93. *
  94. * @return the closure
  95. * @since Commons Collections 3.1
  96. */
  97. public Closure getTrueClosure() {
  98. return iTrueClosure;
  99. }
  100. /**
  101. * Gets the closure called when false.
  102. *
  103. * @return the closure
  104. * @since Commons Collections 3.1
  105. */
  106. public Closure getFalseClosure() {
  107. return iFalseClosure;
  108. }
  109. }