001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.functor.core.composite;
018
019import org.apache.commons.functor.Predicate;
020import org.apache.commons.functor.Procedure;
021
022
023/**
024 * A {@link Procedure} implementation of a while loop. Given a {@link Predicate}
025 * <i>c</i> and an {@link Procedure} <i>p</i>, {@link #run runs}
026 * <code>do { p.run(); } while(c.test())</code>.
027 * <p>
028 * Note that although this class implements
029 * {@link java.io.Serializable}, a given instance will
030 * only be truly <code>Serializable</code> if all the
031 * underlying functors are.  Attempts to serialize
032 * an instance whose delegates are not all
033 * <code>Serializable</code> will result in an exception.
034 * </p>
035 * @version $Revision: 1345136 $ $Date: 2012-06-01 08:47:06 -0400 (Fri, 01 Jun 2012) $
036 */
037public class DoWhileProcedure extends AbstractLoopProcedure {
038    /**
039     * serialVersionUID declaration.
040     */
041    private static final long serialVersionUID = -6064417600588553892L;
042
043    /**
044     * Create a new DoWhileProcedure.
045     * @param action to do
046     * @param condition while true
047     */
048    public DoWhileProcedure(Procedure action, Predicate condition) {
049        super(condition, action);
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    public final void run() {
056        do {
057            getAction().run();
058        } while (getCondition().test());
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public String toString() {
066        return "DoWhileProcedure<do(" + getAction() + ") while(" + getCondition() + ")>";
067    }
068}