Just tiny optimization to avoid object creation

This commit is contained in:
Norman Maurer 2013-07-12 08:23:01 +02:00
parent c38db2afe3
commit 98c6a5810a

View File

@ -21,6 +21,8 @@ import io.netty.util.Recycler.Handle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.RandomAccess;
/** /**
* A simple list that holds the output of a codec. * A simple list that holds the output of a codec.
@ -67,22 +69,33 @@ public final class RecyclableArrayList extends ArrayList<Object> {
@Override @Override
public boolean addAll(Collection<?> c) { public boolean addAll(Collection<?> c) {
for (Object element: c) { checkNullElements(c);
if (element == null) {
throw new IllegalArgumentException("c contains null values");
}
}
return super.addAll(c); return super.addAll(c);
} }
@Override @Override
public boolean addAll(int index, Collection<?> c) { public boolean addAll(int index, Collection<?> c) {
checkNullElements(c);
return super.addAll(index, c);
}
private void checkNullElements(Collection<?> c) {
if (c instanceof RandomAccess && c instanceof List) {
// produce less garbage
List<?> list = (List<?>) c;
int size = list.size();
for (int i = 0; i < size; i++) {
if (list.get(i) == null) {
throw new IllegalArgumentException("c contains null values");
}
}
} else {
for (Object element: c) { for (Object element: c) {
if (element == null) { if (element == null) {
throw new IllegalArgumentException("c contains null values"); throw new IllegalArgumentException("c contains null values");
} }
} }
return super.addAll(index, c); }
} }
@Override @Override