Package com.pnfsoftware.jeb.util.collect
Class ItemHistory<T>
java.lang.Object
com.pnfsoftware.jeb.util.events.EventSource
com.pnfsoftware.jeb.util.collect.ItemHistory<T>
- Type Parameters:
T-
- All Implemented Interfaces:
IEventSource
Generic tracker of items, based on a simple history queue (get last, get next) model.
Typically, such objects are long-lived; internally, references to items are strong. Therefore, it is up to the user to make sure that referenced objects are not prevented from being garbage collected, should they be.
-
Field Summary
Fields inherited from class com.pnfsoftware.jeb.util.events.EventSource
onEventCallCount -
Constructor Summary
ConstructorsConstructorDescriptionCreate a history of 1 with a capacity of 100 andcleanHistoryAfterIndexset to true.ItemHistory(int capacity, boolean cleanHistoryAfterIndex) Create a history object with the -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd an item to the history.voidaddAll(Collection<T> items) intcapacity()Get the history maximum capacity.voidcapacity(int capacity) Update the history capacity.get()Retrieve a copy of the history items.Set the position of the History at a particular index.getNext()getPrevious(T updatedItem) booleanhasNext()booleanpeekNext()intposition()Get the current history index.booleanvoidreset()intsize()toString()Methods inherited from class com.pnfsoftware.jeb.util.events.EventSource
addListener, countListeners, getListeners, getParentSource, insertListener, notifyListeners, notifyListeners, notifyListeners, relay, removeListener, setParentSource
-
Constructor Details
-
ItemHistory
public ItemHistory()Create a history of 1 with a capacity of 100 andcleanHistoryAfterIndexset to true. -
ItemHistory
public ItemHistory(int capacity, boolean cleanHistoryAfterIndex) Create a history object with the- Parameters:
capacity- maximum capacity, must be at least 1cleanHistoryAfterIndex- if true, recording an element into the history when the history index is not at the top will erase all elements after the index and add the new item at the top; if false, the item is inserted at the top regardless of where the history index was
-
-
Method Details
-
reset
public void reset() -
size
public int size() -
capacity
public int capacity()Get the history maximum capacity.- Returns:
-
capacity
public void capacity(int capacity) Update the history capacity.- Parameters:
capacity-
-
position
public int position()Get the current history index. Navigating the history using getNext(), getPrevious(), or modifying the history using add(),remove(), etc. updates this pointer.- Returns:
-
add
Add an item to the history. Note that any next item may be removed (based on cleanHistoryAfterIndex constructor parameter, in particular, ifgetPrevious()method was called without restoring position)- Parameters:
item-
-
addAll
-
remove
-
getMostRecent
-
get
Retrieve a copy of the history items. The first item in the returned list is the oldest one. -
peekPrevious
-
peekNext
-
getPrevious
-
getPrevious
-
getAt
Set the position of the History at a particular index.- Parameters:
newIndex- The new index of the history- Returns:
- The current item at this index.
-
getNext
-
getNext
-
hasPrevious
public boolean hasPrevious() -
hasNext
public boolean hasNext() -
toString
-