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

ben-manes / caffeine / #5651

18 Jul 2026 03:26AM UTC coverage: 66.303% (-33.7%) from 100.0%
#5651

push

github

ben-manes
dependency updates

2809 of 4164 branches covered (67.46%)

5584 of 8422 relevant lines covered (66.3%)

0.66 hits per line

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

0.0
/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.Objects;
25
import java.util.Set;
26
import java.util.Spliterator;
27
import java.util.concurrent.Callable;
28
import java.util.concurrent.ConcurrentMap;
29
import java.util.concurrent.ExecutionException;
30
import java.util.function.BiConsumer;
31
import java.util.function.BiFunction;
32
import java.util.function.Consumer;
33
import java.util.function.Function;
34
import java.util.function.Predicate;
35

36
import org.jspecify.annotations.Nullable;
37

38
import com.google.common.cache.Cache;
39
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
40
import com.google.common.cache.CacheStats;
41
import com.google.common.collect.ForwardingCollection;
42
import com.google.common.collect.ForwardingConcurrentMap;
43
import com.google.common.collect.ForwardingSet;
44
import com.google.common.collect.ImmutableMap;
45
import com.google.common.collect.Iterables;
46
import com.google.common.util.concurrent.ExecutionError;
47
import com.google.common.util.concurrent.UncheckedExecutionException;
48

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

58
  private final com.github.benmanes.caffeine.cache.Cache<K, V> cache;
59
  private transient @Nullable ConcurrentMap<K, V> mapView;
60

61
  CaffeinatedGuavaCache(com.github.benmanes.caffeine.cache.Cache<K, V> cache) {
×
62
    this.cache = requireNonNull(cache);
×
63
  }
×
64

65
  @Override
66
  public @Nullable V getIfPresent(Object key) {
67
    @SuppressWarnings("unchecked")
68
    var castedKey = (K) key;
×
69
    return cache.getIfPresent(castedKey);
×
70
  }
71

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

102
  @Override
103
  public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
104
    @SuppressWarnings("unchecked")
105
    var castedKeys = (Iterable<? extends K>) keys;
×
106
    return ImmutableMap.copyOf(cache.getAllPresent(Iterables.filter(castedKeys, Objects::nonNull)));
×
107
  }
108

109
  @Override
110
  public void put(K key, V value) {
111
    cache.put(key, value);
×
112
  }
×
113

114
  @Override
115
  public void putAll(Map<? extends K, ? extends V> m) {
116
    cache.putAll(m);
×
117
  }
×
118

119
  @Override
120
  public void invalidate(Object key) {
121
    @SuppressWarnings("unchecked")
122
    var castedKey = (K) key;
×
123
    cache.invalidate(castedKey);
×
124
  }
×
125

126
  @Override
127
  public void invalidateAll(Iterable<?> keys) {
128
    @SuppressWarnings("unchecked")
129
    var castedKeys = (Iterable<? extends K>) keys;
×
130
    cache.invalidateAll(Iterables.filter(castedKeys, Objects::nonNull));
×
131
  }
×
132

133
  @Override
134
  public void invalidateAll() {
135
    cache.invalidateAll();
×
136
  }
×
137

138
  @Override
139
  public long size() {
140
    return cache.estimatedSize();
×
141
  }
142

143
  @Override
144
  public CacheStats stats() {
145
    var stats = cache.stats();
×
146
    return new CacheStats(stats.hitCount(), stats.missCount(), stats.loadSuccessCount(),
×
147
        stats.loadFailureCount(), stats.totalLoadTime(), stats.evictionCount());
×
148
  }
149

150
  @Override
151
  public ConcurrentMap<K, V> asMap() {
152
    return (mapView == null) ? (mapView = new AsMapView()) : mapView;
×
153
  }
154

155
  @Override
156
  public void cleanUp() {
157
    cache.cleanUp();
×
158
  }
×
159

160
  final class AsMapView extends ForwardingConcurrentMap<K, V> {
×
161
    @Nullable Set<Entry<K, V>> entrySet;
162
    @Nullable Collection<V> values;
163
    @Nullable Set<K> keySet;
164

165
    @Override public boolean containsKey(@Nullable Object key) {
166
      return (key != null) && delegate().containsKey(key);
×
167
    }
168
    @Override public boolean containsValue(@Nullable Object value) {
169
      return (value != null) && delegate().containsValue(value);
×
170
    }
171
    @Override public @Nullable V get(@Nullable Object key) {
172
      return (key == null) ? null : delegate().get(key);
×
173
    }
174
    @Override public @Nullable V remove(@Nullable Object key) {
175
      return (key == null) ? null : delegate().remove(key);
×
176
    }
177
    @Override public boolean remove(@Nullable Object key, @Nullable Object value) {
178
      return (key != null) && (value != null) && delegate().remove(key, value);
×
179
    }
180
    @Override public boolean replace(K key, @Nullable V oldValue, V newValue) {
181
      requireNonNull(key);
×
182
      requireNonNull(newValue);
×
183
      return (oldValue != null) && delegate().replace(key, oldValue, newValue);
×
184
    }
185
    @Override public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
186
      delegate().replaceAll(function);
×
187
    }
