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 that executes a closure repeatedly until a condition is met,
  22. * like a do-while or while loop.
  23. *
  24. * @since Commons Collections 3.0
  25. * @version $Revision: 1.5 $ $Date: 2004/05/16 11:47:38 $
  26. *
  27. * @author Stephen Colebourne
  28. */
  29. public class WhileClosure implements Closure, Serializable {
  30. /** Serial version UID */
  31. static final long serialVersionUID = -3110538116913760108L;
  32. /** The test condition */
  33. private final Predicate iPredicate;
  34. /** The closure to call */
  35. private final Closure iClosure;
  36. /** The flag, true is a do loop, false is a while */
  37. private final boolean iDoLoop;
  38. /**
  39. * Factory method that performs validation.
  40. *
  41. * @param predicate the predicate used to evaluate when the loop terminates, not null
  42. * @param closure the closure the execute, not null
  43. * @param doLoop true to act as a do-while loop, always executing the closure once
  44. * @return the <code>while</code> closure
  45. * @throws IllegalArgumentException if the predicate or closure is null
  46. */
  47. public static Closure getInstance(Predicate predicate, Closure closure, boolean doLoop) {
  48. if (predicate == null) {
  49. throw new IllegalArgumentException("Predicate must not be null");
  50. }
  51. if (closure == null) {
  52. throw new IllegalArgumentException("Closure must not be null");
  53. }
  54. return new WhileClosure(predicate, closure, doLoop);
  55. }
  56. /**
  57. * Constructor that performs no validation.
  58. * Use <code>getInstance</code> if you want that.
  59. *
  60. * @param predicate the predicate used to evaluate when the loop terminates, not null
  61. * @param closure the closure the execute, not null
  62. * @param doLoop true to act as a do-while loop, always executing the closure once
  63. */
  64. public WhileClosure(Predicate predicate, Closure closure, boolean doLoop) {
  65. super();
  66. iPredicate = predicate;
  67. iClosure = closure;
  68. iDoLoop = doLoop;
  69. }
  70. /**
  71. * Executes the closure until the predicate is false.
  72. *
  73. * @param input the input object
  74. */
  75. public void execute(Object input) {
  76. if (iDoLoop) {
  77. iClosure.execute(input);
  78. }
  79. while (iPredicate.evaluate(input)) {
  80. iClosure.execute(input);
  81. }
  82. }
  83. /**
  84. * Gets the predicate in use.
  85. *
  86. * @return the predicate
  87. * @since Commons Collections 3.1
  88. */
  89. public Predicate getPredicate() {
  90. return iPredicate;
  91. }
  92. /**
  93. * Gets the closure.
  94. *
  95. * @return the closure
  96. * @since Commons Collections 3.1
  97. */
  98. public Closure getClosure() {
  99. return iClosure;
  100. }
  101. /**
  102. * Is the loop a do-while loop.
  103. *
  104. * @return true is do-while, false if while
  105. * @since Commons Collections 3.1
  106. */
  107. public boolean isDoLoop() {
  108. return iDoLoop;
  109. }
  110. }