You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2020/01/27 00:45:01 UTC
[jira] [Closed] (GROOVY-8517) Add ASTNode.getNodeMetaData overload
that supports computation of missing value (like Map.computeIfAbsent)
[ https://issues.apache.org/jira/browse/GROOVY-8517?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Paul King closed GROOVY-8517.
-----------------------------
> Add ASTNode.getNodeMetaData overload that supports computation of missing value (like Map.computeIfAbsent)
> ----------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-8517
> URL: https://issues.apache.org/jira/browse/GROOVY-8517
> Project: Groovy
> Issue Type: Improvement
> Affects Versions: 3.0.0-alpha-1
> Reporter: Eric Milles
> Assignee: Eric Milles
> Priority: Minor
> Fix For: 3.0.0-rc-3
>
>
> {{org.codehaus.groovy.ast.ASTNode}} currently provides 2 versions of the {{getNodeMetaData}} method. I'd like to see a 3rd option that provides a Java 8 computeIfAbsent-like API, since the method that returns the whole Map returns an immutable view.
> {code:java}
> /**
> * ...
> */
> public <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 + ".");
> if (metaDataMap == null) {
> metaDataMap = new ListHashMap();
> }
> return metaDataMap.computeIfAbsent(key, valFn);
> }
> /**
> * Gets the node meta data.
> *
> * @param key - the meta data key
> * @return the node meta data value for this key
> */
> public <T> T getNodeMetaData(Object key) {
> if (metaDataMap == null) {
> return (T) null;
> }
> return (T) metaDataMap.get(key);
> }
> /**
> * Returns an unmodifiable view of the current node metadata.
> * @return the node metadata. Always not null.
> */
> public Map<?,?> getNodeMetaData() {
> if (metaDataMap==null) {
> return Collections.emptyMap();
> }
> return Collections.unmodifiableMap(metaDataMap);
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)