-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInverseContext.java
More file actions
119 lines (104 loc) · 4.34 KB
/
InverseContext.java
File metadata and controls
119 lines (104 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package no.hasmac.jsonld.context;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.util.Map;
import java.util.Optional;
public final class InverseContext {
private final Map<String, Map<String, Map<String, Map<String, String>>>> context;
public InverseContext() {
this.context = new Object2ObjectOpenHashMap<>();
}
private void set(final String variable, final String container, final String type, final String key, final String value) {
context.computeIfAbsent(variable, x -> new Object2ObjectOpenHashMap<>())
.computeIfAbsent(container, x -> new Object2ObjectOpenHashMap<>())
.computeIfAbsent(type, x -> new Object2ObjectLinkedOpenHashMap<>())
.put(key, value);
}
private boolean doesNotContain(final String variable, final String container, final String type, final String key) {
var stringMapMap = context.get(variable);
if(stringMapMap != null){
var stringMapMap1 = stringMapMap.get(container);
if(stringMapMap1 != null){
var stringStringMap = stringMapMap1.get(type);
if(stringStringMap != null){
return !stringStringMap.containsKey(key);
}
}
}
return true;
}
public boolean contains(final String variable) {
return context.containsKey(variable);
}
public boolean contains(final String variable, final String container, final String type) {
var stringMapMap = context.get(variable);
if(stringMapMap != null){
var stringMapMap1 = stringMapMap.get(container);
if(stringMapMap1 != null){
return stringMapMap1.containsKey(type);
}
}
return false;
}
public boolean contains(final String variable, final String container, final String type, final String key) {
return contains(variable)
&& context.get(variable).containsKey(container)
&& context.get(variable).get(container).containsKey(type)
&& context.get(variable).get(container).get(type).containsKey(key);
}
public InverseContext setIfAbsent(final String variable, final String container, final String type, final String key, final String value) {
if (doesNotContain(variable, container, type, key)) {
set(variable, container, type, key, value);
}
return this;
}
public Optional<String> get(final String variable, final String container, final String type, final String key) {
if (doesNotContain(variable, container, type, key)) {
return Optional.empty();
}
return Optional.ofNullable(context.get(variable).get(container).get(type).get(key));
}
public String getNullable(final String variable, final String container, final String type, final String key) {
var map1 = context.get(variable);
if(map1 == null){
return null;
}
var map2 = map1.get(container);
if(map2 == null){
return null;
}
var map3 = map2.get(type);
if(map3 == null){
return null;
}
return map3.get(key);
}
public Map<String, String> getNullable(final String variable, final String container, final String type) {
var map1 = context.get(variable);
if(map1 == null){
return null;
}
var map2 = map1.get(container);
if(map2 == null){
return null;
}
return map2.get(type);
}
}