Skip to content

Package: RepositoryMap

RepositoryMap

nameinstructionbranchcomplexitylinemethod
RepositoryMap()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
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: 32
100%
M: 0 C: 4
100%
M: 0 C: 3
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: 4
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: *
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: void initDescriptors() {
32: this.entityDescriptors = new IdentityHashMap<>();
33: }
34:
35: void add(Descriptor descriptor, Object original, Object clone) {
36:• assert descriptor != null;
37:• assert original != null;
38: // Null values are permitted
39:
40: final Map<Object, Object> entities = getMap(descriptor);
41: entities.put(original, clone);
42: }
43:
44: void remove(Descriptor descriptor, Object original) {
45:• assert descriptor != null;
46:• assert original != null;
47:
48: final Map<Object, Object> entities = getMap(descriptor);
49: entities.remove(original);
50: }
51:
52: /**
53: * Make sure to call {@link #initDescriptors()} before calling this.
54: */
55: void addEntityToRepository(Object entity, Descriptor descriptor) {
56:• assert entityDescriptors != null;
57: entityDescriptors.put(entity, descriptor);
58: }
59:
60: /**
61: * Make sure to call {@link #initDescriptors()} before calling this.
62: */
63: void removeEntityToRepository(Object entity) {
64:• assert entityDescriptors != null;
65: entityDescriptors.remove(entity);
66: }
67:
68: boolean contains(Descriptor descriptor, Object original) {
69:• assert descriptor != null;
70:• assert original != null;
71:
72: final Map<Object, Object> entities = getMap(descriptor);
73: return entities.containsKey(original);
74: }
75:
76: Object get(Descriptor descriptor, Object original) {
77:• assert descriptor != null;
78:• assert original != null;
79:
80: final Map<Object, Object> entities = getMap(descriptor);
81:• if (!entities.containsKey(original)) {
82: return null;
83: }
84: return entities.get(original);
85: }
86:
87: /**
88: * Make sure to call {@link #initDescriptors()} before calling this.
89: */
90: Descriptor getEntityDescriptor(Object entity) {
91:• assert entityDescriptors != null;
92:• assert entity != null;
93:
94: return entityDescriptors.get(entity);
95: }
96:
97: void clear() {
98: origsToClones.values().forEach(Map::clear);
99:• if (entityDescriptors != null) {
100: initDescriptors();
101: }
102: }
103:
104: private Map<Object, Object> getMap(Descriptor descriptor) {
105:• final URI ctx = descriptor.getContext() != null ? descriptor.getContext() : DEFAULT_CONTEXT;
106: Map<Object, Object> entities;
107:• if (!origsToClones.containsKey(ctx)) {
108: entities = new IdentityHashMap<>();
109: origsToClones.put(ctx, entities);
110: } else {
111: entities = origsToClones.get(ctx);
112: }
113: return entities;
114: }
115: }