Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractLoopProcedure |
|
| 2.2857142857142856;2.286 |
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 | import java.io.Serializable; | |
23 | ||
24 | /** | |
25 | * Abstract base class for {@link WhileDoProcedure} and {@link DoWhileProcedure} | |
26 | * used to implement loop procedures. | |
27 | * <p> | |
28 | * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $ | |
29 | * @author Herve Quiroz | |
30 | * @author Rodney Waldhoff | |
31 | */ | |
32 | public abstract class AbstractLoopProcedure implements Procedure, Serializable { | |
33 | /** | |
34 | * serialVersionUID declaration. | |
35 | */ | |
36 | private static final long serialVersionUID = -5903381842630236070L; | |
37 | ||
38 | /** Base hash integer used to shift hash */ | |
39 | private static final int HASH_SHIFT = 4; | |
40 | ||
41 | private final Predicate condition; | |
42 | private final Procedure action; | |
43 | ||
44 | /** | |
45 | * Create a new AbstractLoopProcedure. | |
46 | * @param condition while true, repeat | |
47 | * @param action loop body | |
48 | */ | |
49 | 38 | protected AbstractLoopProcedure(Predicate condition, Procedure action) { |
50 | 38 | this.condition = condition; |
51 | 38 | this.action = action; |
52 | 38 | } |
53 | ||
54 | /** | |
55 | * {@inheritDoc} | |
56 | */ | |
57 | public final boolean equals(Object object) { | |
58 | 34 | if (object == this) { |
59 | 8 | return true; |
60 | } | |
61 | 26 | if (!(object instanceof AbstractLoopProcedure)) { |
62 | 8 | return false; |
63 | } | |
64 | 18 | AbstractLoopProcedure that = (AbstractLoopProcedure) object; |
65 | 18 | return (null == getCondition() ? null == that.getCondition() : getCondition().equals(that.getCondition())) |
66 | && (null == getAction() ? null == that.getAction() : getAction().equals(that.getAction())); | |
67 | } | |
68 | ||
69 | /** | |
70 | * {@inheritDoc} | |
71 | */ | |
72 | public int hashCode() { | |
73 | 46 | return hashCode("AbstractLoopProcedure".hashCode()); |
74 | } | |
75 | ||
76 | /** | |
77 | * {@inheritDoc} | |
78 | */ | |
79 | public String toString() { | |
80 | 8 | return getClass().getName() + "<" + getCondition() + "," + getAction() + ">"; |
81 | } | |
82 | ||
83 | /** | |
84 | * Create a hashCode by manipulating an input hashCode and factoring in instance state. | |
85 | * @param hash incoming hashCode | |
86 | * @return int | |
87 | */ | |
88 | protected int hashCode(int hash) { | |
89 | 46 | hash <<= HASH_SHIFT; |
90 | 46 | if (null != getAction()) { |
91 | 42 | hash ^= getAction().hashCode(); |
92 | } | |
93 | 46 | hash <<= HASH_SHIFT; |
94 | 46 | if (null != getCondition()) { |
95 | 42 | hash ^= getCondition().hashCode(); |
96 | } | |
97 | 46 | return hash; |
98 | } | |
99 | ||
100 | /** | |
101 | * Get the condition. | |
102 | * @return Predicate | |
103 | */ | |
104 | protected final Predicate getCondition() { | |
105 | 196 | return condition; |
106 | } | |
107 | ||
108 | /** | |
109 | * Get the action. | |
110 | * @return Procedure | |
111 | */ | |
112 | protected final Procedure getAction() { | |
113 | 190 | return action; |
114 | } | |
115 | ||
116 | } |