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

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.0.0-alpha-1
    • 3.0.0-rc-3
    • 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);
          }
      

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: