[ https://issues.apache.org/jira/browse/JENA-1427?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=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<T> supplier ) { > return hasNext() ? next() : supplier.get(); > } > {noformat} -- This message was sent by Atlassian JIRA (v6.4.14#64029)