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