You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/11/01 16:45:40 UTC
[groovy] 02/09: GROOVY-8517: add getNodeMetaData(Object,Function)
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 5d2294a725a541512601dfe9ca8f14a2c6688df9
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Oct 31 08:39:05 2019 -0500
GROOVY-8517: add getNodeMetaData(Object,Function)
(cherry picked from commit 5c81d14da2ae050a1bda2d0e986831275da3ef14)
---
.../codehaus/groovy/ast/NodeMetaDataHandler.java | 42 +++++++++++++++-------
1 file changed, 30 insertions(+), 12 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/ast/NodeMetaDataHandler.java b/src/main/java/org/codehaus/groovy/ast/NodeMetaDataHandler.java
index 280cd6b..8bfa8f0 100644
--- a/src/main/java/org/codehaus/groovy/ast/NodeMetaDataHandler.java
+++ b/src/main/java/org/codehaus/groovy/ast/NodeMetaDataHandler.java
@@ -23,30 +23,49 @@ import org.codehaus.groovy.util.ListHashMap;
import java.util.Collections;
import java.util.Map;
+import java.util.function.Function;
/**
- * An interface to mark a node being able to handle metadata
+ * An interface to mark a node being able to handle metadata.
*/
+@SuppressWarnings({"unchecked", "rawtypes"})
public interface NodeMetaDataHandler {
/**
* Gets the node meta data.
*
- * @param key - the meta data key
+ * @param key the meta data key
* @return the node meta data value for this key
*/
default <T> T getNodeMetaData(Object key) {
- Map<?, ?> metaDataMap = this.getMetaDataMap();
-
+ Map metaDataMap = this.getMetaDataMap();
if (metaDataMap == null) {
- return (T) null;
+ return null;
}
return (T) metaDataMap.get(key);
}
/**
+ * Gets the node meta data.
+ *
+ * @param key the meta data key
+ * @param valFn the meta data value supplier
+ * @return the node meta data value for this key
+ */
+ default <T> T getNodeMetaData(Object key, Function<?, ? extends T> valFn) {
+ if (key == null) throw new GroovyBugError("Tried to get/set meta data with null key on " + this + ".");
+
+ Map metaDataMap = this.getMetaDataMap();
+ if (metaDataMap == null) {
+ metaDataMap = new ListHashMap();
+ this.setMetaDataMap(metaDataMap);
+ }
+ return (T) metaDataMap.computeIfAbsent(key, valFn);
+ }
+
+ /**
* Copies all node meta data from the other node to this one
*
- * @param other - the other node
+ * @param other the other node
*/
default void copyNodeMetaData(NodeMetaDataHandler other) {
Map otherMetaDataMap = other.getMetaDataMap();
@@ -65,8 +84,8 @@ public interface NodeMetaDataHandler {
/**
* Sets the node meta data.
*
- * @param key - the meta data key
- * @param value - the meta data value
+ * @param key the meta data key
+ * @param value the meta data value
* @throws GroovyBugError if key is null or there is already meta
* data under that key
*/
@@ -85,8 +104,8 @@ public interface NodeMetaDataHandler {
/**
* Sets the node meta data but allows overwriting values.
*
- * @param key - the meta data key
- * @param value - the meta data value
+ * @param key the meta data key
+ * @param value the meta data value
* @return the old node meta data value for this key
* @throws GroovyBugError if key is null
*/
@@ -104,7 +123,7 @@ public interface NodeMetaDataHandler {
/**
* Removes a node meta data entry.
*
- * @param key - the meta data key
+ * @param key the meta data key
* @throws GroovyBugError if the key is null
*/
default void removeNodeMetaData(Object key) {
@@ -124,7 +143,6 @@ public interface NodeMetaDataHandler {
*/
default Map<?, ?> getNodeMetaData() {
Map metaDataMap = this.getMetaDataMap();
-
if (metaDataMap == null) {
return Collections.emptyMap();
}