Added a clone() method and implement Cloneable.

Removed whitespace from the ends of lines.
This commit is contained in:
Paul Speed 2016-03-27 04:51:31 -04:00
parent 7b29c58fe0
commit c6aac78f42

View File

@ -68,7 +68,7 @@ import java.util.*;
* @version $Revision$
* @author Paul Speed
*/
public class SafeArrayList<E> implements List<E> {
public class SafeArrayList<E> implements List<E>, Cloneable {
// Implementing List directly to avoid accidentally acquiring
// incorrect or non-optimal behavior from AbstractList. For
@ -97,6 +97,24 @@ public class SafeArrayList<E> implements List<E> {
addAll(c);
}
public SafeArrayList<E> clone() {
try {
SafeArrayList<E> clone = (SafeArrayList<E>)super.clone();
// Clone whichever backing store is currently active
if( backingArray != null ) {
clone.backingArray = backingArray.clone();
}
if( buffer != null ) {
clone.buffer = (List<E>)((ArrayList<E>)buffer).clone();
}
return clone;
} catch( CloneNotSupportedException e ) {
throw new AssertionError();
}
}
protected final <T> T[] createArray(Class<T> type, int size) {
return (T[])java.lang.reflect.Array.newInstance(type, size);
}