001package votorola.g.util; // Copyright 2008, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.util.*;
004import votorola.g.lang.*;
005
006
007/** An unmodifiable list backed by an array.  It is unmodifiable in the sense that clients
008  * are provided no means to access and modify the array.  Modification of the array by
009  * other means will still affect the list.  The list is actually serializeable only if
010  * its elements are serializeable.
011  */
012public @ThreadSafe class ArrayListU<E> extends AbstractList<E> implements java.io.Serializable
013{
014
015
016    /** Contructs an ArrayListU.
017      */
018    public ArrayListU( E[] _backingArray )
019    {
020        if( _backingArray == null ) throw new NullPointerException(); // fail fast
021
022        backingArray = _backingArray;
023    }
024
025
026
027   // - C o l l e c t i o n --------------------------------------------------------------
028
029
030    public @Override int size() { return backingArray.length; }
031
032
033
034   // - L i s t --------------------------------------------------------------------------
035
036
037    public @Override E get( int index ) { return backingArray[index]; }
038
039
040
041   // ====================================================================================
042
043
044    /** An unmodifiable list that provides open access to the backing array.
045      */
046    public static @ThreadRestricted("touch") final class Open<E> extends ArrayListU<E>
047    {
048
049
050        /** Contructs an ArrayListU.Open.
051          *
052          *     @param backingArray per {@linkplain #getBackingArray getBackingArray}()
053          */
054        public Open( E[] backingArray ) { super( backingArray ); }
055
056
057
058       // --------------------------------------------------------------------------------
059
060
061        /** Returns the array that backs this list.
062          *
063          *     @return non-null array
064          *
065          *     @see #setBackingArray(Object[])
066          */
067        public E[] getBackingArray() { return backingArray; }
068
069
070
071            /** Sets the array that backs this list.
072              *
073              *     @see #getBackingArray()
074              */
075            public void setBackingArray( final E[] newBackingArray )
076            {
077                if( newBackingArray == null ) throw new NullPointerException(); // fail fast
078
079                backingArray = newBackingArray;
080            }
081
082
083    }
084
085
086
087//// P r i v a t e ///////////////////////////////////////////////////////////////////////
088
089
090    protected E[] backingArray;
091
092
093}