Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 
 package ch.qos.logback.core.helpers;
 
 import java.util.List;

CyclicBuffer holds values in a cyclic array.

It allows read access to any element in the buffer not just the first or last element.

Author(s):
Ceki Gülcü
 
 public class CyclicBuffer<E> {
 
   E[] ea;
   int first;
   int last;
   int numElems;
   int maxSize;

  
Instantiate a new CyclicBuffer of at most maxSize events. The maxSize argument must a positive integer.

Parameters:
maxSize The maximum number of elements in the buffer.
 
   public CyclicBuffer(int maxSizethrows IllegalArgumentException {
     if (maxSize < 1) {
       throw new IllegalArgumentException("The maxSize argument (" + maxSize
           + ") is not a positive integer.");
     }
     init(maxSize);
   }
 
   public CyclicBuffer(CyclicBuffer<E> other) {
    this. = other.maxSize;
     = (E[]) new Object[];
    System.arraycopy(other.ea, 0, this., 0, );
    this. = other.last;
    this. = other.first;
    this. = other.numElems;
   }
 
   @SuppressWarnings("unchecked")
   private void init(int maxSize) {
     this. = maxSize;
      = (E[]) new Object[maxSize];
      = 0;
      = 0;
      = 0;
   }

  
Clears the buffer and resets all attributes.
 
   public void clear() {
     init(this.);
   }

  
Add an event as the last event in the buffer.
 
   public void add(E event) {
     [] = event;
     if (++ == )
        = 0;
 
     if ( < )
       ++;
     else if (++ == )
        = 0;
   }

  
Get the ith oldest event currently in the buffer. If i is outside the range 0 to the number of elements currently in the buffer, then null is returned.
 
   public E get(int i) {
     if (i < 0 || i >= )
       return null;
 
    return [( + i) % ];
  }
  public int getMaxSize() {
    return ;
  }

  
Get the oldest (first) element in the buffer. The oldest element is removed from the buffer.
  public E get() {
    E r = null;
    if ( > 0) {
      --;
      r = [];
      [] = null;
      if (++ == )
         = 0;
    }
    return r;
  }
  
  public List<E> asList() {
    List<E> tList = new ArrayList<E>();
    for(int i = 0; i < length(); i++) {
      tList.add(get(i));
    }
    return tList;
  }

  
Get the number of elements in the buffer. This number is guaranteed to be in the range 0 to maxSize (inclusive).
  public int length() {
    return ;
  }

  
Resize the cyclic buffer to newSize.

Throws:
java.lang.IllegalArgumentException if newSize is negative.
  @SuppressWarnings("unchecked")
  public void resize(int newSize) {
    if (newSize < 0) {
      throw new IllegalArgumentException("Negative array size [" + newSize
          + "] not allowed.");
    }
    if (newSize == )
      return// nothing to do
    // 
    E[] temp = (E[]) new Object[newSize];
    int loopLen = newSize <  ? newSize : ;
    for (int i = 0; i < loopLeni++) {
      temp[i] = [];
      [] = null;
      if (++ == )
         = 0;
    }
     = temp;
     = 0;
     = loopLen;
     = newSize;
    if (loopLen == newSize) {
       = 0;
    } else {
       = loopLen;
    }
  }
New to GrepCode? Check out our FAQ X