• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #5201

02 Jan 2026 05:27AM UTC coverage: 99.911% (-0.09%) from 100.0%
#5201

push

github

ben-manes
build clean up

3832 of 3838 branches covered (99.84%)

7863 of 7870 relevant lines covered (99.91%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.41
/guava/src/main/java/com/github/benmanes/caffeine/guava/CaffeinatedGuavaCache.java
1
/*
2
 * Copyright 2015 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.guava;
17

18
import static java.util.Objects.requireNonNull;
19

20
import java.io.Serializable;
21
import java.util.Collection;
22
import java.util.Map;
23
import java.util.Map.Entry;
24
import java.util.Set;
25
import java.util.concurrent.Callable;
26
import java.util.concurrent.ConcurrentMap;
27
import java.util.concurrent.ExecutionException;
28
import java.util.function.BiFunction;
29
import java.util.function.Function;
30
import java.util.function.Predicate;
31

32
import org.jspecify.annotations.Nullable;
33

34
import com.google.common.cache.Cache;
35
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
36
import com.google.common.cache.CacheStats;
37
import com.google.common.collect.ForwardingCollection;
38
import com.google.common.collect.ForwardingConcurrentMap;
39
import com.google.common.collect.ForwardingSet;
40
import com.google.common.collect.ImmutableMap;
41
import com.google.common.util.concurrent.ExecutionError;
42
import com.google.common.util.concurrent.UncheckedExecutionException;
43

44
/**
45
 * A Caffeine-backed cache through a Guava facade.
46
 *
47
 * @author ben.manes@gmail.com (Ben Manes)
48
 */
49
@SuppressWarnings("serial")
50
class CaffeinatedGuavaCache<K, V> implements Cache<K, V>, Serializable {
51
  private static final long serialVersionUID = 1L;
52

53
  private final com.github.benmanes.caffeine.cache.Cache<K, V> cache;
54
  private transient @Nullable ConcurrentMap<K, V> mapView;
55

56
  CaffeinatedGuavaCache(com.github.benmanes.caffeine.cache.Cache<K, V> cache) {
1✔
57
    this.cache = requireNonNull(cache);
1✔
58
  }
1✔
59

60
  @Override
61
  public @Nullable V getIfPresent(Object key) {
62
    @SuppressWarnings("unchecked")
63
    var castedKey = (K) key;
1✔
64
    return cache.getIfPresent(castedKey);
1✔
65
  }
66

67
  @Override
68
  @SuppressWarnings({"PMD.ExceptionAsFlowControl", "PMD.PreserveStackTrace"})
69
  public V get(K key, Callable<? extends @Nullable V> valueLoader) throws ExecutionException {
70
    requireNonNull(valueLoader);
1✔
71
    try {
72
      return cache.get(key, k -> {
1✔
73
        try {
74
          V value = valueLoader.call();
1✔
75
          if (value == null) {
1✔
76
            throw new InvalidCacheLoadException("null value");
1✔
77
          }
78
          return value;
1✔
79
        } catch (InvalidCacheLoadException e) {
1✔
80
          throw e;
1✔
81
        } catch (RuntimeException e) {
1✔
82
          throw new UncheckedExecutionException(e);
1✔
83
        } catch (InterruptedException e) {
1✔
84
          Thread.currentThread().interrupt();
1✔
85
          throw new CacheLoaderException(e);
1✔
86
        } catch (Exception e) {
1✔
87
          throw new CacheLoaderException(e);
1✔
88
        } catch (Error e) {
1✔
89
          throw new ExecutionError(e);
1✔
90
        }
91
      });
92
    } catch (CacheLoaderException e) {
1✔
93
      throw new ExecutionException(e.getCause());
1✔
94
    }
95
  }
96

97
  @Override
98
  public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
99
    @SuppressWarnings("unchecked")
100
    var castedKeys = (Iterable<? extends K>) keys;
1✔
101
    return ImmutableMap.copyOf(cache.getAllPresent(castedKeys));
1✔
102
  }
103

104
  @Override
105
  public void put(K key, V value) {
106
    cache.put(key, value);
1✔
107
  }
1✔
108

109
  @Override
110
  public void putAll(Map<? extends K, ? extends V> m) {
111
    cache.putAll(m);
1✔
112
  }
1✔
113

114
  @Override
115
  public void invalidate(Object key) {
116
    @SuppressWarnings("unchecked")
117
    var castedKey = (K) key;
1✔
118
    cache.invalidate(castedKey);
1✔
119
  }
1✔
120

121
  @Override
122
  public void invalidateAll(Iterable<?> keys) {
123
    @SuppressWarnings("unchecked")
124
    var castedKeys = (Iterable<? extends K>) keys;
1✔
125
    cache.invalidateAll(castedKeys);
1✔
126
  }
1✔
127

128
  @Override
129
  public void invalidateAll() {
130
    cache.invalidateAll();
1✔
131
  }
1✔
132

133
  @Override
134
  public long size() {
135
    return cache.estimatedSize();
1✔
136
  }
137

138
  @Override
139
  public CacheStats stats() {
140
    var stats = cache.stats();
1✔
141
    return new CacheStats(stats.hitCount(), stats.missCount(), stats.loadSuccessCount(),
1✔
142
        stats.loadFailureCount(), stats.totalLoadTime(), stats.evictionCount());
1✔
143
  }
144

145
  @Override
146
  public ConcurrentMap<K, V> asMap() {
147
    return (mapView == null) ? (mapView = new AsMapView()) : mapView;
1✔
148
  }
149

150
  @Override
151
  public void cleanUp() {
152
    cache.cleanUp();
1✔
153
  }
1✔
154

155
  final class AsMapView extends ForwardingConcurrentMap<K, V> {
1✔
156
    @Nullable Set<Entry<K, V>> entrySet;
157
    @Nullable Collection<V> values;
158
    @Nullable Set<K> keySet;
159

160
    @Override public boolean containsKey(@Nullable Object key) {
161
      return (key != null) && delegate().containsKey(key);
1✔
162
    }
163
    @Override public boolean containsValue(@Nullable Object value) {
164
      return (value != null) && delegate().containsValue(value);
1✔
165
    }
166
    @Override public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
167
      delegate().replaceAll(function);
×
168
    }
×
169
    @Override public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
170
      return delegate().computeIfAbsent(key, mappingFunction);
1✔
171
    }
