查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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           * @param handle can NOT be null.
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<T>(ObjectUtil.checkNotNull(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<T>() {
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  }