Skip to contentMethod: clear()
1: package de.fhdw.wtf.persistence.facade;
2:
3: import java.util.Collection;
4: import java.util.HashMap;
5: import java.util.Iterator;
6: import java.util.LinkedList;
7: import java.util.Map;
8:
9: import de.fhdw.wtf.persistence.exception.TypeOrAssociationNotFoundException;
10: import de.fhdw.wtf.persistence.meta.Association;
11: import de.fhdw.wtf.persistence.meta.MapAssociation;
12: import de.fhdw.wtf.persistence.meta.Specialization;
13: import de.fhdw.wtf.persistence.meta.Type;
14: import de.fhdw.wtf.persistence.meta.UnidirectionalAssociation;
15:
16: /**
17: * A class to represent an implementation of the Type Manager Interface, wich stores information about Types and
18: * Associations.
19: *
20: */
21: public final class TypeManagerImplementation implements TypeManager {
22:         
23:         /**
24:          * The Package separator.
25:          */
26:         private static final String JAVA_PACKAGE_SEPERATOR = ".";
27:         
28:         /**
29:          * The Types are stored in a Map indexed by their Ids.
30:          */
31:         private final Map<Long, Type> cacheId;
32:         
33:         /**
34:          * The Types are store in a Map indexed by their Ids.
35:          */
36:         private final Map<Long, Association> associationCacheId;
37:         
38:         /**
39:          * The cache which stores the connection between the name of the classes and the types. It is used to find a type by
40:          * its name.
41:          */
42:         private final Map<String, Type> cacheName;
43:         
44:         /**
45:          * The cache contains all associations.
46:          */
47:         private final Map<String, Association> cacheAssociationName;
48:         
49:         /**
50:          * Holds all specializations stored in this TypeManagerImplementation.
51:          */
52:         private final Collection<Specialization> specializations;
53:         
54:         // /**
55:         // * The implementation of the Type Manager is a singleton
56:         // */
57:         // private static TypeManagerImplementation instance;
58:         
59:         /**
60:          * Provides the only instance of a Type Manager Implementation.
61:          *
62:          * @return The Type Manager Implementation Object.
63:          */
64:         public static TypeManagerImplementation getInstance() {
65:                 // if (instance == null) {
66:                 // instance = new TypeManagerImplementation();
67:                 // }
68:                 // return instance;
69:                 return new TypeManagerImplementation();
70:         }
71:         
72:         /**
73:          * Private constructor, which initializes the fields.
74:          */
75:         private TypeManagerImplementation() {
76:                 super();
77:                 this.cacheId = new HashMap<>();
78:                 this.associationCacheId = new HashMap<>();
79:                 this.cacheName = new HashMap<>();
80:                 this.cacheAssociationName = new HashMap<>();
81:                 this.specializations = new LinkedList<>();
82:         }
83:         
84:         private String prepareName(final String name) {
85:                 return name.replace('>', '.').replace('$', '.');
86:         }
87:         
88:         /*
89:          * (non-Javadoc)
90:          *
91:          * @see persistence.facade.TyperManager#getTypeForId(long)
92:          */
93:         @Override
94:         public Type getTypeForId(final long id) throws TypeOrAssociationNotFoundException {
95:                 if (!this.cacheId.containsKey(id)) {
96:                         throw new TypeOrAssociationNotFoundException(id);
97:                 }
98:                 return this.cacheId.get(id);
99:         }
100:         
101:         /*
102:          * (non-Javadoc)
103:          *
104:          * @see persistence.facade.TyperManager#getTypeforName(java.lang.String)
105:          */
106:         @Override
107:         public Type getTypeforName(final String name) throws TypeOrAssociationNotFoundException {
108:                 if (!this.cacheName.containsKey(this.prepareName(name))) {
109:                         throw new TypeOrAssociationNotFoundException(name);
110:                 }
111:                 return this.cacheName.get(this.prepareName(name));
112:         }
113:         
114:         /*
115:          * (non-Javadoc)
116:          *
117:          * @see persistence.facade.TyperManager#getAssociationForId(long)
118:          */
119:         @Override
120:         public UnidirectionalAssociation getUnidirectionalAssociationForId(final long id)
121:                         throws TypeOrAssociationNotFoundException {
122:                 if (!this.associationCacheId.containsKey(id)) {
123:                         throw new TypeOrAssociationNotFoundException(id);
124:                 }
125:                 final Association association = this.associationCacheId.get(id);
126:                 if (!(association instanceof UnidirectionalAssociation)) {
127:                         throw new TypeOrAssociationNotFoundException(id);
128:                 }
129:                 
130:                 return (UnidirectionalAssociation) association;
131:         }
132:         
133:         /*
134:          * (non-Javadoc)
135:          *
136:          * @see persistence.facade.TyperManager#getAssociationForId(long)
137:          */
138:         @Override
139:         public MapAssociation getMapAssociationForId(final long id) throws TypeOrAssociationNotFoundException {
140:                 if (!this.associationCacheId.containsKey(id)) {
141:                         throw new TypeOrAssociationNotFoundException(id);
142:                 }
143:                 final Association association = this.associationCacheId.get(id);
144:                 if (!(association instanceof MapAssociation)) {
145:                         throw new TypeOrAssociationNotFoundException(id);
146:                 }
147:                 
148:                 return (MapAssociation) association;
149:         }
150:         
151:         /*
152:          * (non-Javadoc)
153:          *
154:          * @see persistence.facade.TyperManager#getAssociationForName(java.lang.String)
155:          */
156:         @Override
157:         public UnidirectionalAssociation getUnidirectionalAssociationForName(final String name)
158:                         throws TypeOrAssociationNotFoundException {
159:                 if (!this.cacheAssociationName.containsKey(this.prepareName(name))) {
160:                         throw new TypeOrAssociationNotFoundException(name);
161:                 }
162:                 final Association association = this.cacheAssociationName.get(this.prepareName(name));
163:                 if (!(association instanceof UnidirectionalAssociation)) {
164:                         throw new TypeOrAssociationNotFoundException(name);
165:                 }
166:                 
167:                 return (UnidirectionalAssociation) association;
168:         }
169:         
170:         /*
171:          * (non-Javadoc)
172:          *
173:          * @see persistence.facade.TyperManager#getAssociationForName(java.lang.String)
174:          */
175:         @Override
176:         public MapAssociation getMapAssociationForName(final String name) throws TypeOrAssociationNotFoundException {
177:                 if (!this.cacheAssociationName.containsKey(this.prepareName(name))) {
178:                         throw new TypeOrAssociationNotFoundException(name);
179:                 }
180:                 final Association association = this.cacheAssociationName.get(this.prepareName(name));
181:                 if (!(association instanceof MapAssociation)) {
182:                         throw new TypeOrAssociationNotFoundException(name);
183:                 }
184:                 
185:                 return (MapAssociation) association;
186:         }
187:         
188:         @Override
189:         public void saveType(final Type type) {
190:                 this.cacheId.put(type.getId(), type);
191:                 final String name = this.prepareName(type.getName());
192:                 this.cacheName.put(name, type);
193:         }
194:         
195:         @Override
196:         public void saveAssociation(final Association association) {
197:                 this.associationCacheId.put(association.getId(), association);
198:                 final String name =
199:                                 this.prepareName(association.getOwner().getName()) + JAVA_PACKAGE_SEPERATOR
200:                                                 + this.prepareName(association.getName());
201:                 this.cacheAssociationName.put(name, association);
202:         }
203:         
204:         @Override
205:         public long getMaximumTypeId() {
206:                 long result = 0;
207:                 final Iterator<Type> types = this.cacheId.values().iterator();
208:                 while (types.hasNext()) {
209:                         final Type type = types.next();
210:                         if (type.getId() > result) {
211:                                 result = type.getId();
212:                         }
213:                 }
214:                 return result;
215:         }
216:         
217:         @Override
218:         public void clear() {
219:                 this.cacheId.clear();
220:                 this.cacheName.clear();
221:                 this.associationCacheId.clear();
222:                 this.cacheAssociationName.clear();
223:                 this.specializations.clear();
224:         }
225:         
226:         @Override
227:         public void deleteType(final long typeId) throws TypeOrAssociationNotFoundException {
228:                 if (!this.cacheId.containsKey(typeId)) {
229:                         throw new TypeOrAssociationNotFoundException(typeId);
230:                 }
231:                 this.cacheName.remove(this.prepareName(this.cacheId.remove(typeId).getName()));
232:         }
233:         
234:         @Override
235:         public void deleteAssociation(final long associationId) throws TypeOrAssociationNotFoundException {
236:                 if (!this.associationCacheId.containsKey(associationId)) {
237:                         throw new TypeOrAssociationNotFoundException(associationId);
238:                 }
239:                 this.cacheAssociationName.remove(this.associationCacheId.remove(associationId));
240:         }
241:         
242:         @Override
243:         public void saveSpecialization(final Specialization specialization) {
244:                 this.specializations.add(specialization);
245:         }
246:         
247:         @Override
248:         public Collection<Specialization> getAllSpecializations() {
249:                 return new LinkedList<>(this.specializations);
250:         }
251:         
252:         @Override
253:         public void deleteSpecialization(final Specialization specialization) {
254:                 this.specializations.remove(specialization);
255:         }
256:         
257:         @Override
258:         public boolean existsType(final long typeId) {
259:                 return this.cacheId.containsKey(typeId);
260:         }
261:         
262:         @Override
263:         public boolean existsType(final String typeName) {
264:                 return this.cacheName.containsKey(this.prepareName(typeName));
265:         }
266:         
267:         @Override
268:         public boolean existsAssociation(final long associationId) {
269:                 return this.associationCacheId.containsKey(associationId);
270:         }
271:         
272:         @Override
273:         public boolean existsSpecialization(final long specializationId) {
274:                 // TODO Auto-generated method stub
275:                 return false;
276:         }
277:         
278: }