-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
CaffeinatedGuava.java
106 lines (95 loc) · 3.73 KB
/
CaffeinatedGuava.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright 2015 Ben Manes. All Rights Reserved.
*
* 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.github.benmanes.caffeine.guava;
import static java.util.Objects.requireNonNull;
import java.lang.reflect.Method;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.guava.CaffeinatedGuavaLoadingCache.BulkLoader;
import com.github.benmanes.caffeine.guava.CaffeinatedGuavaLoadingCache.SingleLoader;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.errorprone.annotations.CheckReturnValue;
/**
* Static utility methods pertaining to adapting between Caffeine and Guava cache interfaces.
*
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class CaffeinatedGuava {
private CaffeinatedGuava() {}
/**
* Returns a Caffeine cache wrapped in a Guava {@link Cache} facade.
*
* @param builder the configured cache builder
* @return a cache exposed under the Guava APIs
*/
@CheckReturnValue
public static <K, V, K1 extends K, V1 extends V> Cache<K1, V1> build(Caffeine<K, V> builder) {
return new CaffeinatedGuavaCache<>(builder.build());
}
/**
* Returns a Caffeine cache wrapped in a Guava {@link LoadingCache} facade.
*
* @param builder the configured cache builder
* @param loader the cache loader used to obtain new values
* @return a cache exposed under the Guava APIs
*/
@CheckReturnValue
public static <K, V, K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
Caffeine<K, V> builder, CacheLoader<? super K1, V1> loader) {
@SuppressWarnings("unchecked")
CacheLoader<K1, V1> castedLoader = (CacheLoader<K1, V1>) loader;
return build(builder, caffeinate(castedLoader));
}
/**
* Returns a Caffeine cache wrapped in a Guava {@link LoadingCache} facade.
*
* @param builder the configured cache builder
* @param loader the cache loader used to obtain new values
* @return a cache exposed under the Guava APIs
*/
@CheckReturnValue
public static <K, V, K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
Caffeine<K, V> builder,
com.github.benmanes.caffeine.cache.CacheLoader<? super K1, V1> loader) {
return new CaffeinatedGuavaLoadingCache<>(builder.build(loader));
}
/**
* Returns a Caffeine cache loader that delegates to a Guava cache loader.
*
* @param loader the cache loader used to obtain new values
* @return a cache loader exposed under the Caffeine APIs
*/
@CheckReturnValue
public static <K, V> com.github.benmanes.caffeine.cache.CacheLoader<K, V> caffeinate(
CacheLoader<K, V> loader) {
requireNonNull(loader);
return hasLoadAll(loader)
? new BulkLoader<>(loader)
: new SingleLoader<>(loader);
}
static boolean hasLoadAll(CacheLoader<?, ?> cacheLoader) {
return hasMethod(cacheLoader, "loadAll", Iterable.class);
}
static boolean hasMethod(CacheLoader<?, ?> cacheLoader, String name, Class<?>... paramTypes) {
try {
Method method = cacheLoader.getClass().getMethod(name, paramTypes);
return (method.getDeclaringClass() != CacheLoader.class);
} catch (NoSuchMethodException | SecurityException e) {
return false;
}
}
}