Class AbstractRemoteIterator<S,T>
- java.lang.Object
-
- de.aristaflow.adept2.model.common.collection.AbstractRemoteIterator<S,T>
-
- Type Parameters:
S- The source type of the provided elements, which is subject of transformation before returning them.T- The type of the elements that is provided when iterating.
- All Implemented Interfaces:
RemoteIterator<List<T>>,de.aristaflow.adept2.model.communication.ServerSideProxy,Closeable,AutoCloseable
- Direct Known Subclasses:
AbstractTimeTrackingRemoteIterator,TransformingRemoteIterator
public abstract class AbstractRemoteIterator<S,T> extends Object implements RemoteIterator<List<T>>
This class provides the basic means for a window mechanism for lists of elements, that is, the basic means for aRemoteIterator. It provides refusal of accessing a closed remote iterator, basic active thread tracking, transformation and tracking of returned elements and tracking ofAutoCloseableelements. Such elements can either be closed on traversal or when closing the remote iterator.closeOnTraversal()willAutoCloseable.close()all elements of the previous traversal (call togetNext(int),getNext(int, int)orgetPrevious(int)). This reduces the amount of memory required to keep this elements for closing. But it prevents re-accessing these elements. If the elements are created for every traversal, this is no problem, if they are provided via memory, closing needs to be deferred.
WhileAutoCloseable.close()onclose()is the default, subclasses may choose to close on traversal (useful forRemoteIterator.isForwardOnly()and iterators creating new elements when traversing) or even to disable closing at all. The latter is required for remote iterators that cache already accessed elements and re-provide them when traversing back.AutoCloseableelements will be wrapped so that the using classes cannot close the retrieved elements outside of this remote iterator.- See Also:
CloseIgnoringWrapper
-
-
Field Summary
Fields Modifier and Type Field Description protected Cleanup<IOException>cleanupThe clean-up for closing thecloseablesas post-mortem task for this instance or when closing.protected Collection<AutoCloseable>closeablesThe elements that areAutoCloseablethat should be closed at the next traversal or when closing.protected LoggerloggerThe logger for logging messages instead of throwing exceptions.protected Function<S,T>transformatorThe transformator function applied to each individual object before retrieval.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractRemoteIterator(Function<S,T> transformator, Logger logger)Creates a new remote iterator with the designated transformator function applied to each element before returning it and using the designated logger for problems when closing (this remote iterator or the appropriate elements).
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected List<T>aboutToReturn(List<S> elems)Tracks theAutoCloseables of the designated elements before returning them, handles their closing (whencloseOnTraversal) and applies the transformation.voidclose()Explicitly closes this iterator which allows to release occupied memory and resources.booleancloseOnTraversal()Gets whether this remote iterator closes elements ofAutoCloseabletype when traversing to the next elements.protected voidenter()Allows to track entering a method reentrant.protected Longexit()Tracks exiting a method reentrant.List<T>getAll()Gets all elements of this iterator at once.protected abstract List<S>getAllImpl()ImplementsgetAll()without the need to consider result transformation, closeable and alive time tracking.List<T>getNext(int count)Gets the next specified amount of elements of this iterator.List<T>getNext(int count, int start)Gets the next specified amount of elements of this iterator starting at the designated index (0-based).protected abstract List<S>getNextImpl(int count)ImplementsgetNext(int)without the need to consider result transformation, closeable and alive time tracking.protected abstract List<S>getNextImpl(int count, int start)ImplementsgetNext(int, int)without the need to consider result transformation, closeable and alive time tracking.List<T>getPrevious(int count)Gets the previous specified amount of elements of this iterator.protected abstract List<S>getPreviousImpl(int count)ImplementsgetPrevious(int)without the need to consider result transformation, closeable and alive time tracking.protected abstract booleanisClosed()Gets whether this remote iterator has been closed and its data is not available any more.protected voidsetCloseOnTraversal(Boolean closeOnTraversal)Sets whether closing of elements should be applied when traversing (Boolean.TRUE), when closing this remote iterator (Boolean.FALSE) or not at all (null).-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.aristaflow.adept2.model.common.collection.RemoteIterator
drop, getRemainingAliveTime, isForwardOnly, isSnapshot, keepAlive
-
-
-
-
Field Detail
-
logger
protected final Logger logger
The logger for logging messages instead of throwing exceptions.
-
transformator
protected final Function<S,T> transformator
The transformator function applied to each individual object before retrieval.
-
closeables
protected final Collection<AutoCloseable> closeables
The elements that areAutoCloseablethat should be closed at the next traversal or when closing.
-
cleanup
protected final Cleanup<IOException> cleanup
The clean-up for closing thecloseablesas post-mortem task for this instance or when closing.
-
-
Constructor Detail
-
AbstractRemoteIterator
protected AbstractRemoteIterator(Function<S,T> transformator, Logger logger)
Creates a new remote iterator with the designated transformator function applied to each element before returning it and using the designated logger for problems when closing (this remote iterator or the appropriate elements).- Parameters:
transformator- The transformator function applied to each individual object before retrieval.logger- The logger for logging messages instead of throwing exceptions.
-
-
Method Detail
-
enter
protected void enter()
Allows to track entering a method reentrant.
-
exit
protected Long exit()
Tracks exiting a method reentrant.- Returns:
- The possibly new remaining alive time in milliseconds or
nullin case the thread is still active and not exiting finally.
-
isClosed
protected abstract boolean isClosed()
Gets whether this remote iterator has been closed and its data is not available any more.- Returns:
- Whether this remote iterator has been closed and its data is not available any more.
-
getNextImpl
protected abstract List<S> getNextImpl(int count) throws DataSourceException, DataConsistencyException
ImplementsgetNext(int)without the need to consider result transformation, closeable and alive time tracking.
-
getNextImpl
protected abstract List<S> getNextImpl(int count, int start) throws DataSourceException, DataConsistencyException
ImplementsgetNext(int, int)without the need to consider result transformation, closeable and alive time tracking.
-
getPreviousImpl
protected abstract List<S> getPreviousImpl(int count) throws DataSourceException, DataConsistencyException
ImplementsgetPrevious(int)without the need to consider result transformation, closeable and alive time tracking.
-
getAllImpl
protected abstract List<S> getAllImpl() throws DataSourceException, DataConsistencyException
ImplementsgetAll()without the need to consider result transformation, closeable and alive time tracking.
-
aboutToReturn
protected List<T> aboutToReturn(List<S> elems)
Tracks theAutoCloseables of the designated elements before returning them, handles their closing (whencloseOnTraversal) and applies the transformation.- Parameters:
elems- The elements which are about to be returned in the current traversal and which may need to be tracked and/or transformed.- Returns:
- The designated elements transformed appropriately which are about to be returned in the current traversal.
-
setCloseOnTraversal
protected void setCloseOnTraversal(Boolean closeOnTraversal)
Sets whether closing of elements should be applied when traversing (Boolean.TRUE), when closing this remote iterator (Boolean.FALSE) or not at all (null).- Parameters:
closeOnTraversal-Boolean.TRUEfor immediate closing theAutoCloseableelements of the previous traversal,Boolean.FALSEfor closing theAutoCloseableelements when closing this remote iterator, ornullfor no element closing at all.
-
getNext
public final List<T> getNext(int count) throws DataSourceException, DataConsistencyException
Description copied from interface:RemoteIteratorGets the next specified amount of elements of this iterator. The returned data will be consistent within the returned list (but not necessarily with respect to previous or later retrievals) due to the usage of synchronisation.- Specified by:
getNextin interfaceRemoteIterator<S>- Parameters:
count- The amount of elements to retrieve in forward direction. This has to be > 0.- Returns:
- The next n elements of this iterator, where n is the specified count. In case of an
insufficient amount of next elements remaining only the available elements will be
returned. If there are no more next elements,
nullwill be returned.
AutoCloseables will be coupled to thisRemoteIterator, so do not close them; clone them if required longer than until automatic closing which may happen when traversing or when closing thisRemoteIterator. - Throws:
DataSourceException- If there are problems accessing the underlying data source, aDataSourceExceptionwill be thrown.DataConsistencyException- If there are problems retrieving any of the next elements via the identifying attribute of the elements (for instance the primary key of a specific element identifying the next position) is not available any more, aDataConsistencyExceptionwill be thrown.
-
getNext
public final List<T> getNext(int count, int start) throws DataSourceException, DataConsistencyException
Description copied from interface:RemoteIteratorGets the next specified amount of elements of this iterator starting at the designated index (0-based). The returned data will be consistent within the returned list (but not necessarily with respect to previous or later retrievals) due to the usage of synchronisation.
Afterwards the cursor to the current element will be set to the index start + count, that is, getNext(n) will be the same as calling getNext(n, start + count).- Specified by:
getNextin interfaceRemoteIterator<S>- Parameters:
count- The amount of elements to retrieve in forward direction. This has to be > 0.start- The index of the element from which to retrieve the next count elements. 0 is the index of the first element. This has to be >= 0 and it must not be bigger than the amounts of elements in this iterator.- Returns:
- The next n elements of this iterator, where n is the specified count. In case of an
insufficient amount of next elements remaining only the available elements will be
returned. If there are no more next elements,
nullwill be returned.
AutoCloseables will be coupled to thisRemoteIterator, so do not close them; clone them if required longer than until automatic closing which may happen when traversing or when closing thisRemoteIterator. - Throws:
DataSourceException- If there are problems accessing the underlying data source, aDataSourceExceptionwill be thrown.DataConsistencyException- If there are problems retrieving any of the next elements via the identifying attribute of the elements (for instance the primary key of a specific element identifying the next position) is not available any more, aDataConsistencyExceptionwill be thrown.
-
getPrevious
public final List<T> getPrevious(int count) throws DataSourceException, DataConsistencyException
Description copied from interface:RemoteIteratorGets the previous specified amount of elements of this iterator. The returned data will be consistent within the returned list (but not necessarily with respect to previous or later retrievals) due to the usage of synchronisation.- Specified by:
getPreviousin interfaceRemoteIterator<S>- Parameters:
count- The amount of elements to retrieve in backward direction. This has to be > 0.- Returns:
- The previous n elements of this iterator, where n is the specified count. The order
corresponds to the one in the underlying data structure, that is previous elements in
the returned list are also previous elements in the data structure. In case of an
insufficient amount of previous elements remaining only the available elements will be
returned. If there are no more previous elements,
nullwill be returned.
AutoCloseables will be coupled to thisRemoteIterator, so do not close them; clone them if required longer than until automatic closing which may happen when traversing or when closing thisRemoteIterator. - Throws:
DataSourceException- If there are problems accessing the underlying data source, aDataSourceExceptionwill be thrown.DataConsistencyException- If there are problems retrieving any of the previous next elements via the identifying attribute of the elements (for instance the primary key of a specific element identifying the next position) is not available any more or if this iterator is forward only, aDataConsistencyExceptionwill be thrown.
-
getAll
public final List<T> getAll() throws DataSourceException, DataConsistencyException
Description copied from interface:RemoteIteratorGets all elements of this iterator at once. The returned data will be consistent (does not contain elements of different versions) due to the usage of synchronisation. Note that calling this method may take a lot of time.
Calling this method will reset the iterator before the first element if this iterator is not forward only. If this iterator is forward only and does not cache retrieved elements internally, this method will not work but throw aDataSourceExceptionsince the already returned elements may not be retrieved any more.- Specified by:
getAllin interfaceRemoteIterator<S>- Returns:
- All elements of this iterator at once.
AutoCloseables will be coupled to thisRemoteIterator, so do not close them; clone them if required longer than until automatic closing which may happen when traversing or when closing thisRemoteIterator. - Throws:
DataSourceException- If there are problems accessing the underlying data source or this iterator is forward only and currently not positioned before the first element, aDataSourceExceptionwill be thrown.DataConsistencyException- If there are problems retrieving any of the elements via the identifying attribute of the elements (for instance the primary key of a specific element identifying the next position) is not available any more, aDataConsistencyExceptionwill be thrown.
-
closeOnTraversal
public boolean closeOnTraversal()
Description copied from interface:RemoteIteratorGets whether this remote iterator closes elements ofAutoCloseabletype when traversing to the next elements. If so, all elements of the previous traversal will be closed. That is, each call toRemoteIterator.getNext(int),RemoteIterator.getNext(int, int)orRemoteIterator.getPrevious(int)will cache all elements and close them with the next call to eitherRemoteIterator.getNext(int),RemoteIterator.getNext(int, int)orRemoteIterator.getPrevious(int). Otherwise the elements will be closed when closing this remote iterator.- Specified by:
closeOnTraversalin interfaceRemoteIterator<S>- Returns:
- Whether this remote iterator closes elements when traversing to the next elements.
-
close
public void close() throws IOExceptionDescription copied from interface:RemoteIteratorExplicitly closes this iterator which allows to release occupied memory and resources. Closing will be done implicitly after the alive time has elapsed.
This method may be called several times. Implementors have to take care of this.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceRemoteIterator<S>- Specified by:
closein interfacede.aristaflow.adept2.model.communication.ServerSideProxy- Throws:
IOException- If there are problems accessing the underlying data source, anIOExceptionwill be thrown.
-
-