Class IntObjGapList

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<java.lang.Integer>, java.util.Collection<java.lang.Integer>, java.util.List<java.lang.Integer>, java.util.RandomAccess, ICollection<java.lang.Integer>, IListable<java.lang.Integer>
    Direct Known Subclasses:
    IntObjGapList.ImmutableIntObjGapList

    public class IntObjGapList
    extends IList<java.lang.Integer>
    IntObjGapList implements the List interface and uses an instance of IntGapList for storage. It therefore allows to use the advantages of primitive collections like saved memory and improved execution speed when standard list collections are expected.

    Note that this implementation is not synchronized.

    Version:
    $Id: IntObjGapList.java 2200 2014-03-21 10:46:29Z origo $
    See Also:
    GapList, IntGapList, Serialized Form
    • Constructor Detail

      • IntObjGapList

        public IntObjGapList()
      • IntObjGapList

        public IntObjGapList​(int capacity)
      • IntObjGapList

        public IntObjGapList​(java.util.Collection<? extends java.lang.Integer> elems)
    • Method Detail

      • toWrapper

        static java.lang.Integer[] toWrapper​(int[] elems)
      • toPrimitive

        static int[] toPrimitive​(java.lang.Integer[] elems)
      • toPrimitive

        static int[] toPrimitive​(GapList<? extends java.lang.Integer> list2)
      • toPrimitive

        static int[] toPrimitive​(java.util.Collection<? extends java.lang.Integer> list)
      • create

        public static IntObjGapList create​(java.lang.Integer... elems)
      • create

        public static IntObjGapList create​(java.util.Collection<? extends java.lang.Integer> elems)
      • init

        public void init()
      • init

        public void init​(java.lang.Integer... elems)
      • init

        public void init​(java.util.Collection<? extends java.lang.Integer> elems)
      • doClone

        protected void doClone​(IList<java.lang.Integer> that)
        Description copied from class: IList
        Initialize this object after the bitwise copy has been made by Object.clone().
        Specified by:
        doClone in class IList<java.lang.Integer>
        Parameters:
        that - source object
      • doAssign

        protected void doAssign​(IList<java.lang.Integer> that)
        Description copied from class: IList
        Assign this list the content of the that list. This is done by bitwise copying so the that list should not be used afterwards.
        Specified by:
        doAssign in class IList<java.lang.Integer>
        Parameters:
        that - list to copy content from
      • isReadOnly

        public boolean isReadOnly()
        Description copied from class: IList
        Returns true if this list is either unmodifiable or immutable, false otherwise.
        Specified by:
        isReadOnly in class IList<java.lang.Integer>
      • copy

        public IntObjGapList copy()
        Description copied from class: IList
        Returns a shallow copy of this list. The new list will contain the same elements as the source list, i.e. the elements themselves are not copied. The capacity of the list will be set to the number of elements, i.e. size and capacity are equal. This returned list will be modifiable, i.e. a read-only list will be copied and be modifiable again.
        Specified by:
        copy in interface ICollection<java.lang.Integer>
        Specified by:
        copy in class IList<java.lang.Integer>
        Returns:
        a modifiable copy of this list
      • clone

        public IntObjGapList clone()
        Description copied from class: IList
        Returns a shallow copy of this list. The new list will contain the same elements as the source list, i.e. the elements themselves are not copied. The capacity of the list will be set to the number of elements, i.e. size and capacity are equal. If the list is read-only, the same list is returned without change. Use IList.copy() to .
        Overrides:
        clone in class IList<java.lang.Integer>
        Returns:
        a modifiable copy of this list
      • getDefaultElem

        public java.lang.Integer getDefaultElem()
        Specified by:
        getDefaultElem in class IList<java.lang.Integer>
      • doCreate

        public IList<java.lang.Integer> doCreate​(int capacity)
        Description copied from class: IList
        Create list with specified capacity.
        Specified by:
        doCreate in class IList<java.lang.Integer>
        Parameters:
        capacity - initial capacity (use -1 for default capacity)
        Returns:
        created list
      • size

        public int size()
        Description copied from interface: IListable
        Return size of list
        Specified by:
        size in interface java.util.Collection<java.lang.Integer>
        Specified by:
        size in interface IListable<java.lang.Integer>
        Specified by:
        size in interface java.util.List<java.lang.Integer>
        Specified by:
        size in class IList<java.lang.Integer>
      • capacity

        public int capacity()
        Returns capacity of this GapList. Note that two GapLists are considered equal even if they have a distinct capacity. Also the capacity can be changed by operations like clone() etc.
        Specified by:
        capacity in class IList<java.lang.Integer>
        Returns:
        capacity of this GapList
      • get

        public java.lang.Integer get​(int index)
        Description copied from interface: IListable
        Return element at specified position
        Specified by:
        get in interface IListable<java.lang.Integer>
        Specified by:
        get in interface java.util.List<java.lang.Integer>
        Overrides:
        get in class IList<java.lang.Integer>
      • doGet

        protected java.lang.Integer doGet​(int index)
        Description copied from class: IList
        Helper method for getting an element from the list. This is the only method which really gets an element. Override if you need to validity checks before getting.
        Specified by:
        doGet in class IList<java.lang.Integer>
        Parameters:
        index - index of element to return
        Returns:
        the element at the specified position in this list
      • doGetAll

        protected void doGetAll​(java.lang.Object[] elems,
                                int index,
                                int len)
        Description copied from class: IList
        Helper method to fill the specified elements in an array.
        Overrides:
        doGetAll in class IList<java.lang.Integer>
        Parameters:
        elems - array to store the list elements
        index - index of first element to copy
        len - number of elements to copy
      • doAdd

        protected boolean doAdd​(int index,
                                java.lang.Integer elem)
        Description copied from class: IList
        Helper method for adding an element to the list. This is the only method which really adds an element. Override if you need to validity checks before adding.
        Specified by:
        doAdd in class IList<java.lang.Integer>
        Parameters:
        index - index where element should be added (-1 means it is up to the implementation to choose the index)
        elem - element to add
        Returns:
        true if element has been added, false otherwise
      • doSet

        protected java.lang.Integer doSet​(int index,
                                          java.lang.Integer elem)
        Description copied from class: IList
        Helper method for setting an element in the list. This is the only method which really sets an element. Override if you need to validity checks before setting.
        Specified by:
        doSet in class IList<java.lang.Integer>
        Parameters:
        index - index where element will be placed
        elem - element to set
        Returns:
        old element which was at the position
      • doRemove

        protected java.lang.Integer doRemove​(int index)
        Description copied from class: IList
        Helper method to remove an element. This is the only method which really removes an element. Override if you need to validity checks before removing.
        Specified by:
        doRemove in class IList<java.lang.Integer>
        Parameters:
        index - index of element to remove
        Returns:
        removed element
      • doRemoveAll

        protected void doRemoveAll​(int index,
                                   int len)
        Description copied from class: IList
        Remove specified range of elements from list.
        Overrides:
        doRemoveAll in class IList<java.lang.Integer>
        Parameters:
        index - index of first element to remove
        len - number of elements to remove
      • doReSet

        protected java.lang.Integer doReSet​(int index,
                                            java.lang.Integer elem)
        Description copied from class: IList
        Sets an element at specified position. This method is used internally if existing elements will be moved etc. Override if you need to validity checks.
        Specified by:
        doReSet in class IList<java.lang.Integer>
        Parameters:
        index - index where element will be placed
        elem - element to set
        Returns:
        old element which was at the position
      • move

        public void move​(int srcIndex,
                         int dstIndex,
                         int len)
        Description copied from class: IList
        Move specified elements. Source and destination ranges may overlap. The elements which are moved away are set to null, so the size of the list does not change.
        Overrides:
        move in class IList<java.lang.Integer>
        Parameters:
        srcIndex - index of first source element to move
        dstIndex - index of first destination element to move
        len - number of elements to move
      • doEnsureCapacity

        protected void doEnsureCapacity​(int minCapacity)
        Description copied from class: IList
        Increases the capacity of this list instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
        Specified by:
        doEnsureCapacity in class IList<java.lang.Integer>
        Parameters:
        minCapacity - the desired minimum capacity
      • trimToSize

        public void trimToSize()
        Description copied from class: IList
        An application can use this operation to minimize the storage of an instance.
        Specified by:
        trimToSize in class IList<java.lang.Integer>
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Collection<java.lang.Integer>
        Specified by:
        equals in interface java.util.List<java.lang.Integer>
        Overrides:
        equals in class IList<java.lang.Integer>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<java.lang.Integer>
        Specified by:
        hashCode in interface java.util.List<java.lang.Integer>
        Overrides:
        hashCode in class IList<java.lang.Integer>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class IList<java.lang.Integer>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<java.lang.Integer>
        Specified by:
        isEmpty in interface java.util.List<java.lang.Integer>
        Overrides:
        isEmpty in class IList<java.lang.Integer>
      • indexOf

        public int indexOf​(java.lang.Object elem)
        Specified by:
        indexOf in interface java.util.List<java.lang.Integer>
        Overrides:
        indexOf in class IList<java.lang.Integer>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object elem)
        Specified by:
        lastIndexOf in interface java.util.List<java.lang.Integer>
        Overrides:
        lastIndexOf in class IList<java.lang.Integer>
      • remove

        public boolean remove​(java.lang.Object elem)
        Specified by:
        remove in interface java.util.Collection<java.lang.Integer>
        Specified by:
        remove in interface java.util.List<java.lang.Integer>
        Overrides:
        remove in class IList<java.lang.Integer>
      • contains

        public boolean contains​(java.lang.Object elem)
        Specified by:
        contains in interface java.util.Collection<java.lang.Integer>
        Specified by:
        contains in interface java.util.List<java.lang.Integer>
        Overrides:
        contains in class IList<java.lang.Integer>
      • containsAny

        public boolean containsAny​(java.util.Collection<?> coll)
        Description copied from class: IList
        Returns true if any of the elements of the specified collection is contained in the list.
        Overrides:
        containsAny in class IList<java.lang.Integer>
        Parameters:
        coll - collection with elements to be contained
        Returns:
        true if any element is contained, false otherwise
      • containsAll

        public boolean containsAll​(java.util.Collection<?> coll)
        Specified by:
        containsAll in interface java.util.Collection<java.lang.Integer>
        Specified by:
        containsAll in interface java.util.List<java.lang.Integer>
        Overrides:
        containsAll in class IList<java.lang.Integer>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> coll)
        Specified by:
        removeAll in interface java.util.Collection<java.lang.Integer>
        Specified by:
        removeAll in interface java.util.List<java.lang.Integer>
        Overrides:
        removeAll in class IList<java.lang.Integer>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> coll)
        Specified by:
        retainAll in interface java.util.Collection<java.lang.Integer>
        Specified by:
        retainAll in interface java.util.List<java.lang.Integer>
        Overrides:
        retainAll in class IList<java.lang.Integer>
      • toArray

        public java.lang.Object[] toArray​(int index,
                                          int len)
        Returns an array containing the specified elements in this list.
        Overrides:
        toArray in class IList<java.lang.Integer>
        Parameters:
        index - index of first element to copy
        len - number of elements to copy
        Returns:
        array containing the specified elements
        See Also:
        List#toArray()
      • toArray

        public <T> T[] toArray​(T[] array,
                               int index,
                               int len)
        Returns an array containing the specified elements in this list.
        Overrides:
        toArray in class IList<java.lang.Integer>
        Parameters:
        array - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose
        index - index of first element to copy
        len - number of elements to copy
        Returns:
        array containing the specified elements
        See Also:
        List#toArray(Object[])
      • toArray

        public <T> T[] toArray​(java.lang.Class<T> clazz,
                               int index,
                               int len)
        Returns an array containing the specified elements in this list.
        Overrides:
        toArray in class IList<java.lang.Integer>
        Parameters:
        clazz - class for array elements
        index - index of first element to copy
        len - number of elements to copy
        Returns:
        array containing the specified elements
      • addAll

        public boolean addAll​(java.util.Collection<? extends java.lang.Integer> coll)
        Description copied from class: IList
        Adds all of the elements in the specified collection into this list. The new elements will appear in the list in the order that they are returned by the specified collection's iterator.
        Specified by:
        addAll in interface java.util.Collection<java.lang.Integer>
        Specified by:
        addAll in interface java.util.List<java.lang.Integer>
        Overrides:
        addAll in class IList<java.lang.Integer>
        Parameters:
        coll - collection containing elements to be added to this list
        Returns:
        true if this list changed as a result of the call
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends java.lang.Integer> coll)
        Description copied from class: IList
        Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection's iterator.
        Specified by:
        addAll in interface java.util.List<java.lang.Integer>
        Overrides:
        addAll in class IList<java.lang.Integer>
        Parameters:
        index - index at which to insert the first element from the specified collection
        coll - collection containing elements to be inserted into this list
        Returns:
        true if this list changed as a result of the call
      • addArray

        public boolean addArray​(java.lang.Integer... elems)
        Description copied from class: IList
        Adds all specified elements into this list.
        Overrides:
        addArray in class IList<java.lang.Integer>
        Parameters:
        elems - elements to be added to this list
        Returns:
        true if this list changed as a result of the call
      • addArray

        public boolean addArray​(int index,
                                java.lang.Integer... elems)
        Description copied from class: IList
        Inserts the specified elements into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices).
        Overrides:
        addArray in class IList<java.lang.Integer>
        Parameters:
        index - index at which to insert the first element from the specified collection
        elems - elements to be inserted into this list
        Returns:
        true if this list changed as a result of the call
      • addAll

        public boolean addAll​(IList<? extends java.lang.Integer> list2)
        Description copied from class: IList
        Adds all of the elements in the specified list into this list.
        Overrides:
        addAll in class IList<java.lang.Integer>
        Parameters:
        list2 - collection containing elements to be added to this list
        Returns:
        true if this list changed as a result of the call
      • addAll

        public boolean addAll​(int index,
                              IList<? extends java.lang.Integer> list2)
        Description copied from class: IList
        Inserts all of the elements in the specified list into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices).
        Overrides:
        addAll in class IList<java.lang.Integer>
        Parameters:
        index - index at which to insert the first element from the specified collection
        list2 - list containing elements to be inserted into this list
        Returns:
        true if this list changed as a result of the call
      • removeFirstOccurrence

        public boolean removeFirstOccurrence​(java.lang.Object elem)
        Description copied from class: IList
        See Deque.removeFirstOccurrence(java.lang.Object)
        Overrides:
        removeFirstOccurrence in class IList<java.lang.Integer>
      • removeLastOccurrence

        public boolean removeLastOccurrence​(java.lang.Object elem)
        Description copied from class: IList
        See Deque.removeLastOccurrence(java.lang.Object)
        Overrides:
        removeLastOccurrence in class IList<java.lang.Integer>
      • getAll

        public GapList<java.lang.Integer> getAll​(int index,
                                                 int len)
        Description copied from class: IList
        Returns specified range of elements from list.
        Overrides:
        getAll in class IList<java.lang.Integer>
        Parameters:
        index - index of first element to retrieve
        len - number of elements to retrieve
        Returns:
        list containing the specified range of elements
      • setAll

        public void setAll​(int index,
                           IList<? extends java.lang.Integer> list2)
        Description copied from class: IList
        Sets the specified elements.
        Overrides:
        setAll in class IList<java.lang.Integer>
        Parameters:
        index - index of first element to set
        list2 - list with elements to set
      • setAll

        public void setAll​(int index,
                           java.util.Collection<? extends java.lang.Integer> coll)
        Description copied from class: IList
        Sets the specified elements.
        Overrides:
        setAll in class IList<java.lang.Integer>
        Parameters:
        index - index of first element to set
        coll - collection with elements to set
      • setArray

        public void setArray​(int index,
                             java.lang.Integer... elems)
        Description copied from class: IList
        Sets the specified elements.
        Overrides:
        setArray in class IList<java.lang.Integer>
        Parameters:
        index - index of first element to set
        elems - array with elements to set
      • sort

        public void sort​(int index,
                         int len,
                         java.util.Comparator comparator)
        Description copied from class: IList
        Sort specified elements in the list using the specified comparator.
        Specified by:
        sort in class IList<java.lang.Integer>
        Parameters:
        index - index of first element to sort
        len - number of elements to sort
        comparator - comparator to use for sorting (null means the elements natural ordering should be used)
        See Also:
        Arrays.sort(int[])
      • binarySearch

        public <K> int binarySearch​(int index,
                                    int len,
                                    K key,
                                    java.util.Comparator<? super K> comparator)
        Description copied from class: IList
        Searches the specified range for an object using the binary search algorithm.

        Note that the method is defined to work with an arbitrary type <K>. This allows to search directly for a key field in the object without the need to construct an object containing the key:

        
         persons.binarySearch("john", new SearchByName());
         
         class SearchByName implements Comparator<Object> {
        	 public int compare(Object o1, Object o2) {
        	   String s1 = (o1 instanceof String) ? (String) o1 : ((Name) o1).getName();
        	   String s2 = (o2 instanceof String) ? (String) o2 : ((Name) o2).getName();
        	   return s1.compareTo(s2);
        	 }
         }
         /
        Specified by:
        binarySearch in class IList<java.lang.Integer>
        Parameters:
        index - index of first element to search
        len - number of elements to search
        key - the value to be searched for
        comparator - the comparator by which the list is ordered. A null value indicates that the elements' natural ordering should be used.
        Returns:
        index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
        See Also:
        Arrays.binarySearch(long[], long)
      • unmodifiableList

        public IntObjGapList unmodifiableList()
        Description copied from class: IList
        Returns an unmodifiable view of this list. This method allows modules to provide users with "read-only" access to internal lists, where changes made to the original list are visible as query operations on the returned list "read through" to the specified list. Attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException. If this list is already unmodifiable, it is returned unchanged. See IList.immutableList() to get an immutable copy of a list.
        Specified by:
        unmodifiableList in class IList<java.lang.Integer>
        Returns:
        an unmodifiable view of the specified list
      • immutableList

        public IntObjGapList immutableList()
        Description copied from class: IList
        Returns an immutable copy of this list. The returned list is independent from the original list, i.e. changes done later are not seen. Attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException. If this list is already immutable, it is returned unchanged. See IList.unmodifiableList() to get unmodifiable view of a list.
        Specified by:
        immutableList in class IList<java.lang.Integer>
        Returns:
        an immutable copy of the specified list