Added a lit bit improvements for SafeArrayList:
1. improved comparing a SafeArrayList with another SafeArrayList. 2. improved the constructor by other collection 3. added a new constructor with init capacity.
This commit is contained in:
parent
43b52cb77c
commit
6e07a214c6
@ -92,9 +92,21 @@ public class SafeArrayList<E> implements List<E>, Cloneable {
|
|||||||
this.elementType = elementType;
|
this.elementType = elementType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SafeArrayList(Class<E> elementType, Collection<? extends E> c) {
|
public SafeArrayList(final Class<E> elementType, final int capacity) {
|
||||||
this.elementType = elementType;
|
this.elementType = elementType;
|
||||||
addAll(c);
|
this.buffer = new ArrayList<>(capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SafeArrayList(final Class<E> elementType, final Collection<? extends E> collection) {
|
||||||
|
this.elementType = elementType;
|
||||||
|
|
||||||
|
if (collection instanceof SafeArrayList) {
|
||||||
|
this.buffer = Arrays.asList(((SafeArrayList<E>) collection).getArray());
|
||||||
|
} else {
|
||||||
|
this.buffer = new ArrayList<>(collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.size = buffer.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SafeArrayList<E> clone() {
|
public SafeArrayList<E> clone() {
|
||||||
@ -150,12 +162,12 @@ public class SafeArrayList<E> implements List<E>, Cloneable {
|
|||||||
return buffer;
|
return buffer;
|
||||||
|
|
||||||
if( backingArray == null ) {
|
if( backingArray == null ) {
|
||||||
buffer = new ArrayList();
|
buffer = new ArrayList<>();
|
||||||
} else {
|
} else {
|
||||||
// Only keep the array or the buffer but never both at
|
// Only keep the array or the buffer but never both at
|
||||||
// the same time. 1) it saves space, 2) it keeps the rest
|
// the same time. 1) it saves space, 2) it keeps the rest
|
||||||
// of the code safer.
|
// of the code safer.
|
||||||
buffer = new ArrayList( Arrays.asList(backingArray) );
|
buffer = new ArrayList<>( Arrays.asList(backingArray) );
|
||||||
backingArray = null;
|
backingArray = null;
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
@ -243,10 +255,19 @@ public class SafeArrayList<E> implements List<E>, Cloneable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if( o == this )
|
|
||||||
|
if (o == this) {
|
||||||
return true;
|
return true;
|
||||||
if( !(o instanceof List) ) //covers null too
|
} else if (o instanceof SafeArrayList) {
|
||||||
|
|
||||||
|
final Object[] targetArray = ((SafeArrayList) o).getArray();
|
||||||
|
final E[] array = getArray();
|
||||||
|
|
||||||
|
return Arrays.equals(targetArray, array);
|
||||||
|
} else if (!(o instanceof List)) {//covers null too
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
List other = (List)o;
|
List other = (List)o;
|
||||||
Iterator i1 = iterator();
|
Iterator i1 = iterator();
|
||||||
Iterator i2 = other.iterator();
|
Iterator i2 = other.iterator();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user