172
    @Override public @Nullable V computeIfPresent(K key,
173
        BiFunction<? super K, ? super V, ? extends @Nullable V> remappingFunction) {
174
      return delegate().computeIfPresent(key, remappingFunction);
1✔
175
    }
176
    @Override public V compute(K key,
177
        BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
178
      return delegate().compute(key, remappingFunction);
1✔
179
    }
180
    @Override public V merge(K key, V value,
181
        BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
182
      return delegate().merge(key, value, remappingFunction);
×
183
    }
184
    @Override public Set<K> keySet() {
185
      return (keySet == null) ? (keySet = new KeySetView()) : keySet;
1✔
186
    }
187
    @Override public Collection<V> values() {
188
      return (values == null) ? (values = new ValuesView()) : values;
1✔
189
    }
190
    @Override public Set<Entry<K, V>> entrySet() {
191
      return (entrySet == null) ? (entrySet = new EntrySetView()) : entrySet;
1✔
192
    }
193
    @Override protected ConcurrentMap<K, V> delegate() {
194
      return cache.asMap();
1✔
195
    }
196
  }
197

198
  final class KeySetView extends ForwardingSet<K> {
1✔
199
    @Override public boolean removeIf(Predicate<? super K> filter) {
200
      return delegate().removeIf(filter);
×
201
    }
202
    @Override public boolean remove(@Nullable Object o) {
203
      return (o != null) && delegate().remove(o);
1✔
204
    }
205
    @Override protected Set<K> delegate() {
206
      return cache.asMap().keySet();
1✔
207
    }
208
  }
209

210
  final class ValuesView extends ForwardingCollection<V> {
1✔
211
    @Override public boolean removeIf(Predicate<? super V> filter) {
212
      return delegate().removeIf(filter);
×
213
    }
214
    @Override public boolean remove(@Nullable Object o) {
215
      return (o != null) && delegate().remove(o);
1✔
216
    }
217
    @Override protected Collection<V> delegate() {
218
      return cache.asMap().values();
1✔
219
    }
220
  }
221

222
  final class EntrySetView extends ForwardingSet<Entry<K, V>> {
1✔
223
    @Override public boolean add(Entry<K, V> entry) {
224
      throw new UnsupportedOperationException();
1✔
225
    }
226
    @Override public boolean addAll(Collection<? extends Entry<K, V>> entry) {
227
      throw new UnsupportedOperationException();
×
228
    }
229
    @Override public boolean removeIf(Predicate<? super Entry<K, V>> filter) {
230
      return delegate().removeIf(filter);
×
231
    }
232
    @Override protected Set<Entry<K, V>> delegate() {
233
      return cache.asMap().entrySet();
1✔
234
    }
235
  }
236

237
  static final class CacheLoaderException extends RuntimeException {
238
    private static final long serialVersionUID = 1L;
239

240
    CacheLoaderException(Throwable cause) {
241
      super(null, cause, /* enableSuppression= */ false, /* writableStackTrace= */ false);
1✔
242
    }
1✔
243
  }
244
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc