Package Torello.Java.ReadOnly
Class ROArrayListBuilder<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<E>
-
- Torello.Java.ReadOnly.ROArrayListBuilder<E>
-
- Type Parameters:
E
- the type of elements in this list
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.List<E>
,java.util.RandomAccess
public final class ROArrayListBuilder<E> extends java.util.ArrayList<E> implements java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable
This class was originally copied fromGitHub's Open-JDK
Account. Though the original file has been modified, few changes were applied to the Javadoc Commenting. Method and parameter names & types have not been modified whatsoever. This file may be viewed on the GitHub Archive for Java Packagejava.util.*
The Original'.java'
Source-File's Header-Copyright Information is included here:File Copyright
. Within that Copyright Notice, it is suggested that a copy of theGNU Public License V2
also be included alongside.A Copy of Java'sArrayList
class; used for building aReadOnlyArrayList
. Maintains an internal and inaccessibleArrayList<E>
instance. Upon build completion, this instance is passed to theReadOnlyArrayList
instance and stored there.
The internal data-structure is not exposed by any method provided in this API. It is guaranteed that the contents of aReadOnlyArrayList
will not be modified.
Note: The "RO Builder Classes" are somewhat superfluous (a little)
The art of any solid "Read-Only" Data-Structure Class-API is providing an intelligent means of actually inserting Data into a supposedly-read-only class. In this package, the most direct and efficient way of doing so is just to use one of the myriad constructors offered by the ReadOnly Data-Classes.
Another sound way of creating any "ReadOnly-XX" Data-Structure, is to simply populate the correspondingjava.util.XX
class instance, and pass that instance yourReadOnly-X
Constructor. The constructor actuallycopies
the data out of the original structure, and saves it to its own private & internal data-structure in order to guarantee the immutability contract.
But what about a situation where there is anArrayList
that is being populated by a very large number elements? It would likely be of benefit to skip the data-copying step for performance reasons! If so, then a builder can improve performance quite a bit. The real value of using a "Builder" rather than one of theReadOnlyArrayList
constructors is that this Builder'sbuild()
method doesn't actually have any kind of data-copy step at all!
Also, of some interest, is that this class inherits the original Java-Classjava.util.ArrayList
(explained further, below), making it extremely easy to use.
Inheritsjava.util.ArrayList
This class may be passed to any Data-Building Method that accepts aArrayList
, because it inherits from that class. Any implementation that can populate aArrayList
can also populate this builder.
Efficiency Improvement:
This class is nothing but a set of wrapper methods. It is being provided as an alternate, and possibly more efficient, way to construct aReadOnlyArrayList
.
This class inherits the Standard JDK Collection-Framework Classjava.util.ArrayList
, and adds a singleboolean
field named'built'
that, once switched to'true'
, will block any subsequent attempts to mutate the underlying data-structure.
With anArrayList
having more than, say, 10,000 items, the cost of copying the internalArrayList
(which is necessary to construct any Read-Only Class) would perhaps be much too high. Instead, by making use of classROArrayListBuilder
, there is no need to run any kind of internal-data data-copying step at all.
Simply put all data into the Builder, using any / all standard Java-JDKArrayList
methods, and when thebuild()
method is invoked, an internal flag is set which will wholly prohibit any further mutation of the data in your builder - thereby allowing the Builder, itself, to be used as theReadOnlyArrayList
's internal Data-Structure.- See Also:
ReadOnlyArrayList
, Serialized Form
Hi-Lited Source-Code:- View Here: Torello/Java/ReadOnly/ROArrayListBuilder.java
- Open New Browser-Tab: Torello/Java/ReadOnly/ROArrayListBuilder.java
File Size: 21,874 Bytes Line Count: 505 '\n' Characters Found
-
-
Field Summary
Fields Modifier and Type Field Description protected static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description ROArrayListBuilder()
Constructs an empty list with an initial capacity of ten.ROArrayListBuilder(int initialCapacity)
Constructs an emptyROArrayListBuilder
with the specified initial capacity.ROArrayListBuilder(Collection<? extends E> c)
Constructs aROArrayListBuilder
containing the elements of the specified collection, in the order they are returned by the collection's iterator.
-
Method Summary
Convert this Builder into a ReadOnlyArrayList instance Modifier and Type Method ReadOnlyArrayList<E>
build()
Simply transfers'this'
instance' internalArrayList
to theReadOnlyArrayList
Wrapper-Class.Add Items to this Read-Only-List Builder Modifier and Type Method void
add(int index, E element)
Inserts the specified element at the specified position in this list.boolean
add(E e)
Appends the specified element to the end of this list.boolean
addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.boolean
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.Remove Items from this Read-Only-List Builder Modifier and Type Method void
clear()
Removes all of the elements from this list.E
remove(int index)
Removes the element at the specified position in this list.boolean
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.boolean
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.boolean
removeIf(Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate.boolean
retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection.Replace List-Builder Items Modifier and Type Method void
replaceAll(UnaryOperator<E> operator)
Replaces each element of this list with the result of applying the operator to that element.E
set(int index, E element)
Replaces the element at the specified position in this list with the specified element.JDK Standard ArrayList Methods Modifier and Type Method void
ensureCapacity(int minCapacity)
Increases the capacity of thisArrayList
instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.void
sort(Comparator<? super E> c)
Sorts this list according to the order induced by the specified Comparator.void
trimToSize()
Trims the capacity of thisArrayList
instance to be the list's current size.Mutable / Read-Write View Generators - Return Instances with Unmodifiable-Wrappers Modifier and Type Method ListIterator<E>
listIterator()
Restricted-Access InstanceListIterator<E>
listIterator(int index)
Restricted-Access InstanceList<E>
subList(int fromIndex, int toIndex)
Restricted-Access InstanceMethods: class java.lang.Object Modifier and Type Method boolean
equals(Object o)
Compares the specified Object with this Builder for equality, as per the definition in the original classArrayList
.Methods: interface java.lang.Iterable Modifier and Type Method RemoveUnsupportedIterator<E>
iterator()
Restricted-Access InstanceMethods: interface java.lang.Cloneable Modifier and Type Method ROArrayListBuilder<E>
clone()
Clones this instance' ofROArrayListBuilder
.-
Methods inherited from class java.util.ArrayList
contains, forEach, get, hashCode, indexOf, isEmpty, lastIndexOf, removeRange, size, spliterator, toArray, toArray
-
-
-
-
Field Detail
-
serialVersionUID
protected static final long serialVersionUID
This fulfils the SerialVersion UID requirement for all classes that implement Java'sinterface java.io.Serializable
. Using theSerializable
Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
protected static final long serialVersionUID = 1;
-
-
Constructor Detail
-
ROArrayListBuilder
public ROArrayListBuilder(int initialCapacity)
Constructs an emptyROArrayListBuilder
with the specified initial capacity.- Parameters:
initialCapacity
- the initial capacity of the list- Throws:
java.lang.IllegalArgumentException
- if the specified initial capacity is negative- Code:
- Exact Constructor Body:
super(initialCapacity);
-
ROArrayListBuilder
public ROArrayListBuilder()
Constructs an empty list with an initial capacity of ten.
-
ROArrayListBuilder
public ROArrayListBuilder(java.util.Collection<? extends E> c)
Constructs aROArrayListBuilder
containing the elements of the specified collection, in the order they are returned by the collection's iterator.- Parameters:
c
- the collection whose elements are to be placed into this list- Throws:
java.lang.NullPointerException
- if the specified collection is null- Code:
- Exact Constructor Body:
super(c);
-
-
Method Detail
-
build
public ReadOnlyArrayList<E> build()
Simply transfers'this'
instance' internalArrayList
to theReadOnlyArrayList
Wrapper-Class.- Returns:
- a newly constructed
ReadOnlyArrayList
"Wrapper-Class", shielding the internal'arrayList'
private-field from any modification. - Code:
- Exact Method Body:
super.trimToSize(); this.built = true; return (size() == 0) ? ReadOnlyArrayList.emptyROAL() : new ReadOnlyArrayList<>(this, friendClassBadge);
-
trimToSize
public void trimToSize()
Trims the capacity of thisArrayList
instance to be the list's current size. An application can use this operation to minimize the storage of anArrayList
instance.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Overrides:
trimToSize
in classjava.util.ArrayList<E>
- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); super.trimToSize();
-
ensureCapacity
public void ensureCapacity(int minCapacity)
Increases the capacity of thisArrayList
instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Overrides:
ensureCapacity
in classjava.util.ArrayList<E>
- Parameters:
minCapacity
- the desired minimum capacity- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); super.ensureCapacity(minCapacity);
-
set
public E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
set
in interfacejava.util.List<E>
- Overrides:
set
in classjava.util.ArrayList<E>
- Parameters:
index
- index of the element to replaceelement
- element to be stored at the specified position- Returns:
- the element previously at the specified position
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is out of range(index < 0 || index > size())
- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.set(index, element);
-
add
public boolean add(E e)
Appends the specified element to the end of this list.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
add
in interfacejava.util.Collection<E>
- Specified by:
add
in interfacejava.util.List<E>
- Overrides:
add
in classjava.util.ArrayList<E>
- Parameters:
e
- element to be appended to this list- Returns:
TRUE
(as specified byCollection.add
)- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.add(e);
-
add
public void add(int index, E element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
add
in interfacejava.util.List<E>
- Overrides:
add
in classjava.util.ArrayList<E>
- Parameters:
index
- index at which the specified element is to be insertedelement
- element to be inserted- Throws:
java.lang.IndexOutOfBoundsException
- if the index is out of range(index < 0 || index >= size())
- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); super.add(index, element);
-
remove
public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
remove
in interfacejava.util.List<E>
- Overrides:
remove
in classjava.util.ArrayList<E>
- Parameters:
index
- the index of the element to be removed- Returns:
- the element that was removed from the list
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is out of range(index < 0 || index >= size())
- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.remove(index);
-
remove
public boolean remove(java.lang.Object o)
Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest indexi
such thatObjects.equals(o, get(i))
(if such an element exists). ReturnsTRUE
if this list contained the specified element (or equivalently, if this list changed as a result of the call).
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
remove
in interfacejava.util.Collection<E>
- Specified by:
remove
in interfacejava.util.List<E>
- Overrides:
remove
in classjava.util.ArrayList<E>
- Parameters:
o
- element to be removed from this list, if present- Returns:
TRUE
if this list contained the specified element- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.remove(o);
-
clear
public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.
-
addAll
public boolean addAll(java.util.Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of 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. (This implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
addAll
in interfacejava.util.Collection<E>
- Specified by:
addAll
in interfacejava.util.List<E>
- Overrides:
addAll
in classjava.util.ArrayList<E>
- Parameters:
c
- collection containing elements to be added to this list- Returns:
TRUE
if this list changed as a result of the call- Throws:
java.lang.NullPointerException
- if the specified collection is null- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.addAll(c);
-
addAll
public boolean addAll(int index, java.util.Collection<? extends E> c)
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.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
addAll
in interfacejava.util.List<E>
- Overrides:
addAll
in classjava.util.ArrayList<E>
- Parameters:
index
- index at which to insert the first element from the specified collectionc
- collection containing elements to be added to this list- Returns:
TRUE
if this list changed as a result of the call- Throws:
java.lang.NullPointerException
- if the specified collection is nulljava.lang.IndexOutOfBoundsException
- if the index is out of range(index < 0 || index > size())
- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.addAll(index, c);
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
removeAll
in interfacejava.util.Collection<E>
- Specified by:
removeAll
in interfacejava.util.List<E>
- Overrides:
removeAll
in classjava.util.ArrayList<E>
- Parameters:
c
- collection containing elements to be removed from this list- Returns:
TRUE
if this list changed as a result of the call- Throws:
java.lang.ClassCastException
- if the class of an element of this list is incompatible with the specified collection (optional)java.lang.NullPointerException
- if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null- See Also:
ReadOnlyCollection.contains(Object)
- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.removeAll(c);
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
retainAll
in interfacejava.util.Collection<E>
- Specified by:
retainAll
in interfacejava.util.List<E>
- Overrides:
retainAll
in classjava.util.ArrayList<E>
- Parameters:
c
- collection containing elements to be retained in this list- Returns:
TRUE
if this list changed as a result of the call- Throws:
java.lang.ClassCastException
- if the class of an element of this list is incompatible with the specified collection (optional)java.lang.NullPointerException
- if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null- See Also:
ReadOnlyCollection.contains(Object)
- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.retainAll(c);
-
removeIf
public boolean removeIf(java.util.function.Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
removeIf
in interfacejava.util.Collection<E>
- Overrides:
removeIf
in classjava.util.ArrayList<E>
- Parameters:
filter
- a predicate which returns true for elements to be removed- Returns:
TRUE
if any elements were removed- Throws:
java.lang.NullPointerException
- If parameter'filter'
has been passed null.- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); return super.removeIf(filter);
-
replaceAll
public void replaceAll(java.util.function.UnaryOperator<E> operator)
Replaces each element of this list with the result of applying the operator to that element. Errors or runtime exceptions thrown by the operator are relayed to the caller.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
replaceAll
in interfacejava.util.List<E>
- Overrides:
replaceAll
in classjava.util.ArrayList<E>
- Parameters:
operator
- the operator to apply to each element- Throws:
java.lang.NullPointerException
- - if the specified operator is null or if the operator result is a null value and this list does not permit null elements (optional)- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); super.replaceAll(operator);
-
sort
public void sort(java.util.Comparator<? super E> c)
Sorts this list according to the order induced by the specified Comparator. The sort is stable: this method must not reorder equal elements.
All elements in this list must be mutually comparable using the specified comparator (that is,c.compare(e1, e2)
must not throw aClassCastException
for any elements e1 and e2 in the list).
If the specified comparator is null then all elements in this list must implement the Comparable interface and the elements' natural ordering should be used.
This list must be modifiable, but need not be resizable.
Mutator Method:
This method modifies the contents of this class' internalArrayList
. Note that any method which modifies this internalArrayList
field will throw an exception if invoked after a call tobuild()
.- Specified by:
sort
in interfacejava.util.List<E>
- Overrides:
sort
in classjava.util.ArrayList<E>
- Parameters:
c
- the Comparator used to compare list elements. A null value indicates that the elements' natural ordering should be used- Throws:
java.lang.ClassCastException
- if the list contains elements that are not mutually comparable using the specified comparatorjava.lang.IllegalArgumentException
- (optional) if the comparator is found to violate theComparator
contract- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROAL); super.sort(c);
-
iterator
public RemoveUnsupportedIterator<E> iterator()
Restricts a back-door into the underlying data-structure.
UnsupportedOperationException Specifics:
This method invokes the Java StandardCollections
class static-method for 'wrapping' this method's returnedIterator
in an Immutable-Wrapper. Java's Collections Framework Immutable-Wrappers have been around since prior to JDK 8, and are easy to use. The wrapper utilized here is needed because this particular method returns a Read-Write "View" into the underlyingArrayList
that facilitates modifying data inside this class' instances.
Once'this'
builder instance has been built into a Read-Only Data-Structure, thisIterator
(which possesses methods for mutating theArrayList
) would provide a potential back-door for breaking the Immutable-Contract of theReadOnlyArrayList
that is ultimately built.
Remember that all sub-sets and sub-maps returned by the Java Collections Framework guarantee that any changes to the sub-set or sub-map will be reflected and translated back into the originalCollection
from whence they were created.
The returned instance is usable, but any method that would modify thisArrayList
-Builder will, instead, throw a JavaUnsupportedOperationException
.- Specified by:
iterator
in interfacejava.util.Collection<E>
- Specified by:
iterator
in interfacejava.lang.Iterable<E>
- Specified by:
iterator
in interfacejava.util.List<E>
- Overrides:
iterator
in classjava.util.ArrayList<E>
- Returns:
- a
java.util.Iterator
that cannot modify thisArrayList-Builder
- Code:
- Exact Method Body:
return new RemoveUnsupportedIterator<>(super.iterator());
-
listIterator
public java.util.ListIterator<E> listIterator()
Restricts a back-door into the underlying data-structure.
UnsupportedOperationException Specifics:
This method invokes the Java StandardCollections
class static-method for 'wrapping' this method's returnedListIterator
in an Immutable-Wrapper. Java's Collections Framework Immutable-Wrappers have been around since prior to JDK 8, and are easy to use. The wrapper utilized here is needed because this particular method returns a Read-Write "View" into the underlyingArrayList
that facilitates modifying data inside this class' instances.
Once'this'
builder instance has been built into a Read-Only Data-Structure, thisListIterator
(which possesses methods for mutating theArrayList
) would provide a potential back-door for breaking the Immutable-Contract of theReadOnlyArrayList
that is ultimately built.
Remember that all sub-sets and sub-maps returned by the Java Collections Framework guarantee that any changes to the sub-set or sub-map will be reflected and translated back into the originalCollection
from whence they were created.
The returned instance is usable, but any method that would modify thisArrayList
-Builder will, instead, throw a JavaUnsupportedOperationException
.
-
listIterator
public java.util.ListIterator<E> listIterator(int index)
Restricts a back-door into the underlying data-structure.
UnsupportedOperationException Specifics:
This method invokes the Java StandardCollections
class static-method for 'wrapping' this method's returnedListIterator
in an Immutable-Wrapper. Java's Collections Framework Immutable-Wrappers have been around since prior to JDK 8, and are easy to use. The wrapper utilized here is needed because this particular method returns a Read-Write "View" into the underlyingArrayList
that facilitates modifying data inside this class' instances.
Once'this'
builder instance has been built into a Read-Only Data-Structure, thisListIterator
(which possesses methods for mutating theArrayList
) would provide a potential back-door for breaking the Immutable-Contract of theReadOnlyArrayList
that is ultimately built.
Remember that all sub-sets and sub-maps returned by the Java Collections Framework guarantee that any changes to the sub-set or sub-map will be reflected and translated back into the originalCollection
from whence they were created.
The returned instance is usable, but any method that would modify thisArrayList
-Builder will, instead, throw a JavaUnsupportedOperationException
.
-
subList
public java.util.List<E> subList(int fromIndex, int toIndex)
Restricts a back-door into the underlying data-structure.
UnsupportedOperationException Specifics:
This method invokes the Java StandardCollections
class static-method for 'wrapping' this method's returnedList
in an Immutable-Wrapper. Java's Collections Framework Immutable-Wrappers have been around since prior to JDK 8, and are easy to use. The wrapper utilized here is needed because this particular method returns a Read-Write "View" into the underlyingArrayList
that facilitates modifying data inside this class' instances.
Once'this'
builder instance has been built into a Read-Only Data-Structure, thisList
(which possesses methods for mutating theArrayList
) would provide a potential back-door for breaking the Immutable-Contract of theReadOnlyArrayList
that is ultimately built.
Remember that all sub-sets and sub-maps returned by the Java Collections Framework guarantee that any changes to the sub-set or sub-map will be reflected and translated back into the originalCollection
from whence they were created.
The returned instance is usable, but any method that would modify thisArrayList
-Builder will, instead, throw a JavaUnsupportedOperationException
.
-
equals
public boolean equals(java.lang.Object o)
Compares the specified Object with this Builder for equality, as per the definition in the original classjava.util.ArrayList
. Ignores the private field'built'
. If'this'
has not been built, but'o'
has been, that fact is ignored during the equality-comparison.- Specified by:
equals
in interfacejava.util.Collection<E>
- Specified by:
equals
in interfacejava.util.List<E>
- Overrides:
equals
in classjava.util.ArrayList<E>
- Parameters:
o
- object to be compared for equality with thisROArrayListBuilder
instance- Returns:
- true if the specified Object is equal to this Builder
- Code:
- Exact Method Body:
if (this == o) return true; if (! (o instanceof ROArrayListBuilder)) return false; return super.equals((ArrayList) o);
-
clone
public ROArrayListBuilder<E> clone()
Clones this instance' ofROArrayListBuilder
.
The clone that's returned has had it's internal'built'
boolean-flag setFALSE
- Overrides:
clone
in classjava.util.ArrayList<E>
- Returns:
- a clone of this builder
- Code:
- Exact Method Body:
return new ROArrayListBuilder<>(this);
-
-