Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8517

Add ASTNode.getNodeMetaData overload that supports computation of missing value (like Map.computeIfAbsent)

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 3.0.0-alpha-1
    • Fix Version/s: 3.0.0-rc-3
    • Component/s: None
    • Labels:
      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);
          }
      

        Attachments

          Activity

            People

            • Assignee:
              emilles Eric Milles
              Reporter:
              emilles Eric Milles
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: