Eric Milles created GROOVY-10616:
------------------------------------

             Summary: Add peek extension method for Optional, OptionalInt, 
OptionalLong and OptionalDouble
                 Key: GROOVY-10616
                 URL: https://issues.apache.org/jira/browse/GROOVY-10616
             Project: Groovy
          Issue Type: Improvement
          Components: groovy-jdk
            Reporter: Eric Milles
            Assignee: Eric Milles


{{java.util.Optional}}, et al. do not offer a direct method for consuming the 
value if present but returning the optional unchanged.  A "peek" method similar 
to {{Stream#peek(Consumer<? super T>)}} would allow this without having to 
resort to using filter and always returning true or map and always returning 
the value.

Proposed extension methods:
{code:java}
<T> Optional<T> peek(Optional<T> self, Consumer<? super T> consumer)
    OptionalInt peek(OptionalInt self, IntConsumer consumer)
    OptionalLong peek(OptionalLong self, LongConsumer consumer)
    OptionalDouble peek(OptionalDouble self, DoubleConsumer consumer)
{code}

Example usage:
{code:groovy}
def test(Optional<?> opt) {
  opt.peek { print it } // only prints if value present
}
{code}



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to