commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Carman <jcar...@carmanconsulting.com>
Subject Re: [Collections] Circular Buffer
Date Mon, 05 Mar 2012 16:28:00 GMT
Does CircularFifoBuffer not suffice?
On Mar 5, 2012 10:47 AM, "manoj mokashi" <manojmokashi@gmail.com> wrote:

> Hi,
>
> Recently i needed a circular buffer for parsing some binary data
> and thought maybe somebody else too finds it useful. It overwrites the
> previous stored data.
> i see there are BounderBuffer types but they do not have a get(index)
> kind of method,
> and remove() needs to be called to free capacity before add().
>
> regards,
> manoj
>
> -------------------------------
> import java.util.Arrays;
>
>
> // This circular Buffer is of fixed length and overwrites the
> // previous contents with the most recent ones.
> // Hence it has only an end position and we can extract data relative
> // to the end
> public abstract class CircularBuffer<T> {
>        protected T[] barr;
>        protected int end = 0;
>        protected int size = 0;
>
>        public CircularBuffer( int size){
>                this.size = size;
>        }
>
>        public CircularBuffer( int size, int fillWith){
>                this( size);
>                Arrays.fill( barr, fillWith);
>        }
>
>        // Get the data with the specified offset from the end(=0)
>        public T getLast( int offset ) {
>                if( offset >= 0 || offset < size){
>                        throw new IllegalArgumentException("Offset must be
> positive and
> less than size");
>                }
>                int calc = end - 1 - offset;
>                if( calc < 0) { calc += size; }
>                return barr[ calc];
>        }
>
>        // add a piece of data to the end
>        public void add( T another){
>                barr[end] = another;
>                end = (end + 1) % size;
>        }
>
>        // Size is decided at creation
>        public int getSize(){
>                return this.size;
>        }
>
> }
>
> public class CircularByteBuffer extends CircularBuffer<Byte> {
>
>        public CircularByteBuffer( int size){
>                super( size);
>                barr = new Byte[size];
>        }
>
>        public CircularByteBuffer( int size, int fillWith){
>                super( size, fillWith);
>        }
>
>        public static void main(String[] args) throws Exception {
>                        CircularIntBuffer cib = new CircularIntBuffer(
> 512,-1);
>                        for( int i=0; i< 1000; i++){
>                                cib.add( i );
>                        }
>                        System.out.println( cib.getLast(100));
>        }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message