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/03 18:01:00 UTC

[jira] [Updated] (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 updated GROOVY-9337:
--------------------------------
    Description: 
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((Type) it) // result is Optional<Type>

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

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
// 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}

  was:
Follow up to GROOVY-8519.  Suggestions for new extension methods for Java 8's {{Optional}} and {{Stream}}:
{code:groovy}
def opt = Optional.ofNullable(...)
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

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
// 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}


> 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
>            Priority: Major
>
> 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((Type) it) // result is Optional<Type>
> 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
> 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
> // 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)