001package edu.wpi.first.wpilibj.networktables2.util;
002
003
004
005/**
006 * 
007 * A simple unsynchronized list implementation
008 * 
009 * @author mwills
010 *
011 */
012public class List extends ResizeableArrayObject{
013
014        protected int size = 0;
015        /**
016         * create a new list
017         */
018        public List() {
019        }
020        /**
021         * Create a new list with a given initial size of the backing array
022         * @param initialSize the initial size of the backing array
023         */
024        public List(final int initialSize) {
025                super(initialSize);
026        }
027
028        /**
029         * @return true if the list is empty
030         */
031        public boolean isEmpty() {
032                return size == 0;
033        }
034
035        /**
036         * @return the size of the list
037         */
038        public int size(){
039                return size;
040        }
041
042        /**
043         * Add an object to the end of the list
044         * @param o the object to be added
045         */
046        public void add(final Object o){
047                ensureSize(size+1);
048                array[size++] = o;
049        }
050
051        /**
052         * Remove an element from the list
053         * @param index the index of the element to be removed
054         * @throws IndexOutOfBoundsException if the index is not in the list
055         */
056        public void remove(final int index) throws IndexOutOfBoundsException{
057                if(index<0 || index>=size)
058                        throw new IndexOutOfBoundsException();
059                if(index < size-1)
060                        System.arraycopy(array, index+1, array, index, size-index-1);
061                size--;
062        }
063
064        /**
065         * clear all elements from the list
066         */
067        public void clear(){
068                size = 0;
069        }
070
071        /**
072         * @param index
073         * @return a given element from the list
074         */
075        public Object get(final int index) {
076                if(index<0 || index>=size)
077                        throw new IndexOutOfBoundsException();
078                return array[index];
079        }
080
081        /**
082         * remove the first element in the list where {@link Object#equals(Object)} is true
083         * @param object
084         * @return true if the element was removed
085         */
086        public boolean remove(final Object object) {
087                for(int i = 0; i<size; ++i){
088                        Object value = array[i];
089                        if(object==null ? value==null : object.equals(value)){
090                                remove(i);
091                                return true;
092                        }
093                }
094                return false;
095        }
096
097        /**
098         * @param object
099         * @return true if the list contains the given element (where {@link Object#equals(Object)} is true)
100         */
101        public boolean contains(final Object object) {
102                for(int i = 0; i<size; ++i){
103                        Object value = array[i];
104                        if(object==null ? value==null : object.equals(value)){
105                                return true;
106                        }
107                }
108                return false;
109        }
110
111        /**
112         * set the value at a given index in the list
113         * @param index the index where the object should be put
114         * @param obj the new value at the index
115         */
116        public void set(final int index, final Object obj) {
117                if(index<0 || index>=size)
118                        throw new IndexOutOfBoundsException();
119                array[index] = obj;
120        }
121}