|
| 1 | +/* |
| 2 | +* Copyright Cedar Contributors |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package com.cedarpolicy.model; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import java.util.stream.StreamSupport; |
| 23 | +import java.util.Map; |
| 24 | +import com.cedarpolicy.value.Value; |
| 25 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 26 | + |
| 27 | +public class Context { |
| 28 | + |
| 29 | + private Map<String, Value> context; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructs a new empty Context with no key-value pairs. Initializes the internal context map as an empty |
| 33 | + * immutable map. |
| 34 | + */ |
| 35 | + public Context() { |
| 36 | + context = Collections.emptyMap(); |
| 37 | + } |
| 38 | + |
| 39 | + public boolean isEmpty() { |
| 40 | + return context.isEmpty(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Constructs a new Context from an Iterable of key-value pairs. Creates a new HashMap and populates it with the |
| 45 | + * provided entries. Equivalent to from_pairs in Cedar Rust. |
| 46 | + * |
| 47 | + * @param contextList An Iterable containing key-value pairs to initialize this context with |
| 48 | + * @throws IllegalStateException if a duplicate key is found within the iterable |
| 49 | + * @throws IllegalArgumentException if the contextList parameter is null |
| 50 | + */ |
| 51 | + @SuppressFBWarnings("CT_CONSTRUCTOR_THROW") |
| 52 | + public Context(Iterable<Map.Entry<String, Value>> contextList) { |
| 53 | + context = new HashMap<>(); |
| 54 | + mergeContextFromIterable(contextList); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructs a new Context with the provided map of key-value pairs. Creates a defensive copy of the input map to |
| 59 | + * maintain immutability. |
| 60 | + * |
| 61 | + * @param contextMap The map of key-value pairs to initialize this context with |
| 62 | + * @throws IllegalArgumentException if the contextMap parameter is null |
| 63 | + */ |
| 64 | + public Context(Map<String, Value> contextMap) { |
| 65 | + context = new HashMap<>(); |
| 66 | + context.putAll(contextMap); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns a defensive copy of the internal context map. |
| 71 | + * |
| 72 | + * @return A new HashMap containing all key-value pairs from the internal context |
| 73 | + */ |
| 74 | + public Map<String, Value> getContext() { |
| 75 | + return new HashMap<>(context); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Merges another Context object into the current context. |
| 80 | + * |
| 81 | + * @param contextToMerge The Context object to merge into this context |
| 82 | + * @throws IllegalStateException if a duplicate key is found while merging the context |
| 83 | + * @throws IllegalArgumentException if the contextToMerge parameter is null |
| 84 | + */ |
| 85 | + public void merge(Context contextToMerge) throws IllegalStateException, IllegalArgumentException { |
| 86 | + mergeContextFromIterable(contextToMerge.getContext().entrySet()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Merges the provided key-value pairs into the current context. |
| 91 | + * |
| 92 | + * @param contextMaps An Iterable containing key-value pairs to merge into this context |
| 93 | + * @throws IllegalStateException if a duplicate key is found in the existing context or duplicate key found |
| 94 | + * within the iterable |
| 95 | + * @throws IllegalArgumentException if the contextMaps parameter is null |
| 96 | + */ |
| 97 | + public void merge(Iterable<Map.Entry<String, Value>> contextMaps) |
| 98 | + throws IllegalStateException, IllegalArgumentException { |
| 99 | + mergeContextFromIterable(contextMaps); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Retrieves the Value associated with the specified key from the context. |
| 104 | + * |
| 105 | + * @param key The key whose associated Value is to be returned |
| 106 | + * @return The Value associated with the specified key, or null if the key is not found replicating Cedar Rust |
| 107 | + * behavior |
| 108 | + * @throws IllegalArgumentException if the key parameter is null |
| 109 | + */ |
| 110 | + public Value get(String key) { |
| 111 | + if (key == null) { |
| 112 | + throw new IllegalArgumentException("Key cannot be null"); |
| 113 | + } |
| 114 | + return context.getOrDefault(key, null); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Processes an Iterable of Map entries and adds them to the context. |
| 119 | + * |
| 120 | + * @param contextIterator The Iterable containing key-value pairs to add to the context |
| 121 | + * @throws IllegalStateException if a duplicate key is found in the existing context or duplicate key found |
| 122 | + * within the iterable |
| 123 | + * @throws IllegalArgumentException if the contextIterator is null |
| 124 | + */ |
| 125 | + private void mergeContextFromIterable(Iterable<Map.Entry<String, Value>> contextIterator) |
| 126 | + throws IllegalStateException, IllegalArgumentException { |
| 127 | + if (contextIterator == null) { |
| 128 | + throw new IllegalArgumentException("Context iterator cannot be null"); |
| 129 | + } |
| 130 | + |
| 131 | + Map<String, Value> newEntries = StreamSupport.stream(contextIterator.spliterator(), false).peek(entry -> { |
| 132 | + if (context.containsKey(entry.getKey())) { |
| 133 | + throw new IllegalStateException( |
| 134 | + String.format("Duplicate key '%s' in existing context", entry.getKey())); |
| 135 | + } |
| 136 | + }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 137 | + context.putAll(newEntries); |
| 138 | + } |
| 139 | + |
| 140 | + /** Readable string representation. */ |
| 141 | + @Override |
| 142 | + public String toString() { |
| 143 | + return context.toString(); |
| 144 | + } |
| 145 | +} |
0 commit comments