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.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   * A simple list which is recyclable. This implementation does not allow {@code null} elements to be added.
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       * Create a new empty {@link RecyclableArrayList} instance
48       */
49      public static RecyclableArrayList newInstance() {
50          return newInstance(DEFAULT_INITIAL_CAPACITY);
51      }
52  
53      /**
54       * Create a new empty {@link RecyclableArrayList} instance with the given capacity.
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              // produce less garbage
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      * Returns {@code true} if any elements where added or set. This will be reset once {@link #recycle()} was called.
136      */
137     public boolean insertSinceRecycled() {
138         return insertSinceRecycled;
139     }
140 
141     /**
142      * Clear and recycle this instance.
143      */
144     public boolean recycle() {
145         clear();
146         insertSinceRecycled = false;
147         handle.recycle(this);
148         return true;
149     }
150 }