HBASE-24163 MOB compactor implementations should use format specifiers when calling...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / util / SoftObjectPool.java
blob4cce1f8e6b0d1b1ca45e60e8c7783c4f15337eba
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.util;
20 import java.lang.ref.Reference;
21 import java.lang.ref.SoftReference;
23 import org.apache.yetus.audience.InterfaceAudience;
25 /**
26 * A {@code SoftReference} based shared object pool.
27 * The objects are kept in soft references and
28 * associated with keys which are identified by the {@code equals} method.
29 * The objects are created by ObjectFactory on demand.
30 * The object creation is expected to be lightweight,
31 * and the objects may be excessively created and discarded.
32 * Thread safe.
34 @InterfaceAudience.Private
35 public class SoftObjectPool<K, V> extends ObjectPool<K, V> {
37 public SoftObjectPool(ObjectFactory<K, V> objectFactory) {
38 super(objectFactory);
41 public SoftObjectPool(ObjectFactory<K, V> objectFactory, int initialCapacity) {
42 super(objectFactory, initialCapacity);
45 public SoftObjectPool(ObjectFactory<K, V> objectFactory, int initialCapacity,
46 int concurrencyLevel) {
47 super(objectFactory, initialCapacity, concurrencyLevel);
50 @Override
51 public Reference<V> createReference(K key, V obj) {
52 return new SoftObjectReference(key, obj);
55 private class SoftObjectReference extends SoftReference<V> {
56 final K key;
58 SoftObjectReference(K key, V obj) {
59 super(obj, staleRefQueue);
60 this.key = key;
64 @Override
65 public K getReferenceKey(Reference<V> ref) {
66 return ((SoftObjectReference) ref).key;