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.composite;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.functor.BinaryPredicate;
24  
25  /**
26   * Abstract base class for {@link BinaryPredicate BinaryPredicates}
27   * composed of a list of {@link BinaryPredicate BinaryPredicates}.
28   * <p>
29   * Note that although this class implements
30   * {@link Serializable}, a given instance will
31   * only be truly <code>Serializable</code> if all the
32   * underlying functors are.  Attempts to serialize
33   * an instance whose delegates are not all
34   * <code>Serializable</code> will result in an exception.
35   * </p>
36   * @version $Revision: 1156337 $ $Date: 2011-08-10 21:44:54 +0200 (Wed, 10 Aug 2011) $
37   * @author Rodney Waldhoff
38   */
39  abstract class BaseBinaryPredicateList<L, R> implements BinaryPredicate<L, R>, Serializable {
40  
41      /**
42       * serialVersionUID declaration.
43       */
44      private static final long serialVersionUID = -8218822378485819939L;
45      // attributes
46      // ------------------------------------------------------------------------
47      private final List<BinaryPredicate<? super L, ? super R>> list =
48          new ArrayList<BinaryPredicate<? super L, ? super R>>();
49  
50      // constructor
51      // ------------------------------------------------------------------------
52      /**
53       * Create a new BaseBinaryPredicateList.
54       */
55      protected BaseBinaryPredicateList() {
56          super();
57      }
58  
59      /**
60       * Create a new BaseBinaryPredicateList instance.
61       *
62       * @param predicates to add
63       */
64      protected BaseBinaryPredicateList(BinaryPredicate<? super L, ? super R>... predicates) {
65          this();
66          if (predicates != null) {
67              for (BinaryPredicate<? super L, ? super R> p : predicates) {
68                  addBinaryPredicate(p);
69              }
70          }
71      }
72  
73      /**
74       * Create a new BaseBinaryPredicateList instance.
75       *
76       * @param predicates to add
77       */
78      protected BaseBinaryPredicateList(Iterable<BinaryPredicate<? super L, ? super R>> predicates) {
79          this();
80          if (predicates != null) {
81              for (BinaryPredicate<? super L, ? super R> p : predicates) {
82                  addBinaryPredicate(p);
83              }
84          }
85      }
86  
87      // abstract
88      // ------------------------------------------------------------------------
89      /**
90       * {@inheritDoc}
91       */
92      public abstract boolean equals(Object that);
93  
94      /**
95       * {@inheritDoc}
96       */
97      public abstract int hashCode();
98  
99      /**
100      * {@inheritDoc}
101      */
102     public abstract String toString();
103 
104     // modifiers
105     // ------------------------------------------------------------------------
106     /**
107      * Add a BinaryPredicate to the list
108      * @param p BinaryPredicate to add
109      */
110     protected void addBinaryPredicate(BinaryPredicate<? super L, ? super R> p) {
111         if (p == null) {
112             throw new IllegalArgumentException("Cannot add null BinaryPredicate");
113         }
114         list.add(p);
115     }
116 
117     // protected
118     // ------------------------------------------------------------------------
119     /**
120      * Get the "live" list of contained {@link BinaryPredicate} instances.
121      * @return List
122      */
123     protected List<BinaryPredicate<? super L, ? super R>> getBinaryPredicateList() {
124         return list;
125     }
126 
127     /**
128      * Learn whether another list is equal to this one.
129      * @param that BaseBinaryPredicateList to test
130      * @return boolean
131      */
132     protected boolean getBinaryPredicateListEquals(BaseBinaryPredicateList<?, ?> that) {
133         return (null != that && this.list.equals(that.list));
134     }
135 
136     /**
137      * Get a hashCode for the list.
138      * @return int
139      */
140     protected int getBinaryPredicateListHashCode() {
141         return list.hashCode();
142     }
143 
144     /**
145      * Get a toString for the list.
146      * @return String
147      */
148     protected String getBinaryPredicateListToString() {
149         return String.valueOf(list);
150     }
151 
152 }