public class LazyStringArrayList extends AbstractList<E> implements LazyStringList, RandomAccess
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.
| Modifier and Type | Field and Description |
|---|---|
protected static int |
DEFAULT_CAPACITY |
modCount| Constructor and Description |
|---|
LazyStringArrayList() |
LazyStringArrayList(int initialCapacity) |
LazyStringArrayList(LazyStringList from) |
LazyStringArrayList(List<String> from) |
| Modifier and Type | Method and Description |
|---|---|
void |
add(byte[] element)
Appends the specified element to the end of this list (optional operation).
|
void |
add(ByteString element)
Appends the specified element to the end of this list (optional operation).
|
void |
add(int index,
String element)
Inserts the specified element at the specified position in this list (optional operation).
|
boolean |
add(String element)
Appends the specified element to the end of this list (optional operation).
|
boolean |
addAll(Collection<? extends String> c)
Adds all of the elements in the specified collection to this collection (optional operation).
|
boolean |
addAll(int index,
Collection<? extends String> c)
Inserts all of the elements in the specified collection into this list at the specified position
(optional operation).
|
boolean |
addAllByteArray(Collection<byte[]> c)
Appends all elements in the specified byte[] collection to the end of this list.
|
boolean |
addAllByteString(Collection<? extends ByteString> values)
Appends all elements in the specified ByteString collection to the end of this list.
|
List<byte[]> |
asByteArrayList()
Returns a mutable view of this list.
|
List<ByteString> |
asByteStringList()
Returns a view of the data as a list of ByteStrings.
|
void |
clear()
Removes all of the elements from this list (optional operation).
|
static LazyStringArrayList |
emptyList()
Returns an empty immutable
LazyStringArrayList instance |
protected void |
ensureIsMutable()
Throws an
UnsupportedOperationException if the list is immutable. |
boolean |
equals(Object o)
Compares the specified object with this list for equality.
|
String |
get(int index)
Returns the element at the specified position in this list.
|
byte[] |
getByteArray(int index)
Returns the element at the specified position in this list as byte[].
|
ByteString |
getByteString(int index)
Returns the element at the specified position in this list as a ByteString.
|
Object |
getRaw(int index)
Returns the element at the specified position in this list as an Object that will either be a String or a
ByteString.
|
List<?> |
getUnderlyingElements()
Returns an unmodifiable List of the underlying elements, each of which is either a
String or its equivalent UTF-8 encoded ByteString or byte[]. |
LazyStringList |
getUnmodifiableView()
Returns an unmodifiable view of the list.
|
int |
hashCode()
Returns the hash code value for this list.
|
boolean |
isModifiable()
Returns whether this list can be modified via the publicly accessible
List methods. |
void |
makeImmutable()
Makes this list immutable.
|
void |
mergeFrom(LazyStringList other)
Merges all elements from another LazyStringList into this one.
|
LazyStringArrayList |
mutableCopyWithCapacity(int capacity)
Returns a mutable clone of this list with the specified capacity.
|
String |
remove(int index)
Removes the element at the specified position in this list (optional operation).
|
boolean |
remove(Object o)
Removes a single instance of the specified element from this collection, if it is present
(optional operation).
|
boolean |
removeAll(Collection<?> c)
Removes all of this collection's elements that are also contained in the specified collection
(optional operation).
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection
(optional operation).
|
void |
set(int index,
byte[] s)
Replaces the element at the specified position in this list with the specified element (optional operation).
|
void |
set(int index,
ByteString s)
Replaces the element at the specified position in this list with the specified element (optional operation).
|
String |
set(int index,
String s)
Replaces the element at the specified position in this list with the specified element (optional
operation).
|
int |
size()
Returns the number of elements in this collection.
|
indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subListcontains, containsAll, isEmpty, toArray, toArray, toStringcontains, containsAll, equals, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeAll, retainAll, subList, toArray, toArrayprotected static final int DEFAULT_CAPACITY
public LazyStringArrayList()
public LazyStringArrayList(int initialCapacity)
public LazyStringArrayList(LazyStringList from)
public void add(byte[] element)
LazyStringListadd in interface LazyStringListelement - element to be appended to this listpublic void add(ByteString element)
LazyStringListadd in interface LazyStringListelement - element to be appended to this listpublic void add(int index,
String element)
AbstractList
This implementation always throws an UnsupportedOperationException.
@CanIgnoreReturnValue public boolean add(String element)
AbstractListLists 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.
add in interface Collection<String>add in interface List<String>element - element to be appended to this listtrue (as specified by Collection.add(E))@CanIgnoreReturnValue public boolean addAll(Collection<? extends String> c)
AbstractCollectionThis 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).
addAll in interface Collection<String>addAll in interface List<String>c - collection containing elements to be added to this collectiontrue if this collection changed as a result of the callAbstractCollection.add(Object)@CanIgnoreReturnValue
public boolean addAll(int index,
Collection<? extends String> c)
AbstractList
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.
@CanIgnoreReturnValue public boolean addAllByteArray(Collection<byte[]> c)
LazyStringListaddAllByteArray in interface LazyStringListc - collection whose elements are to be added to this list@CanIgnoreReturnValue public boolean addAllByteString(Collection<? extends ByteString> values)
LazyStringListaddAllByteString in interface LazyStringListvalues - collection whose elements are to be added to this listpublic List<byte[]> asByteArrayList()
LazyStringListasByteArrayList in interface LazyStringListpublic List<ByteString> asByteStringList()
ProtocolStringListasByteStringList in interface ProtocolStringListpublic void clear()
AbstractList
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.
public static LazyStringArrayList emptyList()
LazyStringArrayList instancepublic String get(int index)
AbstractListpublic byte[] getByteArray(int index)
LazyStringListgetByteArray in interface LazyStringListindex - index of the element to returnpublic ByteString getByteString(int index)
LazyStringListgetByteString in interface LazyStringListindex - index of the element to returnpublic Object getRaw(int index)
LazyStringListgetRaw in interface LazyStringListindex - index of the element to returnpublic List<?> getUnderlyingElements()
LazyStringListString 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.getUnderlyingElements in interface LazyStringListpublic LazyStringList getUnmodifiableView()
LazyStringListgetUnmodifiableView in interface LazyStringListpublic void mergeFrom(LazyStringList other)
LazyStringListList.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.mergeFrom in interface LazyStringListpublic LazyStringArrayList mutableCopyWithCapacity(int capacity)
Internal.ProtobufListmutableCopyWithCapacity in interface Internal.ProtobufList<String>@CanIgnoreReturnValue public String remove(int index)
AbstractList
This implementation always throws an UnsupportedOperationException.
public void set(int index,
byte[] s)
LazyStringListset in interface LazyStringListindex - index of the element to replaces - the element to be stored at the specified positionpublic void set(int index,
ByteString s)
LazyStringListset in interface LazyStringListindex - index of the element to replaces - the element to be stored at the specified position@CanIgnoreReturnValue public String set(int index, String s)
AbstractList
This implementation always throws an UnsupportedOperationException.
public int size()
AbstractCollectionInteger.MAX_VALUE elements, returns Integer.MAX_VALUE.size in interface Collection<String>size in interface List<String>size in class AbstractCollection<String>protected void ensureIsMutable()
UnsupportedOperationException if the list is immutable. Subclasses are responsible for invoking
this method on mutate operations.public boolean equals(Object o)
AbstractListtrue 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.
equals in interface Collection<E>equals in interface List<E>equals in class AbstractList<E>o - the object to be compared for equality with this listtrue if the specified object is equal to this listObject.hashCode(),
HashMappublic int hashCode()
AbstractList
This implementation uses exactly the code that is used to define the list hash function in the
documentation for the List.hashCode() method.
hashCode in interface Collection<E>hashCode in interface List<E>hashCode in class AbstractList<E>Object.equals(java.lang.Object),
System.identityHashCode(java.lang.Object)public boolean isModifiable()
Internal.ProtobufListList methods.isModifiable in interface Internal.ProtobufList<E>public final void makeImmutable()
Internal.ProtobufListUnsupportedOperationException.makeImmutable in interface Internal.ProtobufList<E>@CanIgnoreReturnValue public boolean remove(Object o)
AbstractCollectione 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.
remove in interface Collection<E>remove in interface List<E>remove in class AbstractCollection<E>o - element to be removed from this collection, if presenttrue if an element was removed as a result of this call@CanIgnoreReturnValue public boolean removeAll(Collection<?> c)
AbstractCollection
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.
removeAll in interface Collection<E>removeAll in interface List<E>removeAll in class AbstractCollection<E>c - collection containing elements to be removed from this collectiontrue if this collection changed as a result of the callAbstractCollection.remove(Object),
AbstractCollection.contains(Object)@CanIgnoreReturnValue public boolean retainAll(Collection<?> c)
AbstractCollection
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.
retainAll in interface Collection<E>retainAll in interface List<E>retainAll in class AbstractCollection<E>c - collection containing elements to be retained in this collectiontrue if this collection changed as a result of the callAbstractCollection.remove(Object),
AbstractCollection.contains(Object)