|
|
@@ -66,9 +66,7 @@ public class NativeArrayList<T> extends AbstractList<T> implements RandomAccess
|
|
|
* @see ArrayUtils#wrap(Object)
|
|
|
*/
|
|
|
public NativeArrayList(Object array) {
|
|
|
- if (!array.getClass().isArray()) {
|
|
|
- throw new IllegalArgumentException(array + " is not an array");
|
|
|
- }
|
|
|
+ checkType(array.getClass());
|
|
|
this.array = array;
|
|
|
this.accessor = UnsafeArrayAccess.getInstance(array.getClass());
|
|
|
this.capacity = Array.getLength(array);
|
|
|
@@ -109,12 +107,8 @@ public class NativeArrayList<T> extends AbstractList<T> implements RandomAccess
|
|
|
* @see ArrayUtils#wrap(Class, Object)
|
|
|
*/
|
|
|
public NativeArrayList(Class<?> clazz, Object array) {
|
|
|
- if (!clazz.isArray()) {
|
|
|
- throw new IllegalArgumentException(clazz + " is not an array");
|
|
|
- }
|
|
|
- if (!array.getClass().isArray()) {
|
|
|
- throw new IllegalArgumentException(array + " is not an array");
|
|
|
- }
|
|
|
+ checkType(clazz);
|
|
|
+ checkType(array.getClass());
|
|
|
this.array = array;
|
|
|
this.accessor = UnsafeArrayAccess.getInstance(clazz, array.getClass());
|
|
|
int ascale = UnsafeAccess.API.arrayIndexScale(array.getClass());
|
|
|
@@ -164,32 +158,22 @@ public class NativeArrayList<T> extends AbstractList<T> implements RandomAccess
|
|
|
|
|
|
@Override
|
|
|
public T get(int index) {
|
|
|
- if(index >= size) {
|
|
|
- throw new IndexOutOfBoundsException(index +" out of bound " + size);
|
|
|
- }
|
|
|
+ checkBounds(index);
|
|
|
return (T) accessor.get(array, index);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public T set(int index, T element) {
|
|
|
- if(index >= capacity) {
|
|
|
- throw new IndexOutOfBoundsException(index + " out of maximal capacity " + capacity);
|
|
|
- }
|
|
|
- if(index >= size) {
|
|
|
- throw new IndexOutOfBoundsException(index +" out of bound " + size);
|
|
|
- }
|
|
|
+ checkCapacity(index);
|
|
|
+ checkBounds(index);
|
|
|
return (T) accessor.change(array, index, element);
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void add(int index, T value) {
|
|
|
- if(index >= capacity) {
|
|
|
- throw new IndexOutOfBoundsException(index + " out of maximal capacity " + capacity);
|
|
|
- }
|
|
|
- if(index > size) {
|
|
|
- throw new IndexOutOfBoundsException(index +" out of bound " + size);
|
|
|
- }
|
|
|
+ checkCapacity(index);
|
|
|
+ checkBounds(index);
|
|
|
System.arraycopy(array, index, array, index + 1, size - index);
|
|
|
size++;
|
|
|
accessor.put(array, index, value);
|
|
|
@@ -197,9 +181,7 @@ public class NativeArrayList<T> extends AbstractList<T> implements RandomAccess
|
|
|
|
|
|
@Override
|
|
|
public T remove(int index) {
|
|
|
- if(index >= size || index<0) {
|
|
|
- throw new IndexOutOfBoundsException(index +" out of bound " + size);
|
|
|
- }
|
|
|
+ checkBounds(index);
|
|
|
T prev = (T)accessor.get(array, index);
|
|
|
int shift = size-index-1;
|
|
|
if (shift > 0) {
|
|
|
@@ -217,6 +199,23 @@ public class NativeArrayList<T> extends AbstractList<T> implements RandomAccess
|
|
|
return array;
|
|
|
}
|
|
|
|
|
|
+ private void checkCapacity(int index) {
|
|
|
+ if(index >= capacity) {
|
|
|
+ throw new IndexOutOfBoundsException(index + " out of maximal capacity " + capacity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkBounds(int index) {
|
|
|
+ if(index >= size || index<0) {
|
|
|
+ throw new IndexOutOfBoundsException(index +" out of bound " + size);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkType(Class<?> clazz) {
|
|
|
+ if (!clazz.isArray()) {
|
|
|
+ throw new IllegalArgumentException(clazz + " is not an array");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|