1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.util.internal;
18
19 import io.netty.util.internal.ObjectPool.Handle;
20 import io.netty.util.internal.ObjectPool.ObjectCreator;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.RandomAccess;
26
27
28
29
30 public final class RecyclableArrayList extends ArrayList<Object> {
31
32 private static final long serialVersionUID = -8605125654176467947L;
33
34 private static final int DEFAULT_INITIAL_CAPACITY = 8;
35
36 private static final ObjectPool<RecyclableArrayList> RECYCLER = ObjectPool.newPool(
37 new ObjectCreator<RecyclableArrayList>() {
38 @Override
39 public RecyclableArrayList newObject(Handle<RecyclableArrayList> handle) {
40 return new RecyclableArrayList(handle);
41 }
42 });
43
44 private boolean insertSinceRecycled;
45
46
47
48
49 public static RecyclableArrayList newInstance() {
50 return newInstance(DEFAULT_INITIAL_CAPACITY);
51 }
52
53
54
55
56 public static RecyclableArrayList newInstance(int minCapacity) {
57 RecyclableArrayList ret = RECYCLER.get();
58 ret.ensureCapacity(minCapacity);
59 return ret;
60 }
61
62 private final Handle<RecyclableArrayList> handle;
63
64 private RecyclableArrayList(Handle<RecyclableArrayList> handle) {
65 this(handle, DEFAULT_INITIAL_CAPACITY);
66 }
67
68 private RecyclableArrayList(Handle<RecyclableArrayList> handle, int initialCapacity) {
69 super(initialCapacity);
70 this.handle = handle;
71 }
72
73 @Override
74 public boolean addAll(Collection<?> c) {
75 checkNullElements(c);
76 if (super.addAll(c)) {
77 insertSinceRecycled = true;
78 return true;
79 }
80 return false;
81 }
82
83 @Override
84 public boolean addAll(int index, Collection<?> c) {
85 checkNullElements(c);
86 if (super.addAll(index, c)) {
87 insertSinceRecycled = true;
88 return true;
89 }
90 return false;
91 }
92
93 private static void checkNullElements(Collection<?> c) {
94 if (c instanceof RandomAccess && c instanceof List) {
95
96 List<?> list = (List<?>) c;
97 int size = list.size();
98 for (int i = 0; i < size; i++) {
99 if (list.get(i) == null) {
100 throw new IllegalArgumentException("c contains null values");
101 }
102 }
103 } else {
104 for (Object element: c) {
105 if (element == null) {
106 throw new IllegalArgumentException("c contains null values");
107 }
108 }
109 }
110 }
111
112 @Override
113 public boolean add(Object element) {
114 if (super.add(ObjectUtil.checkNotNull(element, "element"))) {
115 insertSinceRecycled = true;
116 return true;
117 }
118 return false;
119 }
120
121 @Override
122 public void add(int index, Object element) {
123 super.add(index, ObjectUtil.checkNotNull(element, "element"));
124 insertSinceRecycled = true;
125 }
126
127 @Override
128 public Object set(int index, Object element) {
129 Object old = super.set(index, ObjectUtil.checkNotNull(element, "element"));
130 insertSinceRecycled = true;
131 return old;
132 }
133
134
135
136
137 public boolean insertSinceRecycled() {
138 return insertSinceRecycled;
139 }
140
141
142
143
144 public boolean recycle() {
145 clear();
146 insertSinceRecycled = false;
147 handle.recycle(this);
148 return true;
149 }
150 }