Skip to content

Commit 91abfb7

Browse files
committed
GROOVY-10861
1 parent 0148302 commit 91abfb7

File tree

7 files changed

+453
-214
lines changed

7 files changed

+453
-214
lines changed

base/org.codehaus.groovy30/.checkstyle

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<file-match-pattern match-pattern="groovy/ast/ImportNode.java" include-pattern="false" />
3232
<file-match-pattern match-pattern="groovy/ast/MethodNode.java" include-pattern="false" />
3333
<file-match-pattern match-pattern="groovy/ast/ModuleNode.java" include-pattern="false" />
34+
<file-match-pattern match-pattern="groovy/ast/NodeMetaDataHandler.java" include-pattern="false" />
3435
<file-match-pattern match-pattern="groovy/ast/Parameter.java" include-pattern="false" />
3536
<file-match-pattern match-pattern="groovy/ast/expr/ClassExpression.java" include-pattern="false" />
3637
<file-match-pattern match-pattern="groovy/ast/expr/ConstantExpression.java" include-pattern="false" />
@@ -70,7 +71,6 @@
7071
<file-match-pattern match-pattern="groovy/transform/trait/TraitASTTransformation.java" include-pattern="false" />
7172
<file-match-pattern match-pattern="groovy/transform/trait/TraitComposer.java" include-pattern="false" />
7273
<file-match-pattern match-pattern="groovy/transform/trait/Traits.java" include-pattern="false" />
73-
<file-match-pattern match-pattern="groovy/util/ListHashMap.java" include-pattern="false" />
7474
<file-match-pattern match-pattern="groovy/vmplugin/v8/Java8.java" include-pattern="false" />
7575
</fileset>
7676
<filter name="DerivedFiles" enabled="true" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
}

base/org.codehaus.groovy30/src/org/codehaus/groovy/util/ListHashMap.java

-213
This file was deleted.

base/org.codehaus.groovy40/.checkstyle

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<file-match-pattern match-pattern="groovy/ast/ImportNode.java" include-pattern="false" />
3030
<file-match-pattern match-pattern="groovy/ast/MethodNode.java" include-pattern="false" />
3131
<file-match-pattern match-pattern="groovy/ast/ModuleNode.java" include-pattern="false" />
32+
<file-match-pattern match-pattern="groovy/ast/NodeMetaDataHandler.java" include-pattern="false" />
3233
<file-match-pattern match-pattern="groovy/ast/Parameter.java" include-pattern="false" />
3334
<file-match-pattern match-pattern="groovy/ast/expr/ClassExpression.java" include-pattern="false" />
3435
<file-match-pattern match-pattern="groovy/ast/expr/ConstantExpression.java" include-pattern="false" />

0 commit comments

Comments
 (0)