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.algorithm;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.functor.Predicate;
22  import org.apache.commons.functor.Procedure;
23  
24  /**
25   * Base class for predicated procedure algorithms.
26   *
27   * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
28   */
29  abstract class PredicatedLoop implements Procedure, Serializable {
30      /**
31       * serialVersionUID declaration.
32       */
33      private static final long serialVersionUID = 3221684231735316893L;
34      private final Procedure body;
35      private final Predicate test;
36  
37      /**
38       * Create a new PredicatedLoop.
39       * @param body to execute
40       * @param test whether to keep going
41       */
42      protected PredicatedLoop(Procedure body, Predicate test) {
43          this.body = body;
44          this.test = test;
45      }
46  
47      /**
48       * Get the body of this loop.
49       * @return Procedure
50       */
51      protected Procedure getBody() {
52          return body;
53      }
54  
55      /**
56       * Get the test for this loop.
57       * @return Predicate
58       */
59      protected Predicate getTest() {
60          return test;
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      public boolean equals(Object obj) {
67          if (obj == this) {
68              return true;
69          }
70          if (obj == null || !obj.getClass().equals(getClass())) {
71              return false;
72          }
73          PredicatedLoop other = (PredicatedLoop) obj;
74          return other.body.equals(body) && other.test.equals(test);
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      public int hashCode() {
81          String classname = getClass().getName();
82          int dot = classname.lastIndexOf('.');
83          int result = classname.substring(dot + 1).hashCode();
84          result <<= 2;
85          result ^= body.hashCode();
86          result <<= 2;
87          result ^= test.hashCode();
88          return result;
89      }
90  }