Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
3.0.0-alpha-1
-
None
-
None
Description
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.
/** * ... */ 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); }