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 import java.io.Serializable; 023 024 /** 025 * Abstract base class for {@link WhileDoProcedure} and {@link DoWhileProcedure} 026 * used to implement loop procedures. 027 * <p> 028 * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $ 029 * @author Herve Quiroz 030 * @author Rodney Waldhoff 031 */ 032 public abstract class AbstractLoopProcedure implements Procedure, Serializable { 033 /** 034 * serialVersionUID declaration. 035 */ 036 private static final long serialVersionUID = -5903381842630236070L; 037 038 /** Base hash integer used to shift hash */ 039 private static final int HASH_SHIFT = 4; 040 041 private final Predicate condition; 042 private final Procedure action; 043 044 /** 045 * Create a new AbstractLoopProcedure. 046 * @param condition while true, repeat 047 * @param action loop body 048 */ 049 protected AbstractLoopProcedure(Predicate condition, Procedure action) { 050 this.condition = condition; 051 this.action = action; 052 } 053 054 /** 055 * {@inheritDoc} 056 */ 057 public final boolean equals(Object object) { 058 if (object == this) { 059 return true; 060 } 061 if (!(object instanceof AbstractLoopProcedure)) { 062 return false; 063 } 064 AbstractLoopProcedure that = (AbstractLoopProcedure) object; 065 return (null == getCondition() ? null == that.getCondition() : getCondition().equals(that.getCondition())) 066 && (null == getAction() ? null == that.getAction() : getAction().equals(that.getAction())); 067 } 068 069 /** 070 * {@inheritDoc} 071 */ 072 public int hashCode() { 073 return hashCode("AbstractLoopProcedure".hashCode()); 074 } 075 076 /** 077 * {@inheritDoc} 078 */ 079 public String toString() { 080 return getClass().getName() + "<" + getCondition() + "," + getAction() + ">"; 081 } 082 083 /** 084 * Create a hashCode by manipulating an input hashCode and factoring in instance state. 085 * @param hash incoming hashCode 086 * @return int 087 */ 088 protected int hashCode(int hash) { 089 hash <<= HASH_SHIFT; 090 if (null != getAction()) { 091 hash ^= getAction().hashCode(); 092 } 093 hash <<= HASH_SHIFT; 094 if (null != getCondition()) { 095 hash ^= getCondition().hashCode(); 096 } 097 return hash; 098 } 099 100 /** 101 * Get the condition. 102 * @return Predicate 103 */ 104 protected final Predicate getCondition() { 105 return condition; 106 } 107 108 /** 109 * Get the action. 110 * @return Procedure 111 */ 112 protected final Procedure getAction() { 113 return action; 114 } 115 116 }