View Javadoc
1   /*
2    * Copyright 2019 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  package io.netty.util.internal;
17  
18  import io.netty.util.Recycler;
19  
20  /**
21   * Light-weight object pool.
22   *
23   * @param <T> the type of the pooled object
24   */
25  public abstract class ObjectPool<T> {
26  
27      ObjectPool() { }
28  
29      /**
30       * Get a {@link Object} from the {@link ObjectPool}. The returned {@link Object} may be created via
31       * {@link ObjectCreator#newObject(Handle)} if no pooled {@link Object} is ready to be reused.
32       */
33      public abstract T get();
34  
35      /**
36       * Handle for an pooled {@link Object} that will be used to notify the {@link ObjectPool} once it can
37       * reuse the pooled {@link Object} again.
38       * @param <T>
39       */
40      public interface Handle<T> {
41          /**
42           * Recycle the {@link Object} if possible and so make it ready to be reused.
43           */
44          void recycle(T self);
45      }
46  
47      /**
48       * Creates a new Object which references the given {@link Handle} and calls {@link Handle#recycle(Object)} once
49       * it can be re-used.
50       *
51       * @param <T> the type of the pooled object
52       */
53      public interface ObjectCreator<T> {
54  
55          /**
56           * Creates an returns a new {@link Object} that can be used and later recycled via
57           * {@link Handle#recycle(Object)}.
58           */
59          T newObject(Handle<T> handle);
60      }
61  
62      /**
63       * Creates a new {@link ObjectPool} which will use the given {@link ObjectCreator} to create the {@link Object}
64       * that should be pooled.
65       */
66      public static <T> ObjectPool<T> newPool(final ObjectCreator<T> creator) {
67          return new RecyclerObjectPool<T>(ObjectUtil.checkNotNull(creator, "creator"));
68      }
69  
70      private static final class RecyclerObjectPool<T> extends ObjectPool<T> {
71          private final Recycler<T> recycler;
72  
73          RecyclerObjectPool(final ObjectCreator<T> creator) {
74               recycler = new Recycler<T>() {
75                  @Override
76                  protected T newObject(Handle<T> handle) {
77                      return creator.newObject(handle);
78                  }
79              };
80          }
81  
82          @Override
83          public T get() {
84              return recycler.get();
85          }
86      }
87  }