Java Stream OSID Pattern

A tool to bridge OsidLists to Java iteration and streaming.

Overview

Converts OsidList to Java Iterators to bridge OsidLists with the Java streaming API. This tool implements Iterator, Iterable, and Spliterator over the element type.

Because OsidLists are I/O streams and not internal Collections, checked OperationFailedException exceptions from the OsidList provider are snaked through the stream. You should use OsidStreams.go() at the boundary to unsnake and retrieve the original OsidException to continue on your merry OSID compliant way.

Example

List<Grade> getGoodGrades(GradeList grades) throws OperationFailedException { List<Grade> good = OsidStreams.go(() -> GradeIterator.stream(grades) .filter(grade -> grade.getDisplayName() .getText().startsWith("A")) .collect(Collectors.toList()) ); return (good); }

The above example illustrates the use of the tool and not so much the lazy plopping of results into a Java List. There is always a design issue when you have to get in the way and buffer versus keeping the stream flowing up the stack. But sometimes it can't be helped.

Javadoc Reference

net.okapia.osid.torrefacto.streams

Implementation Coverage

An implementation exists for every org.osid.OsidList.

Class Declaration Pattern

public final class OsidObjectIterator
extends Object
implements Iterator<OsidObject>, Iterable<OsidObject>, Spliterator<OsidObject>

Constructor Pattern

OsidObjectIterator(OsidObjectList list)
ParameterTypeDescription
list OsidObjectList the OsidList to iterate

Static Factory Method Pattern

static Stream<OsidObject> stream(OsidObjectList list)
ParameterTypeDescription
list OsidObjectList the OsidList to stream