Skip to content

Package: RepositoryMap

RepositoryMap

nameinstructionbranchcomplexitylinemethod
RepositoryMap()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
add(Descriptor, Object, Object)
M: 8 C: 16
67%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 5
100%
M: 0 C: 1
100%
addEntityToRepository(Object, Descriptor)
M: 4 C: 11
73%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
clear()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
contains(Descriptor, Object)
M: 8 C: 14
64%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 4
100%
M: 0 C: 1
100%
get(Descriptor, Object)
M: 8 C: 20
71%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
getEntityDescriptor(Object)
M: 8 C: 13
62%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 3
100%
M: 0 C: 1
100%
getMap(Descriptor)
M: 0 C: 27
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
initDescriptors()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
remove(Descriptor, Object)
M: 8 C: 15
65%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 5
100%
M: 0 C: 1
100%
removeEntityToRepository(Object)
M: 4 C: 10
71%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 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 cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18:
19: import java.net.URI;
20: import java.util.HashMap;
21: import java.util.IdentityHashMap;
22: import java.util.Map;
23: import java.util.Set;
24:
25: final class RepositoryMap {
26:
27: private final Map<Set<URI>, Map<Object, Object>> origsToClones = new HashMap<>();
28: private Map<Object, Descriptor> entityDescriptors;
29:
30: RepositoryMap() {
31: initDescriptors();
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: void addEntityToRepository(Object entity, Descriptor descriptor) {
56:• assert entityDescriptors != null;
57: entityDescriptors.put(entity, descriptor);
58: }
59:
60: void removeEntityToRepository(Object entity) {
61:• assert entityDescriptors != null;
62: entityDescriptors.remove(entity);
63: }
64:
65: boolean contains(Descriptor descriptor, Object original) {
66:• assert descriptor != null;
67:• assert original != null;
68:
69: final Map<Object, Object> entities = getMap(descriptor);
70: return entities.containsKey(original);
71: }
72:
73: Object get(Descriptor descriptor, Object original) {
74:• assert descriptor != null;
75:• assert original != null;
76:
77: final Map<Object, Object> entities = getMap(descriptor);
78:• if (!entities.containsKey(original)) {
79: return null;
80: }
81: return entities.get(original);
82: }
83:
84: Descriptor getEntityDescriptor(Object entity) {
85:• assert entityDescriptors != null;
86:• assert entity != null;
87:
88: return entityDescriptors.get(entity);
89: }
90:
91: void clear() {
92: origsToClones.values().forEach(Map::clear);
93: initDescriptors();
94: }
95:
96: private Map<Object, Object> getMap(Descriptor descriptor) {
97: final Set<URI> ctx = descriptor.getContexts();
98: Map<Object, Object> entities;
99:• if (!origsToClones.containsKey(ctx)) {
100: entities = new IdentityHashMap<>();
101: origsToClones.put(ctx, entities);
102: } else {
103: entities = origsToClones.get(ctx);
104: }
105: return entities;
106: }
107: }