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) 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: }