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