All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.serialized.client.feed.InMemorySequenceNumberTracker Maven / Gradle / Ivy

The newest version!
package io.serialized.client.feed;

import org.apache.commons.lang3.Validate;

import java.util.concurrent.atomic.AtomicLong;

/**
 * Default, in-memory, implementation of the sequence number tracker.
 * Will keep the state of which feed entries have been consumed.
 */
public class InMemorySequenceNumberTracker implements SequenceNumberTracker {

  private final AtomicLong sequenceNumber;

  public InMemorySequenceNumberTracker() {
    this.sequenceNumber = new AtomicLong(0);
  }

  public InMemorySequenceNumberTracker(long initialValue) {
    this.sequenceNumber = new AtomicLong(initialValue);
  }

  @Override
  public long lastConsumedSequenceNumber() {
    return sequenceNumber.longValue();
  }

  @Override
  public void updateLastConsumedSequenceNumber(long sequenceNumber) {
    Validate.isTrue(sequenceNumber >= 0,
        "Illegal sequenceNumber [%d] - last consumed sequence number cannot be negative", sequenceNumber);

    Validate.isTrue(sequenceNumber > this.sequenceNumber.get(),
        "Illegal sequenceNumber [%d] - last consumed sequence number must be greater than current", sequenceNumber);

    this.sequenceNumber.set(sequenceNumber);
  }

  @Override
  public void reset() {
    sequenceNumber.set(0);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy