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       * @deprecated For removal. Please use {@link Recycler#get()} instead.
34       */
35      @Deprecated
36      public abstract T get();
37  
38      /**
39       * Handle for an pooled {@link Object} that will be used to notify the {@link ObjectPool} once it can
40       * reuse the pooled {@link Object} again.
41       * @param <T>
42       */
43      public interface Handle<T> {
44          /**
45           * Recycle the {@link Object} if possible and so make it ready to be reused.
46           */
47          void recycle(T self);
48      }
49  
50      /**
51       * Creates a new Object which references the given {@link Handle} and calls {@link Handle#recycle(Object)} once
52       * it can be re-used.
53       *
54       * @param <T> the type of the pooled object
55       *
56       * @deprecated For removal. Please use {@link Recycler()} instead.
57       */
58      @Deprecated
59      public interface ObjectCreator<T> {
60  
61          /**
62           * Creates an returns a new {@link Object} that can be used and later recycled via
63           * {@link Handle#recycle(Object)}.
64           *
65           * @param handle can NOT be null.
66           */
67          T newObject(Handle<T> handle);
68      }
69  
70      /**
71       * Creates a new {@link ObjectPool} which will use the given {@link ObjectCreator} to create the {@link Object}
72       * that should be pooled.
73       *
74       * @deprecated For removal. Please use {@link Recycler()} instead.
75       */
76      @Deprecated
77      public static <T> ObjectPool<T> newPool(final ObjectCreator<T> creator) {
78          return new RecyclerObjectPool<T>(ObjectUtil.checkNotNull(creator, "creator"));
79      }
80  
81      @Deprecated
82      private static final class RecyclerObjectPool<T> extends ObjectPool<T> {
83          private final Recycler<T> recycler;
84  
85          RecyclerObjectPool(final ObjectCreator<T> creator) {
86               recycler = new Recycler<T>() {
87                  @Override
88                  protected T newObject(Handle<T> handle) {
89                      return creator.newObject(handle);
90                  }
91              };
92          }
93  
94          @Override
95          public T get() {
96              return recycler.get();
97          }
98      }
99  }