|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.common.values; |
| 16 | + |
| 17 | +import com.google.errorprone.annotations.Immutable; |
| 18 | +import dev.cel.common.annotations.Internal; |
| 19 | +import dev.cel.common.exceptions.CelAttributeNotFoundException; |
| 20 | +import dev.cel.common.types.CelType; |
| 21 | +import dev.cel.common.types.MapType; |
| 22 | +import dev.cel.common.types.SimpleType; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +/** |
| 30 | + * A custom CelValue implementation that allows O(1) insertions for maps during comprehension. |
| 31 | + * |
| 32 | + * <p>CEL Library Internals. Do Not Use. |
| 33 | + */ |
| 34 | +@Internal |
| 35 | +@Immutable |
| 36 | +@SuppressWarnings("Immutable") // Intentionally mutable for performance reasons |
| 37 | +public final class MutableMapValue extends CelValue |
| 38 | + implements SelectableValue<Object>, Map<Object, Object> { |
| 39 | + private final Map<Object, Object> internalMap; |
| 40 | + private final CelType celType; |
| 41 | + |
| 42 | + public static MutableMapValue create(Map<?, ?> map) { |
| 43 | + return new MutableMapValue(map); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public int size() { |
| 48 | + return internalMap.size(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean isEmpty() { |
| 53 | + return internalMap.isEmpty(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean containsKey(Object key) { |
| 58 | + return internalMap.containsKey(key); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean containsValue(Object value) { |
| 63 | + return internalMap.containsValue(value); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Object get(Object key) { |
| 68 | + return internalMap.get(key); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Object put(Object key, Object value) { |
| 73 | + return internalMap.put(key, value); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Object remove(Object key) { |
| 78 | + return internalMap.remove(key); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void putAll(Map<?, ?> m) { |
| 83 | + internalMap.putAll(m); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void clear() { |
| 88 | + internalMap.clear(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Set<Object> keySet() { |
| 93 | + return internalMap.keySet(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Collection<Object> values() { |
| 98 | + return internalMap.values(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Set<Entry<Object, Object>> entrySet() { |
| 103 | + return internalMap.entrySet(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Object select(Object field) { |
| 108 | + Object val = internalMap.get(field); |
| 109 | + if (val != null) { |
| 110 | + return val; |
| 111 | + } |
| 112 | + if (!internalMap.containsKey(field)) { |
| 113 | + throw CelAttributeNotFoundException.forMissingMapKey(field.toString()); |
| 114 | + } |
| 115 | + throw CelAttributeNotFoundException.of( |
| 116 | + String.format("Map value cannot be null for key: %s", field)); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Optional<?> find(Object field) { |
| 121 | + if (internalMap.containsKey(field)) { |
| 122 | + return Optional.ofNullable(internalMap.get(field)); |
| 123 | + } |
| 124 | + return Optional.empty(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Object value() { |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean isZeroValue() { |
| 134 | + return internalMap.isEmpty(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public CelType celType() { |
| 139 | + return celType; |
| 140 | + } |
| 141 | + |
| 142 | + private MutableMapValue(Map<?, ?> map) { |
| 143 | + this.internalMap = new LinkedHashMap<>(map); |
| 144 | + this.celType = MapType.create(SimpleType.DYN, SimpleType.DYN); |
| 145 | + } |
| 146 | +} |
0 commit comments