Class LazyStringArrayList

  • All Implemented Interfaces:
    Internal.ProtobufList<String>, LazyStringList, ProtocolStringList, Iterable<String>, Collection<String>, List<String>, RandomAccess

    public class LazyStringArrayList
    extends AbstractList<E>
    implements LazyStringList, RandomAccess
    An implementation of LazyStringList that wraps an ArrayList. Each element is one of String, ByteString, or byte[]. It caches the last one requested which is most likely the one needed next. This minimizes memory usage while satisfying the most common use cases.

    Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list.

    If the implementation is accessed via concurrent reads, this is thread safe. Conversions are done in a thread safe manner. It's possible that the conversion may happen more than once if two threads attempt to access the same element and the modifications were not visible to each other, but this will not result in any corruption of the list or change in behavior other than performance.

    • Constructor Detail

      • LazyStringArrayList

        public LazyStringArrayList()
      • LazyStringArrayList

        public LazyStringArrayList​(int initialCapacity)
      • LazyStringArrayList

        public LazyStringArrayList​(LazyStringList from)
      • LazyStringArrayList

        public LazyStringArrayList​(List<String> from)
    • Method Detail

      • emptyList

        public static LazyStringArrayList emptyList()
        Returns an empty immutable LazyStringArrayList instance
      • get

        public String get​(int index)
        Description copied from class: AbstractList
        Returns the element at the specified position in this list.
        Specified by:
        get in interface List<String>
        Specified by:
        get in class AbstractList<String>
        Parameters:
        index - index of the element to return
        Returns:
        the element at the specified position in this list
      • size

        public int size()
        Description copied from class: AbstractCollection
        Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
        Specified by:
        size in interface Collection<String>
        Specified by:
        size in interface List<String>
        Specified by:
        size in class AbstractCollection<String>
        Returns:
        the number of elements in this collection
      • add

        public void add​(int index,
                        String element)
        Description copied from class: AbstractList
        Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

        This implementation always throws an UnsupportedOperationException.

        Specified by:
        add in interface List<String>
        Parameters:
        index - index at which the specified element is to be inserted
        element - element to be inserted
      • add

        @CanIgnoreReturnValue
        public boolean add​(String element)
        Description copied from class: AbstractList
        Appends the specified element to the end of this list (optional operation).

        Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.

        This implementation calls add(size(), e).

        Note that this implementation throws an UnsupportedOperationException unless add(int, E) is overridden.

        Specified by:
        add in interface Collection<String>
        Specified by:
        add in interface List<String>
        Parameters:
        element - element to be appended to this list
        Returns:
        true (as specified by Collection.add(E))
      • add

        public void add​(ByteString element)
        Description copied from interface: LazyStringList
        Appends the specified element to the end of this list (optional operation).
        Specified by:
        add in interface LazyStringList
        Parameters:
        element - element to be appended to this list
      • add

        public void add​(byte[] element)
        Description copied from interface: LazyStringList
        Appends the specified element to the end of this list (optional operation).
        Specified by:
        add in interface LazyStringList
        Parameters:
        element - element to be appended to this list
      • addAll

        @CanIgnoreReturnValue
        public boolean addAll​(Collection<? extends String> c)
        Description copied from class: AbstractCollection
        Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)

        This implementation iterates over the specified collection, and adds each object returned by the iterator to this collection, in turn.

        Note that this implementation will throw an UnsupportedOperationException unless add is overridden (assuming the specified collection is non-empty).

        Specified by:
        addAll in interface Collection<String>
        Specified by:
        addAll in interface List<String>
        Parameters:
        c - collection containing elements to be added to this collection
        Returns:
        true if this collection changed as a result of the call
        See Also:
        AbstractCollection.add(Object)
      • addAll

        @CanIgnoreReturnValue
        public boolean addAll​(int index,
                              Collection<? extends String> c)
        Description copied from class: AbstractList
        Inserts all of the elements in the specified collection into this list at the specified position (optional operation). 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 this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

        This implementation gets an iterator over the specified collection and iterates over it, inserting the elements obtained from the iterator into this list at the appropriate position, one at a time, using add(int, E). Many implementations will override this method for efficiency.

        Note that this implementation throws an UnsupportedOperationException unless add(int, E) is overridden.

        Specified by:
        addAll in interface List<String>
        Parameters:
        index - index at which to insert the first element from the specified collection
        c - collection containing elements to be added to this list
        Returns:
        true if this list changed as a result of the call
      • addAllByteString

        @CanIgnoreReturnValue
        public boolean addAllByteString​(Collection<? extends ByteString> values)
        Description copied from interface: LazyStringList
        Appends all elements in the specified ByteString collection to the end of this list.
        Specified by:
        addAllByteString in interface LazyStringList
        Parameters:
        values - collection whose elements are to be added to this list
        Returns:
        true if this list changed as a result of the call
      • addAllByteArray

        @CanIgnoreReturnValue
        public boolean addAllByteArray​(Collection<byte[]> c)
        Description copied from interface: LazyStringList
        Appends all elements in the specified byte[] collection to the end of this list.
        Specified by:
        addAllByteArray in interface LazyStringList
        Parameters:
        c - collection whose elements are to be added to this list
        Returns:
        true if this list changed as a result of the call
      • remove

        @CanIgnoreReturnValue
        public String remove​(int index)
        Description copied from class: AbstractList
        Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

        This implementation always throws an UnsupportedOperationException.

        Specified by:
        remove in interface List<String>
        Parameters:
        index - the index of the element to be removed
        Returns:
        the element previously at the specified position
      • clear

        public void clear()
        Description copied from class: AbstractList
        Removes all of the elements from this list (optional operation). The list will be empty after this call returns.

        This implementation calls removeRange(0, size()).

        Note that this implementation throws an UnsupportedOperationException unless remove(int index) or removeRange(int fromIndex, int toIndex) is overridden.

        Specified by:
        clear in interface Collection<String>
        Specified by:
        clear in interface List<String>
      • getRaw

        public Object getRaw​(int index)
        Description copied from interface: LazyStringList
        Returns the element at the specified position in this list as an Object that will either be a String or a ByteString.
        Specified by:
        getRaw in interface LazyStringList
        Parameters:
        index - index of the element to return
        Returns:
        the element at the specified position in this list
      • getByteString

        public ByteString getByteString​(int index)
        Description copied from interface: LazyStringList
        Returns the element at the specified position in this list as a ByteString.
        Specified by:
        getByteString in interface LazyStringList
        Parameters:
        index - index of the element to return
        Returns:
        the element at the specified position in this list
      • getByteArray

        public byte[] getByteArray​(int index)
        Description copied from interface: LazyStringList
        Returns the element at the specified position in this list as byte[].
        Specified by:
        getByteArray in interface LazyStringList
        Parameters:
        index - index of the element to return
        Returns:
        the element at the specified position in this list
      • set

        @CanIgnoreReturnValue
        public String set​(int index,
                          String s)
        Description copied from class: AbstractList
        Replaces the element at the specified position in this list with the specified element (optional operation).

        This implementation always throws an UnsupportedOperationException.

        Specified by:
        set in interface List<String>
        Parameters:
        index - index of the element to replace
        s - element to be stored at the specified position
        Returns:
        the element previously at the specified position
      • set

        public void set​(int index,
                        ByteString s)
        Description copied from interface: LazyStringList
        Replaces the element at the specified position in this list with the specified element (optional operation).
        Specified by:
        set in interface LazyStringList
        Parameters:
        index - index of the element to replace
        s - the element to be stored at the specified position
      • set

        public void set​(int index,
                        byte[] s)
        Description copied from interface: LazyStringList
        Replaces the element at the specified position in this list with the specified element (optional operation).
        Specified by:
        set in interface LazyStringList
        Parameters:
        index - index of the element to replace
        s - the element to be stored at the specified position
      • getUnderlyingElements

        public List<?> getUnderlyingElements()
        Description copied from interface: LazyStringList
        Returns an unmodifiable List of the underlying elements, each of which is either a String or its equivalent UTF-8 encoded ByteString or byte[]. It is an error for the caller to modify the returned List, and attempting to do so will result in an UnsupportedOperationException.
        Specified by:
        getUnderlyingElements in interface LazyStringList
      • mergeFrom

        public void mergeFrom​(LazyStringList other)
        Description copied from interface: LazyStringList
        Merges all elements from another LazyStringList into this one. This method differs from List.addAll(Collection) on that underlying byte arrays are copied instead of reference shared. Immutable API doesn't need to use this method as byte[] is not used there at all.
        Specified by:
        mergeFrom in interface LazyStringList
      • asByteArrayList

        public List<byte[]> asByteArrayList()
        Description copied from interface: LazyStringList
        Returns a mutable view of this list. Changes to the view will be made into the original list. This method is used in mutable API only.
        Specified by:
        asByteArrayList in interface LazyStringList
      • equals

        public boolean equals​(Object o)
        Description copied from class: AbstractList
        Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.

        This implementation first checks if the specified object is this list. If so, it returns true; if not, it checks if the specified object is a list. If not, it returns false; if so, it iterates over both lists, comparing corresponding pairs of elements. If any comparison returns false, this method returns false. If either iterator runs out of elements before the other it returns false (as the lists are of unequal length); otherwise it returns true when the iterations complete.

        Specified by:
        equals in interface Collection<E>
        Specified by:
        equals in interface List<E>
        Overrides:
        equals in class AbstractList<E>
        Parameters:
        o - the object to be compared for equality with this list
        Returns:
        true if the specified object is equal to this list
        See Also:
        Object.hashCode(), HashMap
      • remove

        @CanIgnoreReturnValue
        public boolean remove​(Object o)
        Description copied from class: AbstractCollection
        Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).

        This implementation iterates over the collection looking for the specified element. If it finds the element, it removes the element from the collection using the iterator's remove method.

        Note that this implementation throws an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection contains the specified object.

        Specified by:
        remove in interface Collection<E>
        Specified by:
        remove in interface List<E>
        Overrides:
        remove in class AbstractCollection<E>
        Parameters:
        o - element to be removed from this collection, if present
        Returns:
        true if an element was removed as a result of this call
      • removeAll

        @CanIgnoreReturnValue
        public boolean removeAll​(Collection<?> c)
        Description copied from class: AbstractCollection
        Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.

        This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's so contained, it's removed from this collection with the iterator's remove method.

        Note that this implementation will throw an UnsupportedOperationException if the iterator returned by the iterator method does not implement the remove method and this collection contains one or more elements in common with the specified collection.

        Specified by:
        removeAll in interface Collection<E>
        Specified by:
        removeAll in interface List<E>
        Overrides:
        removeAll in class AbstractCollection<E>
        Parameters:
        c - collection containing elements to be removed from this collection
        Returns:
        true if this collection changed as a result of the call
        See Also:
        AbstractCollection.remove(Object), AbstractCollection.contains(Object)
      • retainAll

        @CanIgnoreReturnValue
        public boolean retainAll​(Collection<?> c)
        Description copied from class: AbstractCollection
        Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.

        This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's not so contained, it's removed from this collection with the iterator's remove method.

        Note that this implementation will throw an UnsupportedOperationException if the iterator returned by the iterator method does not implement the remove method and this collection contains one or more elements not present in the specified collection.

        Specified by:
        retainAll in interface Collection<E>
        Specified by:
        retainAll in interface List<E>
        Overrides:
        retainAll in class AbstractCollection<E>
        Parameters:
        c - collection containing elements to be retained in this collection
        Returns:
        true if this collection changed as a result of the call
        See Also:
        AbstractCollection.remove(Object), AbstractCollection.contains(Object)
      • ensureIsMutable

        protected void ensureIsMutable()
        Throws an UnsupportedOperationException if the list is immutable. Subclasses are responsible for invoking this method on mutate operations.