Skip to contentMethod: initDescriptors()
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions;
16:
17: import java.net.URI;
18: import java.util.HashMap;
19: import java.util.IdentityHashMap;
20: import java.util.Map;
21:
22: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
23:
24: final class RepositoryMap {
25:
26:         private static final URI DEFAULT_CONTEXT = URI.create("http://defaultContext");
27:
28:         private final Map<URI, Map<Object, Object>> origsToClones = new HashMap<>();
29:         private Map<Object, Descriptor> entityDescriptors;
30:
31:         RepositoryMap() {
32:         }
33:
34:         void initDescriptors() {
35:                 this.entityDescriptors = new IdentityHashMap<>();
36:         }
37:
38:         void add(Descriptor descriptor, Object original, Object clone) {
39:                 assert descriptor != null;
40:                 assert original != null;
41:                 // Null values are permitted
42:
43:                 final Map<Object, Object> entities = getMap(descriptor);
44:                 entities.put(original, clone);
45:         }
46:
47:         void remove(Descriptor descriptor, Object original) {
48:                 assert descriptor != null;
49:                 assert original != null;
50:
51:                 final Map<Object, Object> entities = getMap(descriptor);
52:                 entities.remove(original);
53:         }
54:
55:         /**
56:          * Make sure to call {@link #initDescriptors()} before calling this.
57:          */
58:         void addEntityToRepository(Object entity, Descriptor descriptor) {
59:                 assert entityDescriptors != null;
60:                 entityDescriptors.put(entity, descriptor);
61:         }
62:
63:         /**
64:          * Make sure to call {@link #initDescriptors()} before calling this.
65:          */
66:         void removeEntityToRepository(Object entity) {
67:                 assert entityDescriptors != null;
68:                 entityDescriptors.remove(entity);
69:         }
70:
71:         boolean contains(Descriptor descriptor, Object original) {
72:                 assert descriptor != null;
73:                 assert original != null;
74:
75:                 final Map<Object, Object> entities = getMap(descriptor);
76:                 return entities.containsKey(original);
77:         }
78:
79:         Object get(Descriptor descriptor, Object original) {
80:                 assert descriptor != null;
81:                 assert original != null;
82:
83:                 final Map<Object, Object> entities = getMap(descriptor);
84:                 if (!entities.containsKey(original)) {
85:                         return null;
86:                 }
87:                 return entities.get(original);
88:         }
89:
90:         /**
91:          * Make sure to call {@link #initDescriptors()} before calling this.
92:          */
93:         Descriptor getEntityDescriptor(Object entity) {
94:                 assert entityDescriptors != null;
95:                 assert entity != null;
96:
97:                 return entityDescriptors.get(entity);
98:         }
99:
100:         void clear() {
101:                 origsToClones.values().forEach(Map::clear);
102:                 if (entityDescriptors != null) {
103:                         initDescriptors();
104:                 }
105:         }
106:
107:         private Map<Object, Object> getMap(Descriptor descriptor) {
108:                 final URI ctx = descriptor.getContext() != null ? descriptor.getContext() : DEFAULT_CONTEXT;
109:                 Map<Object, Object> entities;
110:                 if (!origsToClones.containsKey(ctx)) {
111:                         entities = new IdentityHashMap<>();
112:                         origsToClones.put(ctx, entities);
113:                 } else {
114:                         entities = origsToClones.get(ctx);
115:                 }
116:                 return entities;
117:         }
118: }