Re: [jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-06 Thread ajs6f
If it turns out that nextOptional is problematic because of nulls in iterators, 
we could, at least in theory, change it to return null for null, empty for 
hasNext=false, and Optional.of(next()) for hasNext=true.

ajs6f

> On Dec 6, 2017, at 9:23 AM, Andy Seaborne  wrote:
> 
> Which is why I think we ought to provide "orElse*" directly.
> 
> As we have it currently, ajs6f's argument for only nextOptional() is to 
> provide "orElse" via Optional so the Optional must be the end of iteration.  
> Then it can't be for nulls during iteration (the stream case see [1]).
> 
> Claude - this is the javadoc: point 3 is that ExtendedIterators do not return 
> null for next() in this usage.
> 
> Do you have a suggestion for improving that?
> 
> /**
>Answer with an {@link Optional}.
>   This operation assumes that the {@code ExtendedIterator} does not return 
> null for {@code next()}.
>   If it does,  {@code NullPointerException} is thrown.
>   
>   If there is no next, return {@code Optional.empty()}
>   If the next object exists, and is not null, return that in the {@link 
> Optional}.
>   If the next object exists, and is null, throw {@code 
> NullPointerException}
>   
> */
> 
>Andy
> 
> http://mail.openjdk.java.net/pipermail/jdk8-dev/2013-September/003274.html
> 
> On 06/12/17 14:07, ajs6f wrote:
>> I don't think nextOptional will ever return null, but I think you mean 
>> empty? If so, and IIUC, what you say was exactly my objection (see 
>> discussion in the PR) but as Andy pointed out there, the Model API doesn't 
>> ever actually return an ExtendedIterator with null values, so the method 
>> turns out to be usable in the only use case we have for it.
>> ajs6f
>>> On Dec 6, 2017, at 5:57 AM, Claude Warren  wrote:
>>> 
>>> my reading of this is that nextOptional will return null at the end of the
>>> iteration and that this is indistinguishable from a null in the iteration
>>> unless hasNext() is called.  but calling hasNext() after a null only tells
>>> you if the next item is present not if the last item was present, thus a
>>> null at the end of the iteration may be lost.  It almost seems like you
>>> need a lastRead() method to retrieve the last read object again.  It seems
>>> like this construct will only be useful when there  are no nulls in the
>>> iteration.
>>> 
>>> Claude
>>> 
>>> On Mon, Dec 4, 2017 at 3:59 PM, Andy Seaborne (JIRA) 
>>> wrote:
>>> 
 
[ https://issues.apache.org/jira/browse/JENA-1427?page=
 com.atlassian.jira.plugin.system.issuetabpanels:comment-
 tabpanel=16276983#comment-16276983 ]
 
 Andy Seaborne commented on JENA-1427:
 -
 
 {{nextOptional}} added for release 3.6.0.
 
 Proposal: close this JIRA for now, see how {{nextOptional}} works out and
 revisit {{orElse*}} based on experience.
 
> Add nextOrElse() method in ExtendedIterator
> ---
> 
>Key: JENA-1427
>URL: https://issues.apache.org/jira/browse/JENA-1427
>Project: Apache Jena
> Issue Type: Improvement
> Components: Core
>   Affects Versions: Jena 3.5.0
>   Reporter: Adam Jacobs
>   Priority: Trivial
> Labels: easytask
> 
> Allow a functional approach for returning a default value or throwing a
 custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
>/**
> Answer the next object, if it exists, otherwise invoke the
 _supplier_.
> */
>public default T nextOrElse( Supplier supplier ) {
>return hasNext() ? next() : supplier.get();
>}
> {noformat}
 
 
 
 --
 This message was sent by Atlassian JIRA
 (v6.4.14#64029)
 
>>> 
>>> 
>>> 
>>> -- 
>>> I like: Like Like - The likeliest place on the web
>>> 
>>> LinkedIn: http://www.linkedin.com/in/claudewarren



Re: [jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-06 Thread Andy Seaborne

Which is why I think we ought to provide "orElse*" directly.

As we have it currently, ajs6f's argument for only nextOptional() is to 
provide "orElse" via Optional so the Optional must be the end of 
iteration.  Then it can't be for nulls during iteration (the stream case 
see [1]).


Claude - this is the javadoc: point 3 is that ExtendedIterators do not 
return null for next() in this usage.


Do you have a suggestion for improving that?

 /**
Answer with an {@link Optional}.
   This operation assumes that the {@code ExtendedIterator} does not 
return null for {@code next()}.

   If it does,  {@code NullPointerException} is thrown.
   
   If there is no next, return {@code Optional.empty()}
   If the next object exists, and is not null, return that in the 
{@link Optional}.
   If the next object exists, and is null, throw {@code 
NullPointerException}

   
 */

Andy

http://mail.openjdk.java.net/pipermail/jdk8-dev/2013-September/003274.html

On 06/12/17 14:07, ajs6f wrote:

I don't think nextOptional will ever return null, but I think you mean empty? 
If so, and IIUC, what you say was exactly my objection (see discussion in the 
PR) but as Andy pointed out there, the Model API doesn't ever actually return 
an ExtendedIterator with null values, so the method turns out to be usable in 
the only use case we have for it.

ajs6f


On Dec 6, 2017, at 5:57 AM, Claude Warren  wrote:

my reading of this is that nextOptional will return null at the end of the
iteration and that this is indistinguishable from a null in the iteration
unless hasNext() is called.  but calling hasNext() after a null only tells
you if the next item is present not if the last item was present, thus a
null at the end of the iteration may be lost.  It almost seems like you
need a lastRead() method to retrieve the last read object again.  It seems
like this construct will only be useful when there  are no nulls in the
iteration.

Claude

On Mon, Dec 4, 2017 at 3:59 PM, Andy Seaborne (JIRA) 
wrote:



[ https://issues.apache.org/jira/browse/JENA-1427?page=
com.atlassian.jira.plugin.system.issuetabpanels:comment-
tabpanel=16276983#comment-16276983 ]

Andy Seaborne commented on JENA-1427:
-

{{nextOptional}} added for release 3.6.0.

Proposal: close this JIRA for now, see how {{nextOptional}} works out and
revisit {{orElse*}} based on experience.


Add nextOrElse() method in ExtendedIterator
---

Key: JENA-1427
URL: https://issues.apache.org/jira/browse/JENA-1427
Project: Apache Jena
 Issue Type: Improvement
 Components: Core
   Affects Versions: Jena 3.5.0
   Reporter: Adam Jacobs
   Priority: Trivial
 Labels: easytask

Allow a functional approach for returning a default value or throwing a

custom exception from a Jena iterator.

The following method may be added to the ExtendedIterator interface.
{noformat}
/**
 Answer the next object, if it exists, otherwise invoke the

_supplier_.

 */
public default T nextOrElse( Supplier supplier ) {
return hasNext() ? next() : supplier.get();
}
{noformat}




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)





--
I like: Like Like - The likeliest place on the web

LinkedIn: http://www.linkedin.com/in/claudewarren




Re: [jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-06 Thread ajs6f
I don't think nextOptional will ever return null, but I think you mean empty? 
If so, and IIUC, what you say was exactly my objection (see discussion in the 
PR) but as Andy pointed out there, the Model API doesn't ever actually return 
an ExtendedIterator with null values, so the method turns out to be usable in 
the only use case we have for it.

ajs6f

> On Dec 6, 2017, at 5:57 AM, Claude Warren  wrote:
> 
> my reading of this is that nextOptional will return null at the end of the
> iteration and that this is indistinguishable from a null in the iteration
> unless hasNext() is called.  but calling hasNext() after a null only tells
> you if the next item is present not if the last item was present, thus a
> null at the end of the iteration may be lost.  It almost seems like you
> need a lastRead() method to retrieve the last read object again.  It seems
> like this construct will only be useful when there  are no nulls in the
> iteration.
> 
> Claude
> 
> On Mon, Dec 4, 2017 at 3:59 PM, Andy Seaborne (JIRA) 
> wrote:
> 
>> 
>>[ https://issues.apache.org/jira/browse/JENA-1427?page=
>> com.atlassian.jira.plugin.system.issuetabpanels:comment-
>> tabpanel=16276983#comment-16276983 ]
>> 
>> Andy Seaborne commented on JENA-1427:
>> -
>> 
>> {{nextOptional}} added for release 3.6.0.
>> 
>> Proposal: close this JIRA for now, see how {{nextOptional}} works out and
>> revisit {{orElse*}} based on experience.
>> 
>>> Add nextOrElse() method in ExtendedIterator
>>> ---
>>> 
>>>Key: JENA-1427
>>>URL: https://issues.apache.org/jira/browse/JENA-1427
>>>Project: Apache Jena
>>> Issue Type: Improvement
>>> Components: Core
>>>   Affects Versions: Jena 3.5.0
>>>   Reporter: Adam Jacobs
>>>   Priority: Trivial
>>> Labels: easytask
>>> 
>>> Allow a functional approach for returning a default value or throwing a
>> custom exception from a Jena iterator.
>>> The following method may be added to the ExtendedIterator interface.
>>> {noformat}
>>>/**
>>> Answer the next object, if it exists, otherwise invoke the
>> _supplier_.
>>> */
>>>public default T nextOrElse( Supplier supplier ) {
>>>return hasNext() ? next() : supplier.get();
>>>}
>>> {noformat}
>> 
>> 
>> 
>> --
>> This message was sent by Atlassian JIRA
>> (v6.4.14#64029)
>> 
> 
> 
> 
> -- 
> I like: Like Like - The likeliest place on the web
> 
> LinkedIn: http://www.linkedin.com/in/claudewarren



[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-06 Thread A. Soroka (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16280215#comment-16280215
 ] 

A. Soroka commented on JENA-1427:
-

+1

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


Re: [jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-06 Thread Claude Warren
my reading of this is that nextOptional will return null at the end of the
iteration and that this is indistinguishable from a null in the iteration
unless hasNext() is called.  but calling hasNext() after a null only tells
you if the next item is present not if the last item was present, thus a
null at the end of the iteration may be lost.  It almost seems like you
need a lastRead() method to retrieve the last read object again.  It seems
like this construct will only be useful when there  are no nulls in the
iteration.

Claude

On Mon, Dec 4, 2017 at 3:59 PM, Andy Seaborne (JIRA) 
wrote:

>
> [ https://issues.apache.org/jira/browse/JENA-1427?page=
> com.atlassian.jira.plugin.system.issuetabpanels:comment-
> tabpanel=16276983#comment-16276983 ]
>
> Andy Seaborne commented on JENA-1427:
> -
>
> {{nextOptional}} added for release 3.6.0.
>
> Proposal: close this JIRA for now, see how {{nextOptional}} works out and
> revisit {{orElse*}} based on experience.
>
> > Add nextOrElse() method in ExtendedIterator
> > ---
> >
> > Key: JENA-1427
> > URL: https://issues.apache.org/jira/browse/JENA-1427
> > Project: Apache Jena
> >  Issue Type: Improvement
> >  Components: Core
> >Affects Versions: Jena 3.5.0
> >Reporter: Adam Jacobs
> >Priority: Trivial
> >  Labels: easytask
> >
> > Allow a functional approach for returning a default value or throwing a
> custom exception from a Jena iterator.
> > The following method may be added to the ExtendedIterator interface.
> > {noformat}
> > /**
> >  Answer the next object, if it exists, otherwise invoke the
> _supplier_.
> >  */
> > public default T nextOrElse( Supplier supplier ) {
> > return hasNext() ? next() : supplier.get();
> > }
> > {noformat}
>
>
>
> --
> This message was sent by Atlassian JIRA
> (v6.4.14#64029)
>



-- 
I like: Like Like - The likeliest place on the web

LinkedIn: http://www.linkedin.com/in/claudewarren


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-04 Thread Andy Seaborne (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16276983#comment-16276983
 ] 

Andy Seaborne commented on JENA-1427:
-

{{nextOptional}} added for release 3.6.0.

Proposal: close this JIRA for now, see how {{nextOptional}} works out and 
revisit {{orElse*}} based on experience.

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-04 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16276959#comment-16276959
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user ajs6f commented on the issue:

https://github.com/apache/jena/pull/323
  
Okay, that works for me!


> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-04 Thread ASF subversion and git services (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16276956#comment-16276956
 ] 

ASF subversion and git services commented on JENA-1427:
---

Commit e512f51b10c479fa8fb24573d7b9112cde8e0fda in jena's branch 
refs/heads/master from [~andy.seaborne]
[ https://git-wip-us.apache.org/repos/asf?p=jena.git;h=e512f51 ]

JENA-1427: Merge commit 'refs/pull/323/head' of github.com:apache/jena

This closes #323.


> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-04 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16276957#comment-16276957
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user asfgit closed the pull request at:

https://github.com/apache/jena/pull/323


> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-12-04 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16276953#comment-16276953
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user afs commented on the issue:

https://github.com/apache/jena/pull/323
  
That was already the case in the PR and I've added text to call it out 
explicitly into the javadoc.


> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-30 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16273327#comment-16273327
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user ajs6f commented on the issue:

https://github.com/apache/jena/pull/323
  
That's my point-- I don't think you can do a good fluent API for this 
without narrowing the `Iterator` contract to exclude `null`s, because you can't 
distinguish. We can do that (narrow the contact)-- we own `ExtendedIterator` 
and as you say, we aren't pushing `null`s through it for the `Model` API.

So my vote is that we do what you have here, but _also_ add a note to 
`ExtendedIterator` guaranteeing that `next()` must not return `null`.


> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-30 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16273073#comment-16273073
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user afs commented on the issue:

https://github.com/apache/jena/pull/323
  
This does not make sense to me - how can one tell from 
`nextOptional().orElseThrow()` when at the end without having to drop out of 
fluent? (or functional `first` is fluent in a functional sense).

That's why I banned nulls from iterators when using `nextOptional`; I don't 
know of a case where `ExtendedIterator` can return nulls when used in the model 
API. 

If `nextOptional` can have two different uses, we would be better off with 
`nextOr*` which do differentiate the two cases.



> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-30 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16273015#comment-16273015
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user ajs6f commented on the issue:

https://github.com/apache/jena/pull/323
  
I'm saying we don't do `orElseThrow` or any other `orElseX`, we leave that 
to `nextOptional().orElseThrow()`, etc. Then at iterator end, `nextOptional()` 
returns `empty` forever. If there are `null`s, it returns `empty`. If you want 
to know whether that is because there are many `null`s or because the iterator 
is done, you call `hasNext()`.

Another way to put it: we shouldn't try to eliminate needing `hasNext()` 
with this-- if we want to do that, we should do something like 
`Spliterator::tryAdvance`, which is a whole different story. You can se how 
that would work a little with `Iterator::forEachRemaining`. You don't need 
`hasNext()` with that.


> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-30 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16272931#comment-16272931
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user afs commented on the issue:

https://github.com/apache/jena/pull/323
  
This seems to be arguing for NoSuchElementException for end of iterator and 
Optional.empty for null?

(There needs to be a way to distinguish end-of-iterator from 
iterator-returns-null.)




> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-30 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16272757#comment-16272757
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user ajs6f commented on the issue:

https://github.com/apache/jena/pull/323
  
Do we want to return `empty` for `null`? I ask because the "ergonomics" are 
different. The way we have it now:
```
try {
iterator.nextOptional().ifPresent(this::doStuff);
}
catch (NullPointerException e) { worry about that; }
```
vs. just `iterator.nextOptional().ifPresent(this::doStuff);` where 
`doStuff` is responsible for testing for `null`. I actually like the latter 
because `null` might be a legitimate value, but I may be misunderstanding the 
contract of `ExtendedIterator`-- is it guaranteed that `next()` never returns 
`null`? Is `null` actually not ever legit?



> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-30 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16272511#comment-16272511
 ] 

ASF GitHub Bot commented on JENA-1427:
--

Github user afs commented on the issue:

https://github.com/apache/jena/pull/323
  
Just the `nextOptional` part for JENA-1427.

Adding the other methods discussed there continues.



> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-30 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16272510#comment-16272510
 ] 

ASF GitHub Bot commented on JENA-1427:
--

GitHub user afs opened a pull request:

https://github.com/apache/jena/pull/323

JENA-1427: ExtendedIterator.nextOptional



You can merge this pull request into a Git repository by running:

$ git pull https://github.com/afs/jena next-optional

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/jena/pull/323.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #323


commit 1a8e270fc48b5b5b3eefe24314fb75174723ab43
Author: Andy Seaborne 
Date:   2017-11-30T10:46:41Z

JENA-1427: ExtendedIterator.nextOptional




> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-23 Thread A. Soroka (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16264622#comment-16264622
 ] 

A. Soroka commented on JENA-1427:
-

I'd be very much inclined to just {{nextOptional}}. It's simple and it reuses a 
standard type which is already familiar to users. It provides all the 
functionality in that type, so or-else and more.

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-22 Thread Adam Jacobs (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16263362#comment-16263362
 ] 

Adam Jacobs commented on JENA-1427:
---

Sounds great to me. Whether integrating with Java's {{Optional}} or 
implementing equivalent functionality, either way will work.

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-22 Thread Andy Seaborne (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16263306#comment-16263306
 ] 

Andy Seaborne commented on JENA-1427:
-

If we add to {{ExtendedIterator}}, these are the possible operations:

* {{nextOptional}}
* {{nextOrElse}}
* {{nextOrElseGet}}
* {{nextOrElseThrow}}

where the {{nextOrElse*}} are functionally like {{nextOptional().orElse*}} 
methods (all as default methods, the {{nextOrElse*}} not implemented as 
"optional.orElse").

with an open question about whether the {{nextOrElse*}} are really necessary 
(albeit shorter).

This seems like relatively low risk extension to the API. While generally, I am 
nervous about tinkering with the main API because changes/additions are usually 
opinionated, hence implicitly saying "and don't extend ", 
these operations don't fall into this category.

Would that work?


> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread A. Soroka (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257432#comment-16257432
 ] 

A. Soroka commented on JENA-1427:
-

That's true (and the thing I want most from Java is that it should be Scala! :) 
), but it's important not to let syntax override semantics. We're already 
talking on the dev@ list (thanks in part to your question about immutability!) 
about a potential new API. It might be better to put this effort into that 
work. As [~andy.seaborne] points out above, it's a lot harder to add to an API 
that has been out in the wild for as long as Jena's has than to start from a 
clean field.

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread Adam Jacobs (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257388#comment-16257388
 ] 

Adam Jacobs commented on JENA-1427:
---

Utility methods don't enable the same fluent style of functional programming as 
what {{ExtendedIterator}} offers today...

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread A. Soroka (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257380#comment-16257380
 ] 

A. Soroka commented on JENA-1427:
-

You can definitely convert any {{Iterator}} into a {{Stream}} client-side, and 
Jena even has convenience methods therefor: 
{{org.apache.jena.atlas.iterator.Iter.asStream(Iterator)}}. 

But an {{Iterator}} isn't a {{Stream}} and they should be distinguished pretty 
clearly.

(This took me a long time to understand, and really I had to read what Brian 
Goetz wrote 
[here|https://www.ibm.com/developerworks/library/j-java-streams-3-brian-goetz/index.html]
 and then [~andy.seaborne] had to beat it into my head. The sentence (from that 
article) "When the terminal operation is initiated, the stream implementation 
picks an execution plan." speaks volumes about the difference in these 
abstractions. An {{Iterator}} generally doesn't "pick an execution plan".)

An {{Iterator}} is a source of inputs, a {{Stream}} is a _composition of 
functions_ attached to a source of inputs. If you don't believe me, take a look 
at the difference between something {{java.util.AbstractList.Itr}} and 
something like {{java.util.stream.ReferencePipeline}}.

I really think this kind of functionality might best be left client-sidel but 
maybe you can send a PR with what you'd like and we can get more eyes on it?

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread Andy Seaborne (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257367#comment-16257367
 ] 

Andy Seaborne commented on JENA-1427:
-

Changing {{Iter.first}} (and {{Iter.last}}) look reasonable to me.

{noformat}
RDFNode object = first(model.listObjectsOfProperty(subject, predicate))
  .orElseThrow(() -> new IllegalStateException("No objects 
found for subject and predicate..."));
{noformat}

I am nervous of adding to {{ExtendedIterator}} piecemeal as it is in the main 
public API.


> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread Adam Jacobs (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257356#comment-16257356
 ] 

Adam Jacobs commented on JENA-1427:
---

One more thought, rather than adding more functional methods to 
{{ExtendedIterator}}, perhaps give it the ability to convert into a Java 
{{Stream}}.
https://stackoverflow.com/questions/24511052/how-to-convert-an-iterator-to-a-stream
{noformat}
public default Stream toStream() {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(this, 
Spliterator.ORDERED), false);
}
{noformat}

Or give ExtendedIterator all of these methods!  :)

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread A. Soroka (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257353#comment-16257353
 ] 

A. Soroka commented on JENA-1427:
-

At this point, most compilers factor {{Optional}} into {{null}}checks, so the 
GC hit isn't worrisome.

I still don't understand the design here. Sometimes the new method will change 
the state of the iterator, but there is no way to tell. 

Perhaps a pattern more like {{java.util.Spliterator.tryAdvance(Consumer)}}?

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread Adam Jacobs (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257346#comment-16257346
 ] 

Adam Jacobs commented on JENA-1427:
---

{{Optional}} does add an extra object and an extra call in the functional chain.
{noformat}
RDFNode object = model.listObjectsOfProperty(subject, 
predicate).nextOptional().orElseThrow(() -> new IllegalStateException("No 
objects found for subject and predicate..."));
{noformat}

But it would still solve my use case. Perhaps this is a better method for 
ExtendedIterator?
{noformat}
public default Optional nextOptional() {
return hasNext() ? Optional.of(next()) : Optional.empty();
}
{noformat}

I wouldn't mind having both methods!  :)

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread A. Soroka (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257303#comment-16257303
 ] 

A. Soroka commented on JENA-1427:
-

This seems like it could be a twist on the method 
{{org.apache.jena.atlas.iterator.Iter.first(Iterator)}}, something like:

{code}
/** Return the first element of an iterator if available.
 * @param iter
 * @return An item if present.
 */
public static  Optional first(Iterator iter) {
return Optional.ofNullable(first(iter));
}
{code}

It definitely sounds like you are reaching for {{Optional}}.

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread Adam Jacobs (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257292#comment-16257292
 ] 

Adam Jacobs commented on JENA-1427:
---

The use case would be to consume only one element (or none) from the iterator.

{noformat}
RDFNode object = model.listObjectsOfProperty(subject, predicate).nextOrElse(() 
-> {throw new IllegalStateException("No objects found for subject and 
predicate...");});
{noformat}

Often I have an {{ExtendedIterator}} as a result of one of the list...() 
methods on {{Model}} (listObjects, listResources, etc.).
I expect one matching element to a query. If no element is present, I would 
like to throw a custom exception; otherwise, consume the element.
More rarely, there is a sensible default value in case no elements are found. 
If multiple elements match the query, I do not need more than one. 
Occasionally, multiple elements could be considered an exception as well; but 
most often they do not matter.

Writing this comment made me realize that a {{Model}} method which takes a SPO 
and returns an {{Optional}} might solve this case too; but there are so many 
variations of list methods on Model, to provide a version of each one that 
returns Optional.

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (JENA-1427) Add nextOrElse() method in ExtendedIterator

2017-11-17 Thread A. Soroka (JIRA)

[ 
https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16257220#comment-16257220
 ] 

A. Soroka commented on JENA-1427:
-

Maybe I'm misunderstanding the use case here, but if this method is used 
instead of {{next()}}, how will the client of {{ExtendedIterator}} know when to 
stop consuming?

> Add nextOrElse() method in ExtendedIterator
> ---
>
> Key: JENA-1427
> URL: https://issues.apache.org/jira/browse/JENA-1427
> Project: Apache Jena
>  Issue Type: Improvement
>  Components: Core
>Affects Versions: Jena 3.5.0
>Reporter: Adam Jacobs
>Priority: Trivial
>  Labels: easytask
>
> Allow a functional approach for returning a default value or throwing a 
> custom exception from a Jena iterator.
> The following method may be added to the ExtendedIterator interface.
> {noformat}
> /**
>  Answer the next object, if it exists, otherwise invoke the 
> _supplier_.
>  */
> public default T nextOrElse( Supplier supplier ) {
> return hasNext() ? next() : supplier.get();
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)