Advertisement
Guest User

Gagliano

a guest
Jan 14th, 2011
2,484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. package br.com.ggagliano.swingutil.util;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5.  
  6. /**
  7.  * <p>
  8.  * This class implements a non-thread-safe circular list, with basic methods for
  9.  * controlling internal indexing of access. It inherits from {@link ArrayList},
  10.  * so all of its tools can be used. As it is a circular list, you can use idioms
  11.  * such, which will return <i>true</i>:
  12.  * </p>
  13.  *
  14.  * <p>
  15.  * &nbsp;
  16.  * </p>
  17.  *
  18.  * <p>
  19.  * <code>boolean check = circularList.get(i).equals(circularList.get(i+circularList.size());</code>
  20.  * </p>
  21.  *
  22.  * <p>
  23.  * &nbsp;
  24.  * </p>
  25.  *
  26.  * <p>
  27.  * Note that you may add or remove items from the list. This operations usually
  28.  * updates the internal index used for the class to control the next and
  29.  * previous element and iterate over the list. However, if you remove the actual
  30.  * item from the list, the internal index will lose its reference and will be
  31.  * forced to go back to the first valid item of the list.
  32.  * </p>
  33.  *
  34.  * @author Gabriel Gagliano
  35.  * @since 0.1 14/01/2011
  36.  * @see ArrayList
  37.  */
  38. public class CircularArrayList<E> extends ArrayList<E> {
  39.  
  40.     private static final long serialVersionUID = -2573855929861889907L;
  41.  
  42.     private transient int actualIndex;
  43.  
  44.     /**
  45.      * Simple constructor, with no element at all.
  46.      */
  47.     public CircularArrayList() {
  48.         super();
  49.     }
  50.  
  51.     /**
  52.      * Constructs a circular list containing the elements of the specified
  53.      * collection, in the order they are returned by the collection's iterator.
  54.      *
  55.      * @param collection
  56.      *            The collection whose elements are to be placed into this list.
  57.      */
  58.     public CircularArrayList(final Collection<E> collection) {
  59.         super(collection);
  60.     }
  61.  
  62.     @Override
  63.     public E get(int index) {
  64.         index = index % this.size();
  65.         return super.get(index);
  66.     }
  67.  
  68.     @Override
  69.     public void add(final int index, final E element) {
  70.         if (index <= this.actualIndex) {
  71.             this.actualIndex++;
  72.         }
  73.         super.add(index, element);
  74.     };
  75.  
  76.     @Override
  77.     public boolean addAll(int index, Collection<? extends E> collection) {
  78.         if (index <= this.actualIndex) {
  79.             this.actualIndex = this.actualIndex + collection.size();
  80.         }
  81.         return super.addAll(index, collection);
  82.     }
  83.  
  84.     @Override
  85.     public E remove(final int index) {
  86.         final E element = super.remove(index);
  87.  
  88.         if (index == actualIndex) {
  89.             this.actualIndex = 0;
  90.         } else if (index < this.actualIndex) {
  91.             this.actualIndex--;
  92.         }
  93.  
  94.         return element;
  95.     }
  96.  
  97.     @Override
  98.     public boolean remove(final Object object) {
  99.         final int index = this.indexOf(object);
  100.         return this.remove(index) != null;
  101.     }
  102.  
  103.     @Override
  104.     public boolean removeAll(final Collection<?> collection) {
  105.         int nextIndex;
  106.         if (collection.contains(this.getActual())) {
  107.             nextIndex = 0;
  108.         } else {
  109.             int discount = 0;
  110.             for (Object object : collection) {
  111.                 if (this.contains(object)) {
  112.                     final int objectIndex = this.indexOf(object);
  113.                     if (objectIndex < this.actualIndex) {
  114.                         discount++;
  115.                     }
  116.                 }
  117.             }
  118.             nextIndex = this.actualIndex - discount;
  119.         }
  120.  
  121.         final boolean result = super.removeAll(collection);
  122.         if (result) {
  123.             this.actualIndex = nextIndex;
  124.         }
  125.         return result;
  126.     }
  127.  
  128.     @Override
  129.     public void clear() {
  130.         this.actualIndex = 0;
  131.         super.clear();
  132.     }
  133.  
  134.     /**
  135.      * <p>
  136.      * Gets the exact previous element in the list. If the actual element is the
  137.      * first one, then the last element is returned. If the list has just been
  138.      * created, then the next element is the second one, the actual element is
  139.      * the first, and the previous element is the last.
  140.      * </p>
  141.      * <p>
  142.      * Note that this is not a Thread-safe method! A bad use of it may cause
  143.      * race condition errors and, eventually, throw an
  144.      * {@link ArrayIndexOutOfBoundsException}.
  145.      * </p>
  146.      *
  147.      * @return The exact previous element in the circular list.
  148.      */
  149.     public E previous() {
  150.         if (this.actualIndex == 0) {
  151.             this.actualIndex = this.size() - 1;
  152.         } else {
  153.             this.actualIndex--;
  154.         }
  155.  
  156.         return super.get(actualIndex);
  157.     }
  158.  
  159.     /**
  160.      * <p>
  161.      * Gets the exact actual element in the list, without moving its index. If
  162.      * the list has just been created, then the actual element is the first one.
  163.      * </p>
  164.      * <p>
  165.      * Note that this is not a Thread-safe method! A bad use of it and other
  166.      * methods in this class may cause race condition errors and, eventually,
  167.      * throw an {@link ArrayIndexOutOfBoundsException}.
  168.      * </p>
  169.      *
  170.      * @return The exact actual element in the circular list.
  171.      */
  172.     public E getActual() {
  173.         return super.get(actualIndex);
  174.     }
  175.  
  176.     /**
  177.      * <p>
  178.      * Gets the exact next element in the list. If the actual element is the
  179.      * last one, then the first element is returned. If the list has just been
  180.      * created, then the next element is the second one, the actual element is
  181.      * the first, and the previous element is the last.
  182.      * </p>
  183.      * <p>
  184.      * Note that this is not a Thread-safe method! A bad use of it may cause
  185.      * race condition errors and, eventually, throw an
  186.      * {@link ArrayIndexOutOfBoundsException}.
  187.      * </p>
  188.      *
  189.      * @return The exact next element in the circular list.
  190.      */
  191.     public E next() {
  192.         if (this.actualIndex == this.size() - 1) {
  193.             this.actualIndex = 0;
  194.         } else {
  195.             this.actualIndex++;
  196.         }
  197.  
  198.         return super.get(actualIndex);
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement