1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.nuiton.eugene.models.object.xml;
24
25 import org.nuiton.eugene.models.object.ObjectModelAttribute;
26 import org.nuiton.eugene.models.object.ObjectModelClass;
27 import org.nuiton.eugene.models.object.ObjectModelClassifier;
28 import org.nuiton.eugene.models.object.ObjectModelDependency;
29 import org.nuiton.eugene.models.object.ObjectModelEnumeration;
30 import org.nuiton.eugene.models.object.ObjectModelInterface;
31 import org.nuiton.eugene.models.object.ObjectModelOperation;
32
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Map;
40
41
42
43
44
45
46
47 public abstract class ObjectModelClassifierImpl extends ObjectModelElementImpl implements ObjectModelClassifier {
48
49 protected boolean extern;
50
51 protected String qualifiedName;
52
53 protected String packageName;
54
55 protected List<ObjectModelInterface> interfaces;
56
57 protected List<ObjectModelImplRef> interfacesRefs = new ArrayList<>();
58
59 protected List<ObjectModelOperation> operations = new ArrayList<>();
60
61 protected Map<String, ObjectModelAttribute> attributes = new HashMap<>();
62
63 protected List<ObjectModelAttribute> orderedAttributes = new ArrayList<>();
64
65 protected List<ObjectModelDependency> dependencies = new ArrayList<>();
66
67 protected String type;
68
69 protected boolean inner;
70
71 @Override
72 public String toString() {
73 return "" + getQualifiedName() + " implements " + getInterfaces();
74 }
75
76 @Override
77 public void postInit() {
78 super.postInit();
79 qualifiedName = packageName + "." + name;
80 }
81
82 public void setExtern(boolean extern) {
83 this.extern = extern;
84 }
85
86 public void setPackage(String packageName) {
87 this.packageName = packageName;
88 }
89
90 public void setInner(boolean inner) {
91 this.inner = inner;
92 }
93
94 @Override
95 public boolean isInner() {
96 return inner;
97 }
98
99 public void addInterface(ObjectModelImplRef ref) {
100
101
102 interfacesRefs.add(ref);
103
104 }
105
106 public void addOperation(ObjectModelOperationImpl operation) {
107
108
109 operation.postInit();
110 operation.setDeclaringElement(this);
111 operations.add(operation);
112
113 }
114
115 public void addAttribute(ObjectModelAttributeImpl attribute) {
116 attribute.postInit();
117 attribute.setDeclaringElement(this);
118 attributes.put(attribute.getName(), attribute);
119 orderedAttributes.add(attribute);
120 }
121
122 public void addDependency(ObjectModelDependencyImpl dependency) {
123 dependency.postInit();
124 dependency.setClient(this);
125 dependencies.add(dependency);
126 }
127
128 public void setType(String type) {
129 this.type = type;
130 }
131
132 public boolean isExtern() {
133 return extern;
134 }
135
136 @Override
137 public String getPackageName() {
138 return packageName;
139 }
140
141 @Override
142 public String getQualifiedName() {
143 return qualifiedName;
144 }
145
146 @Override
147 public Collection<ObjectModelInterface> getInterfaces() {
148 if (interfaces == null) {
149 interfaces = new ArrayList<>();
150 for (ObjectModelImplRef ref : interfacesRefs) {
151
152 ObjectModelInterfaceImpl interfacez =
153 (ObjectModelInterfaceImpl)
154 objectModelImpl.getInterface(ref.getName());
155
156 if (interfacez == null) {
157
158 ExternalCacheExtension cache =
159 objectModelImpl.getExtension(
160 ExternalCacheExtension.OBJECTMODEL_EXTENSION,
161 ExternalCacheExtension.class);
162
163
164 interfacez =
165 cache.getCache(ref, ObjectModelInterfaceImpl.class);
166 }
167 interfaces.add(interfacez);
168 }
169 }
170 return interfaces;
171 }
172
173 @Override
174 public Collection<ObjectModelOperation> getOperations(String name) {
175 List<ObjectModelOperation> result = new ArrayList<>();
176 for (ObjectModelOperation op : getOperations()) {
177 if (name.equals(op.getName())) {
178 result.add(op);
179 }
180 }
181 return result;
182 }
183
184 @Override
185 public Collection<ObjectModelOperation> getOperations() {
186 return operations;
187 }
188
189 @Override
190 public Collection<ObjectModelOperation> getAllOtherOperations(
191 boolean distinct) {
192 return getAllInterfaceOperations(distinct);
193 }
194
195 @Override
196 public Collection<ObjectModelOperation> getAllInterfaceOperations(
197 boolean distinct) {
198 Collection<ObjectModelOperation> result;
199 if (distinct) {
200 result = new HashSet<>();
201 } else {
202 result = new LinkedList<>();
203 }
204 getAllInterfaceOperations(result);
205 return result;
206 }
207
208 protected Collection<ObjectModelOperation> getAllInterfaceOperations(
209 Collection<ObjectModelOperation> result) {
210 for (ObjectModelClassifier interfacez : getInterfaces()) {
211 result.addAll(interfacez.getOperations());
212 ((ObjectModelClassifierImpl) interfacez).getAllInterfaceOperations(result);
213 }
214 return result;
215 }
216
217 @Override
218 public Collection<ObjectModelAttribute> getAttributes() {
219 return orderedAttributes;
220 }
221
222
223
224
225
226
227
228
229 @Override
230 public ObjectModelAttribute getAttribute(String attributeName) {
231 return attributeName == null ? null : attributes.get(attributeName);
232 }
233
234 @Override
235 public Collection<ObjectModelAttribute> getAllInterfaceAttributes() {
236 Collection<ObjectModelAttribute> result = new LinkedList<>();
237 getAllInterfaceAttributes(result);
238 return result;
239 }
240
241 @Override
242 public Collection<ObjectModelAttribute> getAllOtherAttributes() {
243 Collection<ObjectModelAttribute> result = getAllInterfaceAttributes();
244 return result;
245 }
246
247 protected Collection<ObjectModelAttribute> getAllInterfaceAttributes(
248 Collection<ObjectModelAttribute> result) {
249 for (Object o : getInterfaces()) {
250 ObjectModelClassifierImpl clazz = (ObjectModelClassifierImpl) o;
251 result.addAll(clazz.getAttributes());
252 clazz.getAllInterfaceAttributes(result);
253 }
254 return result;
255 }
256
257 @Override
258 public Collection<ObjectModelDependency> getDependencies() {
259 return dependencies;
260 }
261
262 @Override
263 public ObjectModelDependency getDependency(String name) {
264 if (name.isEmpty()) {
265 return null;
266 }
267 for (ObjectModelDependency dependency : dependencies) {
268 if (dependency.getName().equalsIgnoreCase(name)) {
269 return dependency;
270 }
271 }
272 return null;
273 }
274
275 @Override
276 public final boolean isClass() {
277 return this instanceof ObjectModelClass;
278 }
279
280 @Override
281 public final boolean isInterface() {
282 return this instanceof ObjectModelInterface;
283 }
284
285 @Override
286 public final boolean isEnum() {
287 return this instanceof ObjectModelEnumeration;
288 }
289 }