View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.core.composite;
18  
19  import org.apache.commons.functor.Predicate;
20  import org.apache.commons.functor.Procedure;
21  
22  
23  /**
24   * A {@link Procedure} implementation of a while loop. Given a {@link Predicate}
25   * <i>c</i> and an {@link Procedure} <i>p</i>, {@link #run runs}
26   * <code>while(c.test()) { p.run(); }</code>.
27   * <p>
28   * Note that although this class implements
29   * {@link java.io.Serializable}, a given instance will
30   * only be truly <code>Serializable</code> if all the
31   * underlying functors are.  Attempts to serialize
32   * an instance whose delegates are not all
33   * <code>Serializable</code> will result in an exception.
34   * </p>
35   * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
36   * @author Herve Quiroz
37   * @author Rodney Waldhoff
38   */
39  public class WhileDoProcedure extends AbstractLoopProcedure {
40      /**
41       * serialVersionUID declaration.
42       */
43      private static final long serialVersionUID = 8157389586531784657L;
44  
45      /**
46       * Create a new WhileDoProcedure.
47       * @param condition while
48       * @param action to do
49       */
50      public WhileDoProcedure(Predicate condition, Procedure action) {
51          super(condition, action);
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      public void run() {
58          while (getCondition().test()) {
59              getAction().run();
60          }
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      public String toString() {
67          return "WhileDoProcedure<while(" + getCondition() + ") do(" + getAction() + ")>";
68      }
69  }