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    *   http://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.Recycler;
20  import io.netty.util.Recycler.Handle;
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 Recycler<RecyclableArrayList> RECYCLER = new Recycler<RecyclableArrayList>() {
37          @Override
38          protected RecyclableArrayList newObject(Handle handle) {
39              return new RecyclableArrayList(handle);
40          }
41      };
42  
43      private boolean insertSinceRecycled;
44  
45      /**
46       * Create a new empty {@link RecyclableArrayList} instance
47       */
48      public static RecyclableArrayList newInstance() {
49          return newInstance(DEFAULT_INITIAL_CAPACITY);
50      }
51  
52      /**
53       * Create a new empty {@link RecyclableArrayList} instance with the given capacity.
54       */
55      public static RecyclableArrayList newInstance(int minCapacity) {
56          RecyclableArrayList ret = RECYCLER.get();
57          ret.ensureCapacity(minCapacity);
58          return ret;
59      }
60  
61      private final Handle handle;
62  
63      private RecyclableArrayList(Handle handle) {
64          this(handle, DEFAULT_INITIAL_CAPACITY);
65      }
66  
67      private RecyclableArrayList(Handle handle, int initialCapacity) {
68          super(initialCapacity);
69          this.handle = handle;
70      }
71  
72      @Override
73      public boolean addAll(Collection<?> c) {
74          checkNullElements(c);
75          if (super.addAll(c)) {
76              insertSinceRecycled = true;
77              return true;
78          }
79          return false;
80      }
81  
82      @Override
83      public boolean addAll(int index, Collection<?> c) {
84          checkNullElements(c);
85          if (super.addAll(index, c)) {
86              insertSinceRecycled = true;
87              return true;
88          }
89          return false;
90      }
91  
92      private static void checkNullElements(Collection<?> c) {
93          if (c instanceof RandomAccess && c instanceof List) {
94              // produce less garbage
95              List<?> list = (List<?>) c;
96              int size = list.size();
97              for (int i = 0; i  < size; i++) {
98                  if (list.get(i) == null) {
99                      throw new IllegalArgumentException("c contains null values");
100                 }
101             }
102         } else {
103             for (Object element: c) {
104                 if (element == null) {
105                     throw new IllegalArgumentException("c contains null values");
106                 }
107             }
108         }
109     }
110 
111     @Override
112     public boolean add(Object element) {
113         if (element == null) {
114             throw new NullPointerException("element");
115         }
116         if (super.add(element)) {
117             insertSinceRecycled = true;
118             return true;
119         }
120         return false;
121     }
122 
123     @Override
124     public void add(int index, Object element) {
125         if (element == null) {
126             throw new NullPointerException("element");
127         }
128         super.add(index, element);
129         insertSinceRecycled = true;
130     }
131 
132     @Override
133     public Object set(int index, Object element) {
134         if (element == null) {
135             throw new NullPointerException("element");
136         }
137         Object old = super.set(index, element);
138         insertSinceRecycled = true;
139         return old;
140     }
141 
142     /**
143      * Returns {@code true} if any elements where added or set. This will be reset once {@link #recycle()} was called.
144      */
145     public boolean insertSinceRecycled() {
146         return insertSinceRecycled;
147     }
148 
149     /**
150      * Clear and recycle this instance.
151      */
152     public boolean recycle() {
153         clear();
154         insertSinceRecycled = false;
155         return RECYCLER.recycle(this, handle);
156     }
157 }