Class LazyStringArrayList
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- com.google.protobuf.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 ofLazyStringListthat wraps anArrayList. Each element is one ofString,ByteString, orbyte[]. 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
ArrayListinstance 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.
-
-
Field Summary
Fields Modifier and Type Field Description protected static intDEFAULT_CAPACITY-
Fields inherited from class java.util.AbstractList
modCount
-
-
Constructor Summary
Constructors Constructor Description LazyStringArrayList()LazyStringArrayList(int initialCapacity)LazyStringArrayList(LazyStringList from)LazyStringArrayList(List<String> from)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(byte[] element)Appends the specified element to the end of this list (optional operation).voidadd(int index, String element)Inserts the specified element at the specified position in this list (optional operation).voidadd(ByteString element)Appends the specified element to the end of this list (optional operation).booleanadd(String element)Appends the specified element to the end of this list (optional operation).booleanaddAll(int index, Collection<? extends String> c)Inserts all of the elements in the specified collection into this list at the specified position (optional operation).booleanaddAll(Collection<? extends String> c)Adds all of the elements in the specified collection to this collection (optional operation).booleanaddAllByteArray(Collection<byte[]> c)Appends all elements in the specified byte[] collection to the end of this list.booleanaddAllByteString(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.voidclear()Removes all of the elements from this list (optional operation).static LazyStringArrayListemptyList()Returns an empty immutableLazyStringArrayListinstanceprotected voidensureIsMutable()Throws anUnsupportedOperationExceptionif the list is immutable.booleanequals(Object o)Compares the specified object with this list for equality.Stringget(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[].ByteStringgetByteString(int index)Returns the element at the specified position in this list as a ByteString.ObjectgetRaw(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 aStringor its equivalent UTF-8 encodedByteStringor byte[].LazyStringListgetUnmodifiableView()Returns an unmodifiable view of the list.inthashCode()Returns the hash code value for this list.booleanisModifiable()Returns whether this list can be modified via the publicly accessibleListmethods.voidmakeImmutable()Makes this list immutable.voidmergeFrom(LazyStringList other)Merges all elements from another LazyStringList into this one.LazyStringArrayListmutableCopyWithCapacity(int capacity)Returns a mutable clone of this list with the specified capacity.Stringremove(int index)Removes the element at the specified position in this list (optional operation).booleanremove(Object o)Removes a single instance of the specified element from this collection, if it is present (optional operation).booleanremoveAll(Collection<?> c)Removes all of this collection's elements that are also contained in the specified collection (optional operation).booleanretainAll(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).voidset(int index, byte[] s)Replaces the element at the specified position in this list with the specified element (optional operation).voidset(int index, ByteString s)Replaces the element at the specified position in this list with the specified element (optional operation).Stringset(int index, String s)Replaces the element at the specified position in this list with the specified element (optional operation).intsize()Returns the number of elements in this collection.-
Methods inherited from class java.util.AbstractList
indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subList
-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, toArray, toArray, toString
-
Methods inherited from interface java.util.List
contains, containsAll, equals, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeAll, retainAll, subList, toArray, toArray
-
-
-
-
Field Detail
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
LazyStringArrayList
public LazyStringArrayList()
-
LazyStringArrayList
public LazyStringArrayList(int initialCapacity)
-
LazyStringArrayList
public LazyStringArrayList(LazyStringList from)
-
-
Method Detail
-
emptyList
public static LazyStringArrayList emptyList()
Returns an empty immutableLazyStringArrayListinstance
-
mutableCopyWithCapacity
public LazyStringArrayList mutableCopyWithCapacity(int capacity)
Description copied from interface:Internal.ProtobufListReturns a mutable clone of this list with the specified capacity.- Specified by:
mutableCopyWithCapacityin interfaceInternal.ProtobufList<String>
-
get
public String get(int index)
Description copied from class:AbstractListReturns the element at the specified position in this list.
-
size
public int size()
Description copied from class:AbstractCollectionReturns the number of elements in this collection. If this collection contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.- Specified by:
sizein interfaceCollection<String>- Specified by:
sizein interfaceList<String>- Specified by:
sizein classAbstractCollection<String>- Returns:
- the number of elements in this collection
-
add
public void add(int index, String element)Description copied from class:AbstractListInserts 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.
-
add
@CanIgnoreReturnValue public boolean add(String element)
Description copied from class:AbstractListAppends 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
UnsupportedOperationExceptionunlessadd(int, E)is overridden.- Specified by:
addin interfaceCollection<String>- Specified by:
addin interfaceList<String>- Parameters:
element- element to be appended to this list- Returns:
true(as specified byCollection.add(E))
-
add
public void add(ByteString element)
Description copied from interface:LazyStringListAppends the specified element to the end of this list (optional operation).- Specified by:
addin interfaceLazyStringList- Parameters:
element- element to be appended to this list
-
add
public void add(byte[] element)
Description copied from interface:LazyStringListAppends the specified element to the end of this list (optional operation).- Specified by:
addin interfaceLazyStringList- Parameters:
element- element to be appended to this list
-
addAll
@CanIgnoreReturnValue public boolean addAll(Collection<? extends String> c)
Description copied from class:AbstractCollectionAdds 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
UnsupportedOperationExceptionunlessaddis overridden (assuming the specified collection is non-empty).- Specified by:
addAllin interfaceCollection<String>- Specified by:
addAllin interfaceList<String>- Parameters:
c- collection containing elements to be added to this collection- Returns:
trueif 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:AbstractListInserts 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
UnsupportedOperationExceptionunlessadd(int, E)is overridden.
-
addAllByteString
@CanIgnoreReturnValue public boolean addAllByteString(Collection<? extends ByteString> values)
Description copied from interface:LazyStringListAppends all elements in the specified ByteString collection to the end of this list.- Specified by:
addAllByteStringin interfaceLazyStringList- 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:LazyStringListAppends all elements in the specified byte[] collection to the end of this list.- Specified by:
addAllByteArrayin interfaceLazyStringList- 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:AbstractListRemoves 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.
-
clear
public void clear()
Description copied from class:AbstractListRemoves 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
UnsupportedOperationExceptionunlessremove(int index)orremoveRange(int fromIndex, int toIndex)is overridden.
-
getRaw
public Object getRaw(int index)
Description copied from interface:LazyStringListReturns the element at the specified position in this list as an Object that will either be a String or a ByteString.- Specified by:
getRawin interfaceLazyStringList- 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:LazyStringListReturns the element at the specified position in this list as a ByteString.- Specified by:
getByteStringin interfaceLazyStringList- 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:LazyStringListReturns the element at the specified position in this list as byte[].- Specified by:
getByteArrayin interfaceLazyStringList- 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:AbstractListReplaces the element at the specified position in this list with the specified element (optional operation).This implementation always throws an
UnsupportedOperationException.
-
set
public void set(int index, ByteString s)Description copied from interface:LazyStringListReplaces the element at the specified position in this list with the specified element (optional operation).- Specified by:
setin interfaceLazyStringList- Parameters:
index- index of the element to replaces- the element to be stored at the specified position
-
set
public void set(int index, byte[] s)Description copied from interface:LazyStringListReplaces the element at the specified position in this list with the specified element (optional operation).- Specified by:
setin interfaceLazyStringList- Parameters:
index- index of the element to replaces- the element to be stored at the specified position
-
getUnderlyingElements
public List<?> getUnderlyingElements()
Description copied from interface:LazyStringListReturns an unmodifiable List of the underlying elements, each of which is either aStringor its equivalent UTF-8 encodedByteStringor byte[]. It is an error for the caller to modify the returned List, and attempting to do so will result in anUnsupportedOperationException.- Specified by:
getUnderlyingElementsin interfaceLazyStringList
-
mergeFrom
public void mergeFrom(LazyStringList other)
Description copied from interface:LazyStringListMerges all elements from another LazyStringList into this one. This method differs fromList.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:
mergeFromin interfaceLazyStringList
-
asByteArrayList
public List<byte[]> asByteArrayList()
Description copied from interface:LazyStringListReturns 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:
asByteArrayListin interfaceLazyStringList
-
asByteStringList
public List<ByteString> asByteStringList()
Description copied from interface:ProtocolStringListReturns a view of the data as a list of ByteStrings.- Specified by:
asByteStringListin interfaceProtocolStringList
-
getUnmodifiableView
public LazyStringList getUnmodifiableView()
Description copied from interface:LazyStringListReturns an unmodifiable view of the list.- Specified by:
getUnmodifiableViewin interfaceLazyStringList
-
equals
public boolean equals(Object o)
Description copied from class:AbstractListCompares the specified object with this list for equality. Returnstrueif 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 elementse1ande2are 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 returnsfalse; if so, it iterates over both lists, comparing corresponding pairs of elements. If any comparison returnsfalse, this method returnsfalse. If either iterator runs out of elements before the other it returnsfalse(as the lists are of unequal length); otherwise it returnstruewhen the iterations complete.- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>- Parameters:
o- the object to be compared for equality with this list- Returns:
trueif the specified object is equal to this list- See Also:
Object.hashCode(),HashMap
-
hashCode
public int hashCode()
Description copied from class:AbstractListReturns the hash code value for this list.This implementation uses exactly the code that is used to define the list hash function in the documentation for the
List.hashCode()method.- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classAbstractList<E>- Returns:
- the hash code value for this list
- See Also:
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
-
isModifiable
public boolean isModifiable()
Description copied from interface:Internal.ProtobufListReturns whether this list can be modified via the publicly accessibleListmethods.- Specified by:
isModifiablein interfaceInternal.ProtobufList<E>
-
makeImmutable
public final void makeImmutable()
Description copied from interface:Internal.ProtobufListMakes this list immutable. All subsequent modifications will throw anUnsupportedOperationException.- Specified by:
makeImmutablein interfaceInternal.ProtobufList<E>
-
remove
@CanIgnoreReturnValue public boolean remove(Object o)
Description copied from class:AbstractCollectionRemoves a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an elementesuch that(o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returnstrueif 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
UnsupportedOperationExceptionif the iterator returned by this collection's iterator method does not implement theremovemethod and this collection contains the specified object.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
o- element to be removed from this collection, if present- Returns:
trueif an element was removed as a result of this call
-
removeAll
@CanIgnoreReturnValue public boolean removeAll(Collection<?> c)
Description copied from class:AbstractCollectionRemoves 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
removemethod.Note that this implementation will throw an
UnsupportedOperationExceptionif the iterator returned by theiteratormethod does not implement theremovemethod and this collection contains one or more elements in common with the specified collection.- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classAbstractCollection<E>- Parameters:
c- collection containing elements to be removed from this collection- Returns:
trueif 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:AbstractCollectionRetains 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
removemethod.Note that this implementation will throw an
UnsupportedOperationExceptionif the iterator returned by theiteratormethod does not implement theremovemethod and this collection contains one or more elements not present in the specified collection.- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Overrides:
retainAllin classAbstractCollection<E>- Parameters:
c- collection containing elements to be retained in this collection- Returns:
trueif this collection changed as a result of the call- See Also:
AbstractCollection.remove(Object),AbstractCollection.contains(Object)
-
ensureIsMutable
protected void ensureIsMutable()
Throws anUnsupportedOperationExceptionif the list is immutable. Subclasses are responsible for invoking this method on mutate operations.
-
-