/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.codehaus.groovy.ast;

import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.util.ListHashMap;

import java.util.Collections;
import java.util.Map;

/**
 * An interface to mark a node being able to handle metadata
 */
public interface NodeMetaDataHandler {
    /**
     * Gets the node meta data.
     *
     * @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();

        if (metaDataMap == null) {
            return (T) null;
        }
        return (T) metaDataMap.get(key);
    }

    /**
     * Copies all node meta data from the other node to this one
     *
     * @param other - the other node
     */
    default void copyNodeMetaData(NodeMetaDataHandler other) {
        Map otherMetaDataMap = other.getMetaDataMap();
        if (otherMetaDataMap == null) {
            return;
        }
        Map metaDataMap = this.getMetaDataMap();
        if (metaDataMap == null) {
            metaDataMap = new ListHashMap();
            this.setMetaDataMap(metaDataMap);
        }

        metaDataMap.putAll(otherMetaDataMap);
    }

    /**
     * Sets the node meta data.
     *
     * @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
     */
    default void setNodeMetaData(Object key, Object value) {
        if (key == null) throw new GroovyBugError("Tried to set meta data with null key on " + this + ".");

        Map metaDataMap = this.getMetaDataMap();
        if (metaDataMap == null) {
            metaDataMap = new ListHashMap();
            this.setMetaDataMap(metaDataMap);
        }
        Object old = metaDataMap.put(key, value);
        if (old != null) throw new GroovyBugError("Tried to overwrite existing meta data " + this + ".");
    }

    /**
     * Sets the node meta data but allows overwriting values.
     *
     * @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
     */
    default Object putNodeMetaData(Object key, Object value) {
        if (key == null) throw new GroovyBugError("Tried to set meta data with null key on " + this + ".");

        Map metaDataMap = this.getMetaDataMap();
        if (metaDataMap == null) {
            metaDataMap = new ListHashMap();
            this.setMetaDataMap(metaDataMap);
        }
        return metaDataMap.put(key, value);
    }

    /**
     * Removes a node meta data entry.
     *
     * @param key - the meta data key
     * @throws GroovyBugError if the key is null
     */
    default void removeNodeMetaData(Object key) {
        if (key == null) throw new GroovyBugError("Tried to remove meta data with null key " + this + ".");

        Map metaDataMap = this.getMetaDataMap();
        if (metaDataMap == null) {
            return;
        }
        metaDataMap.remove(key);
    }

    /**
     * Returns an unmodifiable view of the current node metadata.
     *
     * @return the node metadata. Always not null.
     */
    default Map<?, ?> getNodeMetaData() {
        Map metaDataMap = this.getMetaDataMap();

        if (metaDataMap == null) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(metaDataMap);
    }

    Map<?, ?> getMetaDataMap();

    void setMetaDataMap(Map<?, ?> metaDataMap);
}

@@ -989,16 +989,52 @@
 til.Map;
+%0Aimport java.util.function.Function;
 %0A%0A/**%0A *
@@ -1091,21 +1091,67 @@
 metadata
+.
 %0A */%0A
+@SuppressWarnings(%7B%22unchecked%22, %22rawtypes%22%7D)%0A
 public i
@@ -1237,34 +1237,32 @@
     * @param key
- -
  the meta data k
@@ -1384,22 +1384,16 @@
      Map
-%3C?, ?%3E
  metaDat
@@ -1414,33 +1414,32 @@
 tMetaDataMap();%0A
-%0A
         if (meta
@@ -1480,16 +1480,631 @@
 urn 
-(T) 
 null
+;%0A        %7D%0A        return (T) metaDataMap.get(key);%0A    %7D%0A%0A    /**%0A     * Gets the node meta data.%0A     *%0A     * @param key the meta data key%0A     * @param valFn the meta data value supplier%0A     * @return the node meta data value for this key%0A     */%0A    default %3CT%3E T getNodeMetaData(Object key, Function%3C?, ? extends T%3E valFn) %7B%0A        if (key == null) throw new GroovyBugError(%22Tried to get/set meta data with null key on %22 + this + %22.%22);%0A%0A        Map metaDataMap = this.getMetaDataMap();%0A        if (metaDataMap == null) %7B%0A            metaDataMap = new ListHashMap();%0A            this.setMetaDataMap(metaDataMap)
 ;%0A  
@@ -2134,39 +2134,58 @@
 (T) metaDataMap.
-get(key
+computeIfAbsent(key, valFn
 );%0A    %7D%0A%0A    /*
@@ -2277,18 +2277,16 @@
 am other
- -
  the oth
@@ -2786,34 +2786,32 @@
   * @param key  
- -
  the meta data k
@@ -2824,34 +2824,32 @@
   * @param value
- -
  the meta data v
@@ -3595,18 +3595,16 @@
 am key  
- -
  the met
@@ -3633,18 +3633,16 @@
 am value
- -
  the met
@@ -4247,10 +4247,8 @@
  key
- -
  the
@@ -4859,25 +4859,24 @@
 aDataMap();%0A
-%0A
         if (

add getNodeMetaData(Object,Function)