View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.newsclub.net.unix.pool;
19  
20  import java.util.Objects;
21  import java.util.Queue;
22  import java.util.concurrent.ConcurrentLinkedQueue;
23  import java.util.concurrent.atomic.AtomicInteger;
24  
25  import org.eclipse.jdt.annotation.NonNull;
26  import org.eclipse.jdt.annotation.Nullable;
27  
28  /**
29   * An {@link ObjectPool} implemented using {@link ConcurrentLinkedQueue}.
30   *
31   * @param <O> The object type.
32   * @author Christian Kohlschütter
33   */
34  public final class ConcurrentQueueObjectPool<O> implements ObjectPool<O> {
35    private final AtomicInteger count = new AtomicInteger(0);
36    private final Queue<O> queue = new ConcurrentLinkedQueue<>();
37    private final ObjectSupplier<O> supplier;
38    private final int maxCapacity;
39    private final ObjectSanitizer<O> sanitizer;
40  
41    /**
42     * Constructs a {@link ConcurrentQueueObjectPool} with the given capacity, supplier and sanitizer.
43     *
44     * @param supplier The supplier.
45     * @param sanitizer The sanitizer.
46     * @param maxCapacity The maximum capacity.
47     */
48    public ConcurrentQueueObjectPool(ObjectSupplier<@NonNull O> supplier,
49        ObjectSanitizer<@NonNull O> sanitizer, final int maxCapacity) {
50      if (maxCapacity < 0) {
51        throw new IllegalArgumentException("maxCapacity");
52      }
53      this.supplier = Objects.requireNonNull(supplier);
54      this.sanitizer = Objects.requireNonNull(sanitizer);
55      this.maxCapacity = maxCapacity;
56    }
57  
58    @Override
59    public Lease<O> take() {
60      O obj = queue.poll();
61      if (obj == null) {
62        obj = Objects.requireNonNull(supplier.get());
63      } else {
64        count.decrementAndGet();
65      }
66      return new LeaseImpl(obj);
67    }
68  
69    private final class LeaseImpl implements Lease<O> {
70      private @Nullable O obj;
71  
72      public LeaseImpl(O obj) {
73        this.obj = obj;
74      }
75  
76      @SuppressWarnings("null")
77      @Override
78      public O get() {
79        return obj;
80      }
81  
82      @Override
83      public synchronized void close() {
84        @Nullable
85        O theObject = obj;
86        obj = null;
87        if (theObject != null) {
88          if (count.get() >= maxCapacity) {
89            // enough objects
90          } else if (!sanitizer.sanitize(theObject)) {
91            // decided not to reuse
92          } else {
93            if (queue.offer(theObject)) {
94              count.incrementAndGet();
95            }
96          }
97        }
98      }
99  
100     @Override
101     public void discard() {
102       obj = null;
103     }
104   }
105 }