com.github.javaparser.symbolsolver.cache.GuavaCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javaparser-symbol-solver-core Show documentation
Show all versions of javaparser-symbol-solver-core Show documentation
A Symbol Solver for Java, built on top of JavaParser (core)
The newest version!
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver.cache;
import com.github.javaparser.resolution.cache.Cache;
import com.github.javaparser.resolution.cache.CacheStats;
import java.util.Objects;
import java.util.Optional;
/**
* This class is used to wrap a Guava {@link com.google.common.cache.Cache}.
*
* @param The type of the key.
* @param The type of the value.
*/
public class GuavaCache implements Cache {
/**
* Wrap a Guava cache with a custom cache.
*
* @param guavaCache The guava cache to be wrapped-
*
* @param The expected type for the key.
* @param The expected type for the value.
*
* @return A newly created instance of {@link NoCache}.
*/
public static GuavaCache create(
com.google.common.cache.Cache guavaCache) {
return new GuavaCache<>(guavaCache);
}
private final com.google.common.cache.Cache guavaCache;
public GuavaCache(com.google.common.cache.Cache guavaCache) {
this.guavaCache = Objects.requireNonNull(guavaCache, "The argument GuavaCache can't be null.");
}
@Override
public void put(K key, V value) {
guavaCache.put(key, value);
}
@Override
public Optional get(K key) {
return Optional.ofNullable(guavaCache.getIfPresent(key));
}
@Override
public void remove(K key) {
guavaCache.invalidate(key);
}
@Override
public void removeAll() {
guavaCache.invalidateAll();
}
@Override
public boolean contains(K key) {
return get(key).isPresent();
}
@Override
public long size() {
return guavaCache.size();
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public CacheStats stats() {
com.google.common.cache.CacheStats stats = guavaCache.stats();
return new DefaultCacheStats(
stats.hitCount(),
stats.missCount(),
stats.loadSuccessCount(),
stats.loadExceptionCount(),
stats.totalLoadTime(),
stats.evictionCount());
}
}