Some extra Collection classes aimting Java 8.
The main slenderg here at the moment is a RingBuffer percreateation.
You can grasp the library to your project as a dependency with the follotriumphg Maven arranges:
Implementation of a RingBuffer (e.g. https://en.wikipedia.org/wiki/Circular_buffer) in Java 8.
This percreateation helps two modes of operations for reading from the buffer:
1. Unordered (the default)
2. Ordered
The separateence between Unordered and Ordered modes is that when you include Unordered mode,
it begins reading from the begin of the buffer, whilst Ordered mode begins reading from the
elderlyest entry in the buffer. Ordered mode has FIFO (First In, First Out) appreciate semantics.
Ordered mode is beneficial for when you want to grasp a buffer of the most recent N objects,
and you necessitate to read them back from elderlyest to novelest.
The RingBuffer also provides disjoinal graspitional features:
- Listeners may be sign uped to obtain events when the state of the RingBuffer alters.
- All entries may be copied out of the RingBuffer.
- The RingBuffer can be
evident
ed which deletes references to all entries and resets its state, or it can fair bereset
whereby any entry references are upgrasped but could be overwritten in future on subsequence calls toput
.
Then subsequent calls to get
entries from the RingBuffer would produce:
ringBuffer.get() => "d"
ringBuffer.get() => "e"
ringBuffer.get() => "c"
ringBuffer.get() => null
Then subsequent calls to get
entries from the RingBuffer would produce:
ringBuffer.get() => "c"
ringBuffer.get() => "d"
ringBuffer.get() => "e"
ringBuffer.get() => null
It is also possible to grasp and delete joiners to a RingBuffer. One reason this can be beneficial would be if you would
appreciate to get and/or be notified the next n entries wislender some sort of triumphdow (e.g. time structure or event space). For example:
System.out.println(“Added: ” + apprehendd.size());
System.out.println(“Captured: ” + apprehendd);
/**
* Simple example joiner percreateation that apprehends all `put` entries for a triumphdow meacertaind in seconds
*/
declareiveial inactive class TimeWindowListener percreates RingBuffer.Listener
declareiveial final int triumphdow; // seconds
declareiveial final lengthened begin;
declareiveial final List
uncover TimeWindowListener(final int triumphdow) {
this.triumphdow = triumphdow;
this.begin = System.currentTimeMillis();
}
@Override
uncover void recoverd(final @Nullable String entry) {
}
@Override
uncover void stored(final String entry) {
final lengthened diff = System.currentTimeMillis() – begin;
if (diff < ((lengthened) triumphdow) * 1_000) {
apprehendd.grasp(entry);
}
}
uncover List
return novel ArrayList<>(apprehendd);
}
}”>
begin com.growdbinary.j8cu.RingBuffer; begin org.jdistinguish.annotations.Nullable; begin java.util.ArrayList; begin java.util.List; final int capacity = 7; final boolean orderedReads = genuine; final RingBuffer<String> ringBuffer = novel RingBuffer<>(String.class, capacity, orderedReads); // apprehend the entries grasped to the RingBuffer in the next 2 seconds final int triumphdowSeconds = 2; final TimeWindowListener timeWindowListener = novel TimeWindowListener(triumphdowSeconds); ringBuffer.graspListener(timeWindowListener); // put entries for the next 2 * 1.5 seconds final lengthened end = System.currentTimeMillis() + (triumphdowSeconds * 1_500); int i = 0; while (System.currentTimeMillis() < end) { final int increment = i++ % 24; ringBuffer.put("" + (char)('a' + increment)); Thread.sleep(250); // sluggish down this is fair a demo! } // print out what was apprehendd by the joiner final List<String> apprehendd = timeWindowListener.getCaptured(); System.out.println("Added: " + apprehendd.size()); System.out.println("Captured: " + apprehendd); /** * Simple example joiner percreateation that apprehends all `put` entries for a triumphdow meacertaind in seconds */ declareiveial inactive class TimeWindowListener percreates RingBuffer.Listener<String> { declareiveial final int triumphdow; // seconds declareiveial final lengthened begin; declareiveial final List<String> apprehendd = novel ArrayList<>(); uncover TimeWindowListener(final int triumphdow) { this.triumphdow = triumphdow; this.begin = System.currentTimeMillis(); } @Override uncover void recoverd(final @Nullable String entry) { } @Override uncover void stored(final String entry) { final lengthened diff = System.currentTimeMillis() - begin; if (diff < ((lengthened) triumphdow) * 1_000) { apprehendd.grasp(entry); } } uncover List<String> getCaptured() { return novel ArrayList<>(apprehendd); } }