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.UnaryPredicate;
24
25 /**
26 * Abstract base class for {@link UnaryPredicate UnaryPredicates}
27 * composed of a list of {@link UnaryPredicate UnaryPredicates}.
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 BaseUnaryPredicateList<A> implements UnaryPredicate<A>, Serializable {
40
41 /**
42 * serialVersionUID declaration.
43 */
44 private static final long serialVersionUID = 1467575113401282954L;
45 // attributes
46 // ------------------------------------------------------------------------
47 private final List<UnaryPredicate<? super A>> list = new ArrayList<UnaryPredicate<? super A>>();
48
49 // constructor
50 // ------------------------------------------------------------------------
51 /**
52 * Create a new BaseUnaryPredicateList.
53 */
54 protected BaseUnaryPredicateList() {
55 super();
56 }
57
58 /**
59 * Create a new BaseUnaryPredicateList instance.
60 *
61 * @param predicates to add
62 */
63 protected BaseUnaryPredicateList(UnaryPredicate<? super A>... predicates) {
64 this();
65 if (predicates != null) {
66 for (UnaryPredicate<? super A> p : predicates) {
67 addUnaryPredicate(p);
68 }
69 }
70 }
71
72 /**
73 * Create a new BaseUnaryPredicateList instance.
74 *
75 * @param predicates to add
76 */
77 protected BaseUnaryPredicateList(Iterable<UnaryPredicate<? super A>> predicates) {
78 this();
79 if (predicates != null) {
80 for (UnaryPredicate<? super A> p : predicates) {
81 addUnaryPredicate(p);
82 }
83 }
84 }
85
86 // abstract
87 // ------------------------------------------------------------------------
88 /**
89 * {@inheritDoc}
90 */
91 public abstract boolean equals(Object that);
92
93 /**
94 * {@inheritDoc}
95 */
96 public abstract int hashCode();
97
98 /**
99 * {@inheritDoc}
100 */
101 public abstract String toString();
102
103 // modifiers
104 // ------------------------------------------------------------------------
105 /**
106 * Add a UnaryPredicate to the list
107 * @param p UnaryPredicate to add
108 */
109 protected void addUnaryPredicate(UnaryPredicate<? super A> p) {
110 if (p == null) {
111 throw new IllegalArgumentException("Cannot add null UnaryPredicate");
112 }
113 list.add(p);
114 }
115
116 // protected
117 // ------------------------------------------------------------------------
118
119 /**
120 * Get the "live" list of contained {@link UnaryPredicate}s.
121 * @return List
122 */
123 protected List<UnaryPredicate<? super A>> getUnaryPredicateList() {
124 return list;
125 }
126
127 /**
128 * Learn whether another BaseUnaryPredicateList has content equal to this
129 * @param that the BaseUnaryPredicateList to test
130 * @return boolean
131 */
132 protected boolean getUnaryPredicateListEquals(BaseUnaryPredicateList<?> 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 getUnaryPredicateListHashCode() {
141 return list.hashCode();
142 }
143
144 /**
145 * Get a toString for the list.
146 * @return String
147 */
148 protected String getUnaryPredicateListToString() {
149 return String.valueOf(list);
150 }
151
152 }