public class TimeSlidingWindow<E> extends Object
A structure to keep record of elements for a certain period of time. It supports concurrent access and has amortized time complexity to add and remove element of O(1) (each removal is prepaid while the element is added).
The window keeps time order of the elements and although it offers the possibility of providing artificial time, it still needs that the objects are added in a correct time order (older objects first).
The time window is closed on both ends. For example, a sliding window all length 500 at the current time 600 will have both objects for time 100 and 600.
Constructor and Description |
---|
TimeSlidingWindow(long windowLength)
Creates a new time sliding window of the given length in milliseconds.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E object)
Adds an object to the sliding window.
|
void |
add(E object,
long time)
Adds an object to the sliding window with an information about artificial time.
|
void |
clear()
Removes all elements from the sliding window.
|
void |
forEach(Consumer<E> action)
Performs the given action for each element of the sliding window
until all elements have been processed or the action throws an
exception.
|
void |
forEach(long currentTime,
Consumer<E> action)
Performs the given action for each element of the sliding window until all elements have been processed or the action throws an
exception.
|
void |
gc()
Performs a garbage collection of elements that are out of the time window.
|
void |
gc(long time)
Performs a garbage collection based on the provided time information.
|
long |
getLength()
Gets the length of the sliding window in milliseconds.
|
long |
size() |
public TimeSlidingWindow(long windowLength)
windowLength
- The length of the sliding window in milliseconds.public long getLength()
public void add(E object)
object
- Object to be added.public void add(E object, long time)
object
- The object to be added.time
- Artificial time when the object was added to the window.public void clear()
public long size()
public void forEach(Consumer<E> action)
Performs the given action for each element of the sliding window until all elements have been processed or the action throws an exception.
Only the elements in the valid time window are processed. All older elements are garbage collected.
action
- The action to be performed for each element.public void forEach(long currentTime, Consumer<E> action)
Performs the given action for each element of the sliding window until all elements have been processed or the action throws an exception.
Only the elements in the valid time window are processed. All older elements are garbage collected.
This action might not return all elements when 1) it was already called with a higher (later) current time, 2) forEach(Consumer)
was already called which actually propagated a higher (later) time to this method.
currentTime
- Artificial time to control the position of the sliding window.action
- The action to be performed for each element.public void gc()
forEach(java.util.function.Consumer<E>)
was not called for a long time.public void gc(long time)
time
- Current artificial time.Copyright © 2010–2017 PerfCake Community. All rights reserved.