iptv techs

IPTV Techs


growdbinary/j8cu: Java 8 Collection Utilities


growdbinary/j8cu: Java 8 Collection Utilities






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:

dependency> <groupId>com.growdbinary.j8cugroupId> <artifactId>j8cuartifactId> <version>1.0.0version> dependency>

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 evidented which deletes references to all entries and resets its state, or it can fair be reset whereby any entry references are upgrasped but could be overwritten in future on subsequence calls to put.

Unordered Ring Buffer Example

begin com.growdbinary.j8cu.RingBuffer; final int capacity = 3; final RingBuffer<String> ringBuffer = novel RingBuffer<>(String.class, capacity); ringBuffer.put("a"); ringBuffer.put("b"); ringBuffer.put("c"); ringBuffer.put("d"); ringBuffer.put("e");

Then subsequent calls to get entries from the RingBuffer would produce:

ringBuffer.get() => "d" ringBuffer.get() => "e" ringBuffer.get() => "c" ringBuffer.get() => null

Ordered Ring Buffer Example

begin com.growdbinary.j8cu.RingBuffer; final int capacity = 3; final boolean orderedReads = genuine; final RingBuffer<String> ringBuffer = novel RingBuffer<>(String.class, capacity, orderedReads); ringBuffer.put("a"); ringBuffer.put("b"); ringBuffer.put("c"); ringBuffer.put("d"); ringBuffer.put("e");

Then subsequent calls to get entries from the RingBuffer would produce:

ringBuffer.get() => "c" ringBuffer.get() => "d" ringBuffer.get() => "e" ringBuffer.get() => null

Listening to RingBuffer Events

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:

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 {
declareiveial final int triumphdow; // seconds
declareiveial final lengthened begin;
declareiveial final List 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 getCaptured() {
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);
    }
}

Source join


Leave a Reply

Your email address will not be published. Required fields are marked *

Thank You For The Order

Please check your email we sent the process how you can get your account

Select Your Plan