1 | 118 | |
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.io.Serializable; |
20 | |
import java.util.AbstractList; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Collection; |
23 | |
import java.util.Collections; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.LinkedHashMap; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import org.apache.commons.collections4.IteratorUtils; |
30 | |
import org.apache.commons.collections4.Transformer; |
31 | |
import org.apache.commons.lang3.ArrayUtils; |
32 | |
import org.apache.commons.lang3.Validate; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 410 | public class EntityMap extends EntityCollectionSupport implements NamedEntityCollection { |
39 | |
|
40 | |
private static final long serialVersionUID = 3162898927743996952L; |
41 | |
|
42 | 3496 | private static class Child implements Serializable { |
43 | |
|
44 | |
|
45 | |
private static final long serialVersionUID = 2347729609002856564L; |
46 | |
|
47 | 378 | private final String name; |
48 | |
@SuppressWarnings("PMD.UnusedPrivateField") |
49 | 1370 | private final Entity entity; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 1176 | |
56 | 1764 | public Child(String name, Entity entity) { |
57 | 1764 | this.name = name; |
58 | 588 | if (entity == null) { |
59 | 0 | throw new IllegalArgumentException(); |
60 | 1176 | } |
61 | 1764 | this.entity = entity; |
62 | 588 | } |
63 | |
|
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | 116 | |
70 | |
private static class ChildrenList extends AbstractList<Entity> implements Serializable { |
71 | |
|
72 | |
private static final long serialVersionUID = -954784669450571284L; |
73 | 1988 | |
74 | 2 | private static final Transformer<Child, Entity> CHILD_TO_ENTITY = new Transformer<Child, Entity>() { |
75 | |
|
76 | 1984 | public Entity transform(Child input) { |
77 | 992 | return input.entity; |
78 | |
} |
79 | 2 | }; |
80 | |
|
81 | |
private final List<Child> wrapped; |
82 | |
|
83 | 88 | |
84 | 88 | |
85 | 88 | |
86 | |
|
87 | 440 | private ChildrenList(List<Child> wrapped) { |
88 | 440 | super(); |
89 | 440 | this.wrapped = wrapped; |
90 | 176 | } |
91 | |
|
92 | 0 | |
93 | |
|
94 | |
|
95 | |
@Override |
96 | 0 | public Entity get(int index) { |
97 | 0 | return CHILD_TO_ENTITY.transform(wrapped.get(index)); |
98 | |
} |
99 | |
|
100 | 186 | |
101 | |
|
102 | |
|
103 | |
@Override |
104 | 558 | public Iterator<Entity> iterator() { |
105 | 379 | return IteratorUtils.transformedIterator(wrapped.iterator(), CHILD_TO_ENTITY); |
106 | |
} |
107 | |
|
108 | |
@Override |
109 | 21 | public int size() { |
110 | 14 | return wrapped.size(); |
111 | |
} |
112 | |
|
113 | |
} |
114 | |
|
115 | |
|
116 | |
private List<Child> children; |
117 | |
|
118 | |
|
119 | |
private Map<String, Entity> childMap; |
120 | |
|
121 | |
private List<Entity> exposeChildrenList; |
122 | |
|
123 | |
|
124 | 294 | |
125 | |
|
126 | 294 | |
127 | 88 | |
128 | 970 | public synchronized void add(String name, Entity child) { |
129 | 676 | Validate.notNull(child, "child entity is null"); |
130 | 882 | |
131 | 1146 | if (children == null) { |
132 | 440 | children = new ArrayList<Child>(); |
133 | 440 | exposeChildrenList = new ChildrenList(children); |
134 | 470 | childMap = new LinkedHashMap<String, Entity>(); |
135 | 1143 | } |
136 | 588 | if (childMap.containsKey(name)) { |
137 | 294 | throw new IllegalArgumentException("cannot add > 1 child entity '" + name + "'"); |
138 | 1176 | } |
139 | 1371 | if (name != null) { |
140 | 522 | childMap.put(name, child); |
141 | 882 | } |
142 | 1470 | children.add(new Child(name, child)); |
143 | 588 | } |
144 | |
|
145 | 262 | |
146 | |
|
147 | |
|
148 | |
|
149 | 786 | public synchronized Map<String, Entity> getChildMap() { |
150 | 524 | return childMap == null ? Collections.<String, Entity> emptyMap() : Collections.unmodifiableMap(childMap); |
151 | |
} |
152 | 931 | |
153 | |
|
154 | |
|
155 | |
|
156 | 2793 | public synchronized String[] getChildNames() { |
157 | 3724 | return childMap == null ? ArrayUtils.EMPTY_STRING_ARRAY |
158 | 1862 | : childMap.keySet().toArray(new String[childMap.size()]); |
159 | |
} |
160 | 258 | |
161 | |
|
162 | |
|
163 | |
|
164 | 774 | public Entity getChild(String name) { |
165 | 516 | return getChildMap().get(name); |
166 | |
} |
167 | 0 | |
168 | |
|
169 | |
|
170 | |
|
171 | 0 | public boolean hasChild(String name) { |
172 | 0 | return getChildMap().containsKey(name); |
173 | |
} |
174 | 193 | |
175 | |
|
176 | |
|
177 | |
|
178 | 579 | public synchronized Collection<Entity> getChildren() { |
179 | 386 | return children == null ? Collections.<Entity> emptyList() : Collections.unmodifiableList(exposeChildrenList); |
180 | |
} |
181 | |
|
182 | 59 | |
183 | 59 | |
184 | 59 | |
185 | 59 | @Override |
186 | 236 | public synchronized EntityMap clone() { |
187 | 354 | EntityMap result = (EntityMap) super.clone(); |
188 | 484 | result.children = null; |
189 | 484 | result.childMap = null; |
190 | 295 | result.exposeChildrenList = null; |
191 | 354 | if (children != null) { |
192 | 1181 | for (Child child : children) { |
193 | 945 | result.add(child.name, child.entity.clone()); |
194 | |
} |
195 | 177 | } |
196 | 118 | return result; |
197 | |
} |
198 | |
} |