org.mule.streaming
Class AbstractConsumer<T,P>

java.lang.Object
  extended by org.mule.streaming.AbstractConsumer<T,P>
Type Parameters:
T - the type that the consumer will return
P - the type that the producer generates
All Implemented Interfaces:
Closeable, Consumer<T>, ProvidesTotalHint
Direct Known Subclasses:
ListConsumer, SimpleConsumer

public abstract class AbstractConsumer<T,P>
extends Object
implements Consumer<T>

Base implementation of Consumer. This template class takes care of the contract's subtleties like returning fast when closed and throwing the correct types of exceptions

Since:
3.5.0

Field Summary
protected  Producer<P> producer
           
 
Constructor Summary
AbstractConsumer(Producer<P> producer)
           
 
Method Summary
protected abstract  boolean checkConsumed()
          Implement this method to actualy check for the Producer being fully consumed without worrying about it being closed or throwing any exceptions
 void close()
          Closes the resource.
 T consume()
          Retrieves the next available item.
protected abstract  T doConsume()
          Implement this method to actually consume the producer without worrying about exception types or checking fo this consumer to be closed.
 boolean isConsumed()
          Returns true if no more items are available or if the consumer was closed.
 int size()
          returns the total amount of items available for storage/processing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

producer

protected final Producer<P> producer
Constructor Detail

AbstractConsumer

public AbstractConsumer(Producer<P> producer)
Method Detail

doConsume

protected abstract T doConsume()
                        throws NoSuchElementException
Implement this method to actually consume the producer without worrying about exception types or checking fo this consumer to be closed. If the producer is consumed then simply return null

Returns:
a new item or null
Throws:
NoSuchElementException

checkConsumed

protected abstract boolean checkConsumed()
Implement this method to actualy check for the Producer being fully consumed without worrying about it being closed or throwing any exceptions

Returns:
whether the Producer has been consumed or not

consume

public final T consume()
                throws NoSuchElementException
Retrieves the next available item.

Specified by:
consume in interface Consumer<T>
Returns:
an object of type T if available. null otherwise
Throws:
NoSuchElementException

isConsumed

public final boolean isConsumed()
Returns true if no more items are available or if the consumer was closed. When this method returns true, implementors of this class are require to invoke the Closeable.close() method before returning in order to release resources as quickly as possible. Users of this component who no longer need this require before it is fully consumed are also required to close it.

Specified by:
isConsumed in interface Consumer<T>
Returns:
true if no more items are available. false otherwise

close

public void close()
           throws MuleException
Closes the resource. Calling this method is mandatory for any component using this instance once it finishes using it. This method should not throw exception is invoked on an instance that has already been closed

Specified by:
close in interface Closeable
Throws:
MuleException - if an exception occurs closing the resource

size

public int size()
returns the total amount of items available for storage/processing. In some scenarios, it might not be possible/convenient to actually retrieve this value or it might not be available at this point. -1 is returned in such a case.

Specified by:
size in interface ProvidesTotalHint


Copyright © 2003-2014 MuleSoft, Inc.. All Rights Reserved.