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.Predicate;
24  
25  /**
26   * Abstract base class for {@link Predicate Predicates}
27   * composed of a list of {@link Predicate Predicates}.
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: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
37   * @author Rodney Waldhoff
38   */
39  abstract class BasePredicateList implements Predicate, Serializable {
40      /**
41       * serialVersionUID declaration.
42       */
43      private static final long serialVersionUID = 7860902316994888181L;
44      // attributes
45      // ------------------------------------------------------------------------
46      private final List<Predicate> list = new ArrayList<Predicate>();
47  
48      // constructor
49      // ------------------------------------------------------------------------
50      /**
51       * Create a new BasePredicateList instance.
52       */
53      protected BasePredicateList() {
54          super();
55      }
56  
57      /**
58       * Create a new BasePredicateList instance.
59       *
60       * @param predicates to add
61       */
62      protected BasePredicateList(Predicate... predicates) {
63          this();
64          if (predicates != null) {
65              for (Predicate p : predicates) {
66                  addPredicate(p);
67              }
68          }
69      }
70  
71      /**
72       * Create a new BasePredicateList instance.
73       *
74       * @param predicates to add
75       */
76      protected BasePredicateList(Iterable<Predicate> predicates) {
77          this();
78          if (predicates != null) {
79              for (Predicate p : predicates) {
80                  addPredicate(p);
81              }
82          }
83      }
84  
85      // abstract
86      // ------------------------------------------------------------------------
87      /**
88       * {@inheritDoc}
89       */
90      public abstract boolean equals(Object that);
91  
92      /**
93       * {@inheritDoc}
94       */
95      public abstract int hashCode();
96  
97      /**
98       * {@inheritDoc}
99       */
100     public abstract String toString();
101 
102     // modifiers
103     // ------------------------------------------------------------------------
104     /**
105      * Add a Predicate to the list.
106      * @param p Predicate to add
107      */
108     protected void addPredicate(Predicate p) {
109         if (p == null) {
110             throw new IllegalArgumentException("Cannot add null Predicate");
111         }
112         list.add(p);
113     }
114 
115     // protected
116     // ------------------------------------------------------------------------
117 
118     /**
119      * Get the "live" list of {@link Predicate}s.
120      * @return List<Predicate>
121      */
122     protected List<Predicate> getPredicateList() {
123         return list;
124     }
125 
126     /**
127      * Learn whether the list of another BasePredicateList is equal to my list.
128      * @param that BasePredicateList to test
129      * @return boolean
130      */
131     protected boolean getPredicateListEquals(BasePredicateList that) {
132         return (null != that && this.list.equals(that.list));
133     }
134 
135     /**
136      * Get a hashCode for my list.
137      * @return int
138      */
139     protected int getPredicateListHashCode() {
140         return list.hashCode();
141     }
142 
143     /**
144      * Get a toString for my list.
145      * @return String
146      */
147     protected String getPredicateListToString() {
148         return String.valueOf(list);
149     }
150 
151 }