Source:
<code>public void awaitCompletion() { logger.trace("output endpoint running endpoint={} work={}", getEndpointPath(), getWorkUnit()); setPhase(WorkPhase.RUNNING); SessionState session = allowsSession() ? getEndpoint().getCaller().newSessionState() : null; calling: while (true) { Stream<InputStream> output = null; try { logger.trace("output endpoint={} count={} state={}", getEndpointPath(), getCallCount(), getEndpointState()); output = getEndpoint().getCaller().call( getEndpoint().getClient(), getEndpointState(), session, getWorkUnit() ); incrementCallCount(); } catch(Throwable throwable) { throw new RuntimeException("error while calling "+getEndpoint().getEndpointPath(), throwable); } if (allowsEndpointState()) { <START> if(output!= null && output.findFirst()!= null && output.findFirst().isPresent()) { <END> InputStream[] result = output.toArray(size -> new InputStream[size]); setEndpointState(result[0]); for(int i=1; i<result.length; i++) { forEachOutput((Consumer<InputStream>) result[i]); } } } switch(getPhase()) { case INTERRUPTING: setPhase(WorkPhase.INTERRUPTED); logger.info("output interrupted endpoint={} count={} work={}", getEndpointPath(), getCallCount(), getWorkUnit()); break calling; case RUNNING: if (output == null) { setPhase(WorkPhase.COMPLETED); logger.info("output completed endpoint={} count={} work={}", getEndpointPath(), getCallCount(), getWorkUnit()); break calling; } case INTERRUPTED: case COMPLETED: throw new IllegalStateException( "cannot process more output as current phase is " + getPhase().name()); default: throw new MarkLogicInternalException( "unexpected state for "+getEndpointPath()+" during loop: "+getPhase().name() ); } } }</code><technical_language>findFirst() is a terminal operation completely consumes stream. work perform tests in steps? is, if output is null, construct array. Then, if array is empty, process it</technical_language>

Target:
public void awaitCompletion() { if(outputConsumer == null) throw new IllegalStateException("Consumer is null."); logger.trace("output endpoint running endpoint={} work={}", getEndpointPath(), getWorkUnit()); if(getPhase() != WorkPhase.INITIALIZING) { throw new IllegalStateException( "Cannot process output since current phase is " + getPhase().name()); } setPhase(WorkPhase.RUNNING); SessionState session = allowsSession() ? getEndpoint().getCaller().newSessionState() : null; calling: while (true) { Stream<InputStream> output = null; try { logger.trace("output endpoint={} count={} state={}", getEndpointPath(), getCallCount(), getEndpointState()); output = getEndpoint().getCaller().call( getEndpoint().getClient(), getEndpointState(), session, getWorkUnit() ); incrementCallCount(); } catch(Throwable throwable) { throw new RuntimeException("error while calling "+getEndpoint().getEndpointPath(), throwable); } if(output != null) { InputStream[] result = output.toArray(size -> new InputStream[size]); if (allowsEndpointState() && result.length>0) { setEndpointState(result[0]); } for(int i= allowsEndpointState()?1:0; i<result.length; i++) { outputConsumer.accept(result[i]); } } switch(getPhase()) { case INTERRUPTING: setPhase(WorkPhase.INTERRUPTED); logger.info("output interrupted endpoint={} count={} work={}", getEndpointPath(), getCallCount(), getWorkUnit()); break calling; case RUNNING: if (output == null) { setPhase(WorkPhase.COMPLETED); logger.info("output completed endpoint={} count={} work={}", getEndpointPath(), getCallCount(), getWorkUnit()); break calling; } case INTERRUPTED: case COMPLETED: throw new IllegalStateException( "cannot process more output as current phase is " + getPhase().name()); default: throw new MarkLogicInternalException( "unexpected state for "+getEndpointPath()+" during loop: "+getPhase().name() ); } } }