com.aspectran.utils.cache.ConcurrentReferenceCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aspectran-utils Show documentation
Show all versions of aspectran-utils Show documentation
Miscellaneous utility classes used across multiple packages
/*
* Copyright (c) 2008-2024 The Aspectran Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aspectran.utils.cache;
import com.aspectran.utils.Assert;
import com.aspectran.utils.ConcurrentReferenceHashMap;
import com.aspectran.utils.ConcurrentReferenceHashMap.ReferenceType;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
/**
* A {@link Cache} that uses {@link ReferenceType#SOFT soft} or
* {@linkplain ReferenceType#WEAK weak} references for both {@code keys} and {@code values}.
*
* @param the type of the key used for caching
* @param the type of the cached values
* @since 6.6.7
*/
public class ConcurrentReferenceCache implements Cache {
private final Map cache;
private final Function generator;
public ConcurrentReferenceCache(Function generator) {
Assert.notNull(generator, "Generator function must not be null");
this.cache = new ConcurrentReferenceHashMap<>(16, ReferenceType.SOFT);
this.generator = generator;
}
public ConcurrentReferenceCache(ReferenceType referenceType, Function generator) {
Assert.notNull(referenceType, "Reference type must not be null");
Assert.notNull(generator, "Generator function must not be null");
this.cache = new ConcurrentReferenceHashMap<>(16, referenceType);
this.generator = generator;
}
@Override
public V get(K key) {
V cached = cache.get(key);
if (cached == null) {
cached = generator.apply(key);
V existing = cache.putIfAbsent(key, cached);
if (existing != null) {
cached = existing;
}
}
return cached;
}
@Override
public void remove(K key) {
cache.remove(key);
}
@Override
public void clear() {
cache.clear();
}
@Override
public Set keySet() {
return cache.keySet();
}
@Override
public int size() {
return cache.size();
}
@Override
public boolean isEmpty() {
return cache.isEmpty();
}
}