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. /**
  20. * Closure implementation that calls another closure n times, like a for loop.
  21. *
  22. * @since Commons Collections 3.0
  23. * @version $Revision: 1.5 $ $Date: 2004/05/16 11:47:38 $
  24. *
  25. * @author Stephen Colebourne
  26. */
  27. public class ForClosure implements Closure, Serializable {
  28. /** Serial version UID */
  29. static final long serialVersionUID = -1190120533393621674L;
  30. /** The number of times to loop */
  31. private final int iCount;
  32. /** The closure to call */
  33. private final Closure iClosure;
  34. /**
  35. * Factory method that performs validation.
  36. * <p>
  37. * A null closure or zero count returns the <code>NOPClosure</code>.
  38. * A count of one returns the specified closure.
  39. *
  40. * @param count the number of times to execute the closure
  41. * @param closure the closure to execute, not null
  42. * @return the <code>for</code> closure
  43. */
  44. public static Closure getInstance(int count, Closure closure) {
  45. if (count <= 0 || closure == null) {
  46. return NOPClosure.INSTANCE;
  47. }
  48. if (count == 1) {
  49. return closure;
  50. }
  51. return new ForClosure(count, closure);
  52. }
  53. /**
  54. * Constructor that performs no validation.
  55. * Use <code>getInstance</code> if you want that.
  56. *
  57. * @param count the number of times to execute the closure
  58. * @param closure the closure to execute, not null
  59. */
  60. public ForClosure(int count, Closure closure) {
  61. super();
  62. iCount = count;
  63. iClosure = closure;
  64. }
  65. /**
  66. * Executes the closure <code>count</code> times.
  67. *
  68. * @param input the input object
  69. */
  70. public void execute(Object input) {
  71. for (int i = 0; i < iCount; i++) {
  72. iClosure.execute(input);
  73. }
  74. }
  75. /**
  76. * Gets the closure.
  77. *
  78. * @return the closure
  79. * @since Commons Collections 3.1
  80. */
  81. public Closure getClosure() {
  82. return iClosure;
  83. }
  84. /**
  85. * Gets the count.
  86. *
  87. * @return the count
  88. * @since Commons Collections 3.1
  89. */
  90. public int getCount() {
  91. return iCount;
  92. }
  93. }