View Javadoc
1   /*
2    * #%L
3    * EUGene :: EUGene
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
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   * ObjectModelClassifierImpl.
43   *
44   * @author chatellier
45   * @author cedric
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         //if (ref == null)
101         //    return new ObjectModelImplRef();
102         interfacesRefs.add(ref);
103         //return ref;
104     }
105 
106     public void addOperation(ObjectModelOperationImpl operation) {
107         //if (operation == null)
108         //    return new ObjectModelOperationImpl(objectModelImpl, this);
109         operation.postInit();
110         operation.setDeclaringElement(this);
111         operations.add(operation);
112         //return operation;
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) { // Interface not exist in model
157 
158                     ExternalCacheExtension cache =
159                             objectModelImpl.getExtension(
160                                     ExternalCacheExtension.OBJECTMODEL_EXTENSION,
161                                     ExternalCacheExtension.class);
162 
163                     // get external interface from cache (or create it)
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      * Returns the attribute corresponding to the given name, or null if the
224      * class contains no attribute for this name.
225      *
226      * @return the ObjectModelAttribute of the found attribute, or null if the
227      * class contains no attribute for this name.
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 }