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