T
- any kind of objectpublic class SlidingWindow<T>
extends java.lang.Object
implements java.io.Serializable
This class offers a easy to use sliding window implementation. The size of the window is fixed
and can't be modified after initializing the SlidingWindow
. You can insert elements
of the same type of objects into the window. This objects may vary in size (e.g. if you want to
save space by adding only new elements to the window whose values vary at least a given
threshold, you would add some kind of summary for all the elements that can be covered using one
value. In this case the element's size would be total number of elements that fall into the
summary).
Because of moving the borders is left to the user this implementation can be used for managing
windows that age per time unit as well as per seen element.
Constructor and Description |
---|
SlidingWindow(java.lang.Integer windowSize)
The window size is fixed after initiating an instance of
SlidingWindow |
Modifier and Type | Method and Description |
---|---|
void |
add(T element)
Adds a new element to the window.
|
void |
add(T element,
java.lang.Integer size)
Adds a new element to the window.
|
T |
get(java.lang.Integer index)
Returns the element at position
index |
java.util.Collection<T> |
getAll()
Returns a collection containing each element in the window
|
java.util.Collection<java.lang.Integer> |
getAllLifeTimes()
Returns a collection containing all elements' life times.
|
java.lang.Integer |
getLifeTime(java.lang.Integer index)
Returns the remaining life time of the specified element.
|
T |
getNewestElement()
Returns the newest element in the window.
|
T |
getOldestElement()
Returns the oldest element in the window.
|
java.lang.Integer |
getSize(java.lang.Integer index)
Returns the element's size at position
index |
java.lang.Integer |
getWindowSize()
Returns the size of this sliding window.
|
boolean |
isEmpty()
Checks whether the
SlidingWindow is empty |
void |
slideWindow(java.lang.Integer positions)
Moves the window a given number of positions.
|
void |
slideWindowByOnePosition()
Just moves the border of the window by one position.
|
public SlidingWindow(java.lang.Integer windowSize)
SlidingWindow
windowSize
- a (positive) Integer
value.public void add(T element)
element
- public void add(T element, java.lang.Integer size)
element
- size
- - Integer
value that specifies the element
s sizejava.lang.RuntimeException
- if size
is greater than windowSize
or a negative
Integer
public void slideWindowByOnePosition()
public void slideWindow(java.lang.Integer positions)
positions
- - a Integer
value representing the elapsed "time"java.lang.RuntimeException
- if positions
is a negative valuepublic T get(java.lang.Integer index)
index
index
- public T getOldestElement()
public T getNewestElement()
public java.util.Collection<T> getAll()
Collection
of element of type Tpublic java.util.Collection<java.lang.Integer> getAllLifeTimes()
Collection
of element of type Integer
public java.lang.Integer getLifeTime(java.lang.Integer index)
index
- public java.lang.Integer getWindowSize()
Integer
that stands for the size of the windowpublic java.lang.Integer getSize(java.lang.Integer index)
index
index
- Integer
valuepublic boolean isEmpty()
SlidingWindow
is emptytrue
if the SlindingWindow
contains any element or
false
if at least one element in the window is active.