|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.codehaus.groovy.ast; |
| 20 | + |
| 21 | +import org.codehaus.groovy.GroovyBugError; |
| 22 | + |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.function.Function; |
| 27 | + |
| 28 | +/** |
| 29 | + * An interface to mark a node being able to handle metadata. |
| 30 | + */ |
| 31 | +public interface NodeMetaDataHandler { |
| 32 | + /** |
| 33 | + * Gets the node meta data. |
| 34 | + * |
| 35 | + * @param key the meta data key |
| 36 | + * @return the node meta data value for this key |
| 37 | + */ |
| 38 | + default <T> T getNodeMetaData(Object key) { |
| 39 | + Map metaDataMap = this.getMetaDataMap(); |
| 40 | + if (metaDataMap == null) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + return (T) metaDataMap.get(key); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the node meta data. |
| 48 | + * |
| 49 | + * @param key the meta data key |
| 50 | + * @param valFn the meta data value supplier |
| 51 | + * @return the node meta data value for this key |
| 52 | + */ |
| 53 | + default <T> T getNodeMetaData(Object key, Function<?, ? extends T> valFn) { |
| 54 | + if (key == null) throw new GroovyBugError("Tried to get/set meta data with null key on " + this + "."); |
| 55 | + |
| 56 | + Map metaDataMap = this.getMetaDataMap(); |
| 57 | + if (metaDataMap == null) { |
| 58 | + metaDataMap = Collections.synchronizedMap(new HashMap(8)); |
| 59 | + this.setMetaDataMap(metaDataMap); |
| 60 | + } |
| 61 | + return (T) metaDataMap.computeIfAbsent(key, valFn); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Copies all node meta data from the other node to this one |
| 66 | + * |
| 67 | + * @param other the other node |
| 68 | + */ |
| 69 | + default void copyNodeMetaData(NodeMetaDataHandler other) { |
| 70 | + Map otherMetaDataMap = other.getMetaDataMap(); |
| 71 | + if (otherMetaDataMap == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + Map metaDataMap = this.getMetaDataMap(); |
| 75 | + if (metaDataMap == null) { |
| 76 | + metaDataMap = Collections.synchronizedMap(new HashMap(8)); |
| 77 | + this.setMetaDataMap(metaDataMap); |
| 78 | + } |
| 79 | + |
| 80 | + metaDataMap.putAll(otherMetaDataMap); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Sets the node meta data. |
| 85 | + * |
| 86 | + * @param key the meta data key |
| 87 | + * @param value the meta data value |
| 88 | + * @throws GroovyBugError if key is null or there is already meta |
| 89 | + * data under that key |
| 90 | + */ |
| 91 | + default void setNodeMetaData(Object key, Object value) { |
| 92 | + Object old = putNodeMetaData(key, value); |
| 93 | + if (old != null) throw new GroovyBugError("Tried to overwrite existing meta data " + this + "."); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets the node meta data but allows overwriting values. |
| 98 | + * |
| 99 | + * @param key the meta data key |
| 100 | + * @param value the meta data value |
| 101 | + * @return the old node meta data value for this key |
| 102 | + * @throws GroovyBugError if key is null |
| 103 | + */ |
| 104 | + default Object putNodeMetaData(Object key, Object value) { |
| 105 | + if (key == null) throw new GroovyBugError("Tried to set meta data with null key on " + this + "."); |
| 106 | + |
| 107 | + Map metaDataMap = this.getMetaDataMap(); |
| 108 | + if (metaDataMap == null) { |
| 109 | + if (value == null) return null; |
| 110 | + metaDataMap = Collections.synchronizedMap(new HashMap(8)); |
| 111 | + this.setMetaDataMap(metaDataMap); |
| 112 | + } else if (value == null) { |
| 113 | + return metaDataMap.remove(key); |
| 114 | + } |
| 115 | + return metaDataMap.put(key, value); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Removes a node meta data entry. |
| 120 | + * |
| 121 | + * @param key the meta data key |
| 122 | + * @throws GroovyBugError if the key is null |
| 123 | + */ |
| 124 | + default void removeNodeMetaData(Object key) { |
| 125 | + if (key == null) throw new GroovyBugError("Tried to remove meta data with null key " + this + "."); |
| 126 | + |
| 127 | + Map metaDataMap = this.getMetaDataMap(); |
| 128 | + if (metaDataMap == null) { |
| 129 | + return; |
| 130 | + } |
| 131 | + metaDataMap.remove(key); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns an unmodifiable view of the current node metadata. |
| 136 | + * |
| 137 | + * @return the node metadata. Always not null. |
| 138 | + */ |
| 139 | + default Map<?, ?> getNodeMetaData() { |
| 140 | + Map metaDataMap = this.getMetaDataMap(); |
| 141 | + if (metaDataMap == null) { |
| 142 | + return Collections.emptyMap(); |
| 143 | + } |
| 144 | + return new HashMap<>(metaDataMap); |
| 145 | + } |
| 146 | + |
| 147 | + Map<?, ?> getMetaDataMap(); |
| 148 | + |
| 149 | + void setMetaDataMap(Map<?, ?> metaDataMap); |
| 150 | +} |
0 commit comments