1 | 112 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.flatfile; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collection; |
21 | |
import java.util.Collections; |
22 | |
import java.util.List; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 224 | public class EntityArray extends EntityCollectionSupport implements |
30 | |
IndexedEntityCollection { |
31 | |
private static final long serialVersionUID = 8716199462287161060L; |
32 | |
|
33 | 228 | private int size = -1; |
34 | 228 | private int minimumSize = 0; |
35 | 228 | private int maximumSize = Integer.MAX_VALUE; |
36 | 228 | private boolean resizable = false; |
37 | |
private List<Entity> children; |
38 | |
private Entity prototype; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public EntityArray() { |
44 | |
|
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public EntityArray(int size) { |
52 | 0 | setSize(size); |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 228 | public EntityArray(Entity prototype) { |
60 | 228 | setPrototype(prototype); |
61 | 228 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public EntityArray(Entity prototype, int size) { |
69 | 0 | this(prototype); |
70 | 0 | setSize(size); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public Entity getChild(int index) { |
77 | 294 | initialize(); |
78 | 294 | return (Entity) children.get(index); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public synchronized Collection<Entity> getChildren() { |
85 | 690 | initialize(); |
86 | 684 | return Collections.unmodifiableCollection(children); |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public synchronized Entity getPrototype() { |
94 | 72 | return prototype; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public synchronized void setPrototype(Entity prototype) { |
102 | 228 | if (prototype == null) { |
103 | 0 | throw new IllegalArgumentException("prototype Entity was null"); |
104 | |
} |
105 | 228 | this.prototype = prototype; |
106 | 228 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public synchronized void setSize(int size) { |
113 | 276 | if (this.size >= 0 && !isResizable()) { |
114 | 12 | if (size == this.size()) { |
115 | 6 | return; |
116 | |
} |
117 | 6 | throw new IllegalStateException("size already set"); |
118 | |
} |
119 | 264 | if (size < minimumSize || size > maximumSize) { |
120 | 24 | throw new IllegalArgumentException("illegal collection size: " |
121 | 6 | + size + "; should be <= " + maximumSize + " and >= " |
122 | 6 | + minimumSize); |
123 | |
} |
124 | 246 | this.size = size; |
125 | 246 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public synchronized int size() { |
131 | 732 | return size; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public synchronized boolean isSizable() { |
138 | 30 | return size == -1 || isResizable(); |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
@Override |
146 | |
public synchronized EntityArray clone() { |
147 | 336 | EntityArray result = (EntityArray) super.clone(); |
148 | 336 | if (children != null) { |
149 | 96 | result.children = new ArrayList<Entity>(); |
150 | 826 | for (Entity e : children) { |
151 | 2094 | result.children.add(e.clone()); |
152 | 1396 | } |
153 | |
} |
154 | 336 | return result; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
private synchronized void initialize() { |
161 | 984 | if (children != null) { |
162 | 726 | return; |
163 | |
} |
164 | 258 | if (size < 0) { |
165 | 6 | throw new IllegalStateException("EntityArray size not set"); |
166 | |
} |
167 | 252 | if (size == 0) { |
168 | 24 | children = Collections.<Entity>emptyList(); |
169 | 24 | return; |
170 | |
} |
171 | 228 | if (prototype == null) { |
172 | 0 | throw new IllegalStateException("Prototype child entity not set"); |
173 | |
} |
174 | 228 | children = new ArrayList<Entity>(size); |
175 | 228 | adjustSize(); |
176 | 228 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
private synchronized void adjustSize() { |
182 | 228 | if (children.size() > size) { |
183 | 0 | children.subList(size, children.size()).clear(); |
184 | 0 | return; |
185 | |
} |
186 | 2160 | for (int i = children.size(); i < size; i++) { |
187 | 1932 | children.add(prototype.clone()); |
188 | |
} |
189 | 228 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public synchronized int getMaximumSize() { |
196 | 30 | return maximumSize; |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public synchronized void setMaximumSize(int maximumSize) { |
204 | 18 | if (maximumSize > Integer.MAX_VALUE || maximumSize < getMinimumSize()) { |
205 | 0 | throw new IllegalArgumentException(Integer.toString(maximumSize)); |
206 | |
} |
207 | 18 | this.maximumSize = maximumSize; |
208 | 18 | } |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
public synchronized int getMinimumSize() { |
215 | 30 | return minimumSize; |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
public synchronized void setMinimumSize(int minimumSize) { |
223 | 18 | if (minimumSize < 0 || minimumSize > getMaximumSize()) { |
224 | 0 | throw new IllegalArgumentException(Integer.toString(minimumSize)); |
225 | |
} |
226 | 18 | this.minimumSize = minimumSize; |
227 | 18 | } |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public boolean isResizable() { |
234 | 18 | return resizable; |
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public void setResizable(boolean resizable) { |
242 | 0 | this.resizable = resizable; |
243 | 0 | } |
244 | |
|
245 | |
} |