You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2020/01/04 16:13:00 UTC

[jira] [Assigned] (GROOVY-9337) DGM: more Optional and Stream extension methods

     [ https://issues.apache.org/jira/browse/GROOVY-9337?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles reassigned GROOVY-9337:
-----------------------------------

    Assignee: Eric Milles

> DGM: more Optional and Stream extension methods
> -----------------------------------------------
>
>                 Key: GROOVY-9337
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9337
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.5.8, 3.0.0
>            Reporter: Eric Milles
>            Assignee: Eric Milles
>            Priority: Major
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> Follow up to GROOVY-8519.  Suggestions for new extension methods for Java 8's {{Optional}} and {{Stream}}:
> {code:groovy}
> def opt = Optional.ofNullable(...)
> opt.filter(Type) // DGM that is shorthand for opt.filter(it -> it instanceof Type).map(it -> (Type) it) // result is Optional<Type>
> // methods available on Stream, but missing for Optional:
> opt.mapToInt(...) // DGM that accepts ToIntFunction and returns OptionalInt
> opt.mapToLong(...) // DGM that accepts ToLongFunction and returns OptionalLong
> opt.mapToDouble(...) // DGM that accepts ToDoubleFunction and returns OptionalDouble
> opt.stream() // DGM that is shorthand for opt.map(Stream::of).orElseGet(Stream::empty)
> // useful for Stream.concat() or generally interfacing with stream-based APIs
> // add get() to OptionalInt, OptionalLong and OptionalDouble as shorthand for getAsInt(), etc.
> OptionalInt optInt = ...
> optInt.filter(i -> ...) // accepts IntPredicate and returns OptionalInt.  Not sure why this is missing from OptionalInt, but this allows testing the value against some criteria.
> // similarly for OptionalLong and OptionalDouble
> def one = Stream.of(...)
> def two = Stream.of(...)
> def str = one + two // plus() overload for stream as shorthand for Stream.concat()
> str = str + obj // plus() overload for Stream.concat(str, obj.stream())
> str << obj // leftShift() overload; may make sense, may not
> def str2 = Stream.ofNullable(obj) // DGSM to return Stream.empty() for null and Stream.of(obj) for non-null -- exists in Java 9+
> // or maybe a general stream(Object) so you could do this instead:
> def str3 = obj.stream() // Stream.empty() for null and Stream.of(obj) for non-null
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)