View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * A simple list which is recyclable. This implementation does not allow {@code null} elements to be added.
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       * Create a new empty {@link RecyclableArrayList} instance
43       */
44      public static RecyclableArrayList newInstance() {
45          return newInstance(DEFAULT_INITIAL_CAPACITY);
46      }
47  
48      /**
49       * Create a new empty {@link RecyclableArrayList} instance with the given capacity.
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              // produce less garbage
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      * Returns {@code true} if any elements where added or set. This will be reset once {@link #recycle()} was called.
134      */
135     public boolean insertSinceRecycled() {
136         return insertSinceRecycled;
137     }
138 
139     /**
140      * Clear and recycle this instance.
141      */
142     public boolean recycle() {
143         clear();
144         insertSinceRecycled = false;
145         handle.recycle(this);
146         return true;
147     }
148 }