×
188
    @Override public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
189
      return delegate().computeIfAbsent(key, mappingFunction);
×
190
    }
191
    @Override public @Nullable V computeIfPresent(K key,
192
        BiFunction<? super K, ? super V, ? extends @Nullable V> remappingFunction) {
193
      return delegate().computeIfPresent(key, remappingFunction);
×
194
    }
195
    @Override public V compute(K key,
196
        BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
197
      return delegate().compute(key, remappingFunction);
×
198
    }
199
    @Override public V merge(K key, V value,
200
        BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
201
      return delegate().merge(key, value, remappingFunction);
×
202
    }
203
    @Override public void forEach(BiConsumer<? super K, ? super V> action) {
204
      delegate().forEach(action);
×
205
    }
×
206
    @Override public Set<K> keySet() {
207
      return (keySet == null) ? (keySet = new KeySetView()) : keySet;
×
208
    }
209
    @Override public Collection<V> values() {
210
      return (values == null) ? (values = new ValuesView()) : values;
×
211
    }
212
    @Override public Set<Entry<K, V>> entrySet() {
213
      return (entrySet == null) ? (entrySet = new EntrySetView()) : entrySet;
×
214
    }
215
    @Override protected ConcurrentMap<K, V> delegate() {
216
      return cache.asMap();
×
217
    }
218
  }
219

220
  final class KeySetView extends ForwardingSet<K> {
×
221
    @Override public boolean contains(@Nullable Object o) {
222
      return (o != null) && delegate().contains(o);
×
223
    }
224
    @Override public boolean remove(@Nullable Object o) {
225
      return (o != null) && delegate().remove(o);
×
226
    }
227
    @Override public boolean removeIf(Predicate<? super K> filter) {
228
      return delegate().removeIf(filter);
×
229
    }
230
    @Override public void forEach(Consumer<? super K> action) {
231
      delegate().forEach(action);
×
232
    }
×
233
    @Override public Spliterator<K> spliterator() {
234
      return delegate().spliterator();
×
235
    }
236
    @Override protected Set<K> delegate() {
237
      return cache.asMap().keySet();
×
238
    }
239
  }
240

241
  final class ValuesView extends ForwardingCollection<V> {
×
242
    @Override public boolean contains(@Nullable Object o) {
243
      return (o != null) && delegate().contains(o);
×
244
    }
245
    @Override public boolean remove(@Nullable Object o) {
246
      return (o != null) && delegate().remove(o);
×
247
    }
248
    @Override public boolean removeIf(Predicate<? super V> filter) {
249
      return delegate().removeIf(filter);
×
250
    }
251
    @Override public void forEach(Consumer<? super V> action) {
252
      delegate().forEach(action);
×
253
    }
×
254
    @Override public Spliterator<V> spliterator() {
255
      return delegate().spliterator();
×
256
    }
257
    @Override protected Collection<V> delegate() {
258
      return cache.asMap().values();
×
259
    }
260
  }
261

262
  @SuppressWarnings("NullableProblems")
263
  final class EntrySetView extends ForwardingSet<Entry<K, V>> {
×
264
    @Override public boolean add(Entry<K, V> entry) {
265
      throw new UnsupportedOperationException();
×
266
    }
267
    @Override public boolean addAll(Collection<? extends Entry<K, V>> entries) {
268
      if (entries.isEmpty()) {
×
269
        return false;
×
270
      }
271
      throw new UnsupportedOperationException();
×
272
    }
273
    @Override public boolean removeIf(Predicate<? super Entry<K, V>> filter) {
274
      return delegate().removeIf(filter);
×
275
    }
276
    @Override public void forEach(Consumer<? super Entry<K, V>> action) {
277
      delegate().forEach(action);
×
278
    }
×
279
    @Override public Spliterator<Entry<K, V>> spliterator() {
280
      return delegate().spliterator();
×
281
    }
282
    @Override protected Set<Entry<K, V>> delegate() {
283
      return cache.asMap().entrySet();
×
284
    }
285
  }
286

287
  static final class CacheLoaderException extends RuntimeException {
288
    private static final long serialVersionUID = 1L;
289

290
    CacheLoaderException(Throwable cause) {
291
      super(null, cause, /* enableSuppression= */ false, /* writableStackTrace= */ false);
×
292
    }
×
293
  }
294
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc