org.javasimon.callback.lastsplits
Class CircularList<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<T>
          extended by org.javasimon.callback.lastsplits.CircularList<T>
All Implemented Interfaces:
Iterable<T>, Collection<T>, List<T>

public class CircularList<T>
extends AbstractList<T>

Ring/circular buffer, fixed size FIFO list. Stores values as a fixed size array looping insertion point when array is full. Warnings:

Since:
3.2
Author:
gquintana

Nested Class Summary
private  class CircularList.EmptyIterator
          Empty iterator used when the list is empty
private  class CircularList.MainIterator
          Main iterator user when the list contains at least one element
 
Field Summary
private  T[] elements
          Elements
private  int firstIndex
          Index of the the first element.
private  int lastIndex
          Index + 1 of the last element.
private  int size
          Number of element stored in this list.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
CircularList(int capacity)
          Contructor
 
Method Summary
 boolean add(T newElement)
          Add an element to the list, overwriting last added element when list's capacity is reached
 boolean addAll(Collection<? extends T> newElements)
          Insert a collection of elements, looping on them
 void clear()
          Removes all elements from the list
private  int convertIndex(int index)
          Converts an index starting from 0 into an index starting from first
private
<X> void
copy(X[] elementsCopy)
          Copy elements in a target array
 T first()
          Get the first (inserted) element
 T get(int index)
          Get element by index
 int getCapacity()
          Get capacity (buffer size).
private  int incrementIndex(int index, int maxIndex)
          Increment an index
 boolean isEmpty()
          Tells whether the list is empty
private  boolean isFull()
           
 Iterator<T> iterator()
          Create a new iterator to browse elements
 T last()
          Get the last (inserted) element
 T removeFirst()
          Remove the first (inserted) element of the collection
 int size()
          Return the number of elements in the list
 Object[] toArray()
           
<T> T[]
toArray(T[] elementsCopy)
           
 
Methods inherited from class java.util.AbstractList
add, addAll, equals, hashCode, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
 
Methods inherited from class java.util.AbstractCollection
contains, containsAll, remove, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
contains, containsAll, remove, removeAll, retainAll
 

Field Detail

elements

private final T[] elements
Elements


lastIndex

private int lastIndex
Index + 1 of the last element. Or said differently index where will be added the next element


firstIndex

private int firstIndex
Index of the the first element. Or said differently index which will be removed when list capacity is achieved.


size

private int size
Number of element stored in this list.

Constructor Detail

CircularList

public CircularList(int capacity)
Contructor

Parameters:
capacity - Size of the ring buffer
Method Detail

getCapacity

public int getCapacity()
Get capacity (buffer size).

Returns:
capacity

convertIndex

private int convertIndex(int index)
Converts an index starting from 0 into an index starting from first

Parameters:
index - Index
Returns:
Index converted

get

public T get(int index)
Get element by index

Specified by:
get in interface List<T>
Specified by:
get in class AbstractList<T>
Parameters:
index - Index
Returns:
Element

size

public int size()
Return the number of elements in the list

Specified by:
size in interface Collection<T>
Specified by:
size in interface List<T>
Specified by:
size in class AbstractCollection<T>
Returns:
Size

isEmpty

public boolean isEmpty()
Tells whether the list is empty

Specified by:
isEmpty in interface Collection<T>
Specified by:
isEmpty in interface List<T>
Overrides:
isEmpty in class AbstractCollection<T>
Returns:
true if empty

incrementIndex

private int incrementIndex(int index,
                           int maxIndex)
Increment an index

Parameters:
index - Input index
maxIndex - Assigned value when capacity is reached
Returns:
Output index

add

public boolean add(T newElement)
Add an element to the list, overwriting last added element when list's capacity is reached

Specified by:
add in interface Collection<T>
Specified by:
add in interface List<T>
Overrides:
add in class AbstractList<T>
Parameters:
newElement - Element to add
Returns:
Always true

isFull

private boolean isFull()

addAll

public boolean addAll(Collection<? extends T> newElements)
Insert a collection of elements, looping on them

Specified by:
addAll in interface Collection<T>
Specified by:
addAll in interface List<T>
Overrides:
addAll in class AbstractCollection<T>
Parameters:
newElements - Collection of elements to add
Returns:
Always true

removeFirst

public T removeFirst()
Remove the first (inserted) element of the collection

Returns:
Removed element or null if any

first

public T first()
Get the first (inserted) element

Returns:
First element or null if any

last

public T last()
Get the last (inserted) element

Returns:
Last element or null if any

clear

public void clear()
Removes all elements from the list

Specified by:
clear in interface Collection<T>
Specified by:
clear in interface List<T>
Overrides:
clear in class AbstractList<T>

iterator

public Iterator<T> iterator()
Create a new iterator to browse elements

Specified by:
iterator in interface Iterable<T>
Specified by:
iterator in interface Collection<T>
Specified by:
iterator in interface List<T>
Overrides:
iterator in class AbstractList<T>
Returns:
Iterator

copy

private <X> void copy(X[] elementsCopy)
Copy elements in a target array

Parameters:
elementsCopy - Target array

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<T>
Specified by:
toArray in interface List<T>
Overrides:
toArray in class AbstractCollection<T>

toArray

public <T> T[] toArray(T[] elementsCopy)
Specified by:
toArray in interface Collection<T>
Specified by:
toArray in interface List<T>
Overrides:
toArray in class AbstractCollection<T>


Copyright © 2013. All Rights Reserved.