Class RingArray<T>


  • public class RingArray<T>
    extends Object
    Implements essentially an ArrayList of fixed size in which only the elements from (high-size) to high are kept. So for example, if you want to keep at most 48 elements indexed by timeslot, every time you add an element at some timeslot t, all elements older than t-48 would be discarded. Attempts to retrieve elements outside the current "window" will return null. Attempts to insert elements below the current window will produce an IndexOutOfBoundsException.
    Author:
    John Collins
    • Constructor Summary

      Constructors 
      Constructor Description
      RingArray​(int size)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      List<T> asList​(int start)
      Returns the current active elements as an AbstractList.
      void clean​(int startIndex)
      Cleans the "unused" portion of the array, from the current maxIndex to the given index
      void clear()
      Clears all data out of the array
      T get​(int index)
      Returns the element at the given index, or null if index is out of range
      T[] getActiveArray​(int startIndex)
      Returns the "active" elements of the ring as a simple Array
      int getActiveLength​(int startIndex)
      Returns the "active length" of the array starting at the specified index
      void set​(int index, T element)
      Sets the element at the given index, potentially overwriting elements with index less than index-size.
    • Constructor Detail

      • RingArray

        public RingArray​(int size)
    • Method Detail

      • clear

        public void clear()
        Clears all data out of the array
      • clean

        public void clean​(int startIndex)
        Cleans the "unused" portion of the array, from the current maxIndex to the given index
      • getActiveLength

        public int getActiveLength​(int startIndex)
        Returns the "active length" of the array starting at the specified index
      • set

        public void set​(int index,
                        T element)
        Sets the element at the given index, potentially overwriting elements with index less than index-size. Attempts to add elements below the current window will fail silently.
      • get

        public T get​(int index)
        Returns the element at the given index, or null if index is out of range
      • getActiveArray

        public T[] getActiveArray​(int startIndex)
        Returns the "active" elements of the ring as a simple Array
      • asList

        public List<T> asList​(int start)
        Returns the current active elements as an AbstractList. Note that this list is a snapshot and will no longer be valid after elements are added or removed from the ring.