Skip to content

Method: clear()

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