forked from cedar-policy/cedar-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContext.java
More file actions
150 lines (132 loc) · 5.47 KB
/
Context.java
File metadata and controls
150 lines (132 loc) · 5.47 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright Cedar Contributors
*
* 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
*
* https://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 com.cedarpolicy.model;
import java.util.HashMap;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import java.util.Map;
import com.cedarpolicy.value.Value;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
public class Context {
private Map<String, Value> context;
/**
* Constructs a new empty Context with no key-value pairs.
* Initializes the internal context map as an empty immutable map.
*/
public Context() {
context = Collections.emptyMap();
}
public boolean isEmpty() {
return context.isEmpty();
}
/**
* Constructs a new Context from an Iterable of key-value pairs.
* Creates a new HashMap and populates it with the provided entries.
* Equivalent to from_pairs in Cedar Rust.
*
* @param contextList An Iterable containing key-value pairs to initialize this context with
* @throws IllegalStateException if a duplicate key is found within the iterable
* @throws IllegalArgumentException if the contextList parameter is null
*/
@SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
public Context(Iterable<Map.Entry<String, Value>> contextList) {
context = new HashMap<>();
fromIterable(contextList);
}
/**
* Constructs a new Context with the provided map of key-value pairs.
* Creates a defensive copy of the input map to maintain immutability.
*
* @param contextMap The map of key-value pairs to initialize this context with
* @throws IllegalArgumentException if the contextMap parameter is null
*/
public Context(Map<String, Value> contextMap) {
context = new HashMap<>();
context.putAll(contextMap);
}
/**
* Returns a defensive copy of the internal context map.
*
* @return A new HashMap containing all key-value pairs from the internal context
*/
public Map<String, Value> getContext() {
return new HashMap<>(context);
}
/**
* Merges another Context object into the current context.
*
* @param contextToMerge The Context object to merge into this context
* @throws IllegalStateException if a duplicate key is found while merging the context
* @throws IllegalArgumentException if the contextToMerge parameter is null
*/
public void merge(Context contextToMerge) throws IllegalStateException, IllegalArgumentException {
fromIterable(contextToMerge.getContext().entrySet());
}
/**
* Merges the provided key-value pairs into the current context.
*
* @param contextMaps An Iterable containing key-value pairs to merge into this context
* @throws IllegalStateException if a duplicate key is found in the existing context or duplicate key found within the iterable
* @throws IllegalArgumentException if the contextMaps parameter is null
*/
public void merge(Iterable<Map.Entry<String, Value>> contextMaps) throws IllegalStateException, IllegalArgumentException {
fromIterable(contextMaps);
}
/**
* Retrieves the Value associated with the specified key from the context.
*
* @param key The key whose associated Value is to be returned
* @return The Value associated with the specified key, or null if the key is not found replicating Cedar Rust behavior
* @throws IllegalArgumentException if the key parameter is null
*/
public Value get(String key) {
if (key == null) {
throw new IllegalArgumentException("Key cannot be null");
}
return context.getOrDefault(key, null);
}
/**
* Processes an Iterable of Map entries and adds them to the context.
*
* @param contextIterator The Iterable containing key-value pairs to add to the context
* @throws IllegalStateException if a duplicate key is found in the existing context or duplicate key found within the iterable
* @throws IllegalArgumentException if the contextIterator is null
*/
private void fromIterable(Iterable<Map.Entry<String, Value>> contextIterator) throws IllegalStateException, IllegalArgumentException {
if (contextIterator == null) {
throw new IllegalArgumentException("Context iterator cannot be null");
}
Map<String, Value> newEntries = StreamSupport.stream(contextIterator.spliterator(), false)
.peek(entry -> {
if (context.containsKey(entry.getKey())) {
throw new IllegalStateException(
String.format("Duplicate key '%s' in existing context", entry.getKey())
);
}
})
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue
));
context.putAll(newEntries);
}
/** Readable string representation. */
@Override
public String toString() {
return context.toString();
}
}