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