com.alipay.disruptor.Sequenced Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2011 LMAX Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.disruptor;
public interface Sequenced {
/**
* The capacity of the data structure to hold entries.
*
* @return the size of the RingBuffer.
*/
int getBufferSize();
/**
* Has the buffer got capacity to allocate another sequence. This is a concurrent
* method so the response should only be taken as an indication of available capacity.
*
* @param requiredCapacity in the buffer
* @return true if the buffer has the capacity to allocate the next sequence otherwise false.
*/
boolean hasAvailableCapacity(final int requiredCapacity);
/**
* Get the remaining capacity for this sequencer.
*
* @return The number of slots remaining.
*/
long remainingCapacity();
/**
* Claim the next event in sequence for publishing.
*
* @return the claimed sequence value
*/
long next();
/**
* Claim the next n events in sequence for publishing. This is for batch event producing. Using batch producing
* requires a little care and some math.
*
* int n = 10;
* long hi = sequencer.next(n);
* long lo = hi - (n - 1);
* for (long sequence = lo; sequence <= hi; sequence++) {
* // Do work.
* }
* sequencer.publish(lo, hi);
*
*
* @param n the number of sequences to claim
* @return the highest claimed sequence value
*/
long next(int n);
/**
* Attempt to claim the next event in sequence for publishing. Will return the
* number of the slot if there is at least requiredCapacity
slots
* available.
*
* @return the claimed sequence value
* @throws InsufficientCapacityException InsufficientCapacityException
*/
long tryNext() throws InsufficientCapacityException;
/**
* Attempt to claim the next n events in sequence for publishing. Will return the
* highest numbered slot if there is at least requiredCapacity
slots
* available. Have a look at {@link Sequencer#next()} for a description on how to
* use this method.
*
* @param n the number of sequences to claim
* @return the claimed sequence value
* @throws InsufficientCapacityException InsufficientCapacityException
*/
long tryNext(int n) throws InsufficientCapacityException;
/**
* Publishes a sequence. Call when the event has been filled.
*
* @param sequence sequence
*/
void publish(long sequence);
/**
* Batch publish sequences. Called when all of the events have been filled.
*
* @param lo first sequence number to publish
* @param hi last sequence number to publish
*/
void publish(long lo, long hi);
}