1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.policy.bean.basic; |
11 | |
|
12 | |
import java.util.ArrayList; |
13 | |
import java.util.Collection; |
14 | |
import java.util.Iterator; |
15 | |
import java.util.List; |
16 | |
|
17 | |
import org.openpermis.policy.PartProblemReporter; |
18 | |
import org.openpermis.policy.PartProblemReporter.ProblemMessage; |
19 | |
import org.openpermis.policy.bean.BeanCollection; |
20 | |
import org.openpermis.policy.bean.PartBean; |
21 | |
import org.openpermis.policy.bean.SerialNumber; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public abstract class BasicAbstractCollection<P extends PartBean> |
30 | |
extends BasicPartBean |
31 | |
implements BeanCollection<P> |
32 | |
{ |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
private static final long serialVersionUID = -5284544211072597574L; |
39 | |
|
40 | |
|
41 | |
|
42 | |
private List<P> collection; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
protected BasicAbstractCollection (SerialNumber serialNumber) { |
50 | 6022 | super(BeanCollection.class, serialNumber); |
51 | 6022 | this.collection = new ArrayList<P>(); |
52 | 6022 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
protected BasicAbstractCollection (SerialNumber serialNumber, Collection<P> collection) { |
58 | 3488 | this(serialNumber); |
59 | 3488 | if (collection != null) { |
60 | 2980 | for (P p : collection) { |
61 | 2817 | if (p == null) { |
62 | 4 | throw new IllegalArgumentException("Collection contains null entries."); |
63 | |
} |
64 | |
} |
65 | 2976 | this.collection.addAll(collection); |
66 | |
} |
67 | 3484 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
protected Collection<P> getCollection () { |
77 | 3 | return this.collection; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public Iterator<P> iterator () { |
86 | 4018 | return this.collection.iterator(); |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public List<P> toList () { |
93 | 5372 | final List<P> result = new ArrayList<P>(this.collection.size()); |
94 | 5372 | for (P partBean : this.collection) { |
95 | 4999 | result.add(partBean); |
96 | |
} |
97 | 5372 | return result; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
protected abstract boolean isCollectionType (BasicPart part); |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
protected boolean comparablePart (BasicPart part) { |
114 | 8668 | return isCollectionType(part); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
@Override |
121 | |
@SuppressWarnings("unchecked") |
122 | |
protected boolean equalPart (BasicPart part) { |
123 | 4334 | final List<P> list = ((BasicAbstractCollection) part).toList(); |
124 | |
|
125 | 4334 | for (P element : this.collection) { |
126 | 199 | if (list.contains(element)) { |
127 | 185 | list.remove(element); |
128 | |
} else { |
129 | 14 | return false; |
130 | |
} |
131 | |
} |
132 | 4320 | if (list.isEmpty()) { |
133 | 944 | return true; |
134 | |
} |
135 | 3376 | return false; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
@Override |
142 | |
protected int partHashCode () { |
143 | 104 | int hash = 0; |
144 | 104 | final Iterator<?> i = iterator(); |
145 | 200 | while (i.hasNext()) { |
146 | 96 | final Object obj = i.next(); |
147 | 96 | if (obj != null) { |
148 | 96 | hash += obj.hashCode(); |
149 | |
} |
150 | 96 | } |
151 | 104 | return hash; |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
@Override |
158 | |
protected void appendPartDetails (StringBuilder sb) { |
159 | 0 | appendDetails(sb, "size", Integer.valueOf(this.collection.size())); |
160 | 0 | final StringBuilder serials = new StringBuilder(); |
161 | 0 | for (P partBean : this.collection) { |
162 | 0 | if (serials.length() > 0) { |
163 | 0 | serials.append(", "); |
164 | |
} |
165 | 0 | serials.append(partBean.getSerialNumber()); |
166 | |
} |
167 | 0 | appendDetails(sb, "serials", serials.toString()); |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
@Override |
174 | |
public PartBean findBySerialNumber (SerialNumber partSerialNumber) { |
175 | 0 | final PartBean superBean = super.findBySerialNumber(partSerialNumber); |
176 | 0 | if (superBean != null) { |
177 | 0 | return superBean; |
178 | |
} |
179 | 0 | for (P part : this.collection) { |
180 | 0 | if (part != null && partSerialNumber.equals(part.getSerialNumber())) { |
181 | 0 | return part; |
182 | |
} |
183 | |
} |
184 | 0 | return null; |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
@Override |
191 | |
public boolean isPartValid (PartProblemReporter reporter) { |
192 | 0 | return isPartValid(reporter, false, true, false, false, false); |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
private boolean isPartValid ( |
205 | |
PartProblemReporter reporter, |
206 | |
boolean checkNotEmpty, |
207 | |
boolean checkValid, |
208 | |
boolean checkNameUniqueness, |
209 | |
boolean checkObjectUniqueness, |
210 | |
boolean checkIdentityUniqueness) |
211 | |
{ |
212 | |
boolean valid; |
213 | 0 | valid = isChildCollectionValid( |
214 | |
reporter, |
215 | |
this.collection, |
216 | |
checkNotEmpty, |
217 | |
checkValid, |
218 | |
checkNameUniqueness, |
219 | |
checkObjectUniqueness, |
220 | |
checkIdentityUniqueness |
221 | |
); |
222 | 0 | if (!valid) { |
223 | 0 | reportProblem(reporter, ProblemMessage.illegalCollection); |
224 | |
} |
225 | 0 | return valid; |
226 | |
} |
227 | |
|
228 | |
} |