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 */ 017 package org.apache.commons.functor.core.composite; 018 019 import org.apache.commons.functor.Predicate; 020 import 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: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $ 036 * @author Herve Quiroz 037 * @author Rodney Waldhoff 038 */ 039 public class DoWhileProcedure extends AbstractLoopProcedure { 040 /** 041 * serialVersionUID declaration. 042 */ 043 private static final long serialVersionUID = -6064417600588553892L; 044 045 /** 046 * Create a new DoWhileProcedure. 047 * @param action to do 048 * @param condition while true 049 */ 050 public DoWhileProcedure(Procedure action, Predicate condition) { 051 super(condition, action); 052 } 053 054 /** 055 * {@inheritDoc} 056 */ 057 public final void run() { 058 do { 059 getAction().run(); 060 } while (getCondition().test()); 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 public String toString() { 067 return "DoWhileProcedure<do(" + getAction() + ") while(" + getCondition() + ")>"; 068 } 069 }