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

ben-manes / caffeine / #5173

29 Dec 2025 05:27AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5173

push

github

ben-manes
speed up development ci build

0 of 3838 branches covered (0.0%)

0 of 7869 relevant lines covered (0.0%)

0.0 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.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) {
×
57
    this.cache = requireNonNull(cache);
×
58
  }
×
59

60
  @Override
61
  public @Nullable V getIfPresent(Object key) {
62
    @SuppressWarnings("unchecked")
63
    var castedKey = (K) key;
×
64
    return cache.getIfPresent(castedKey);
×
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);
×
71
    try {
72
      return cache.get(key, k -> {
×
73
        try {
74
          V value = valueLoader.call();
×
75
          if (value == null) {
×
76
            throw new InvalidCacheLoadException("null value");
×
77
          }
78
          return value;
×
79
        } catch (InvalidCacheLoadException e) {
×
80
          throw e;
×
81
        } catch (RuntimeException e) {
×
82
          throw new UncheckedExecutionException(e);
×
83
        } catch (InterruptedException e) {
×
84
          Thread.currentThread().interrupt();
×
85
          throw new CacheLoaderException(e);
×
86
        } catch (Exception e) {
×
87
          throw new CacheLoaderException(e);
×
88
        } catch (Error e) {
×
89
          throw new ExecutionError(e);
×
90
        }
91
      });
92
    } catch (CacheLoaderException e) {
×
93
      throw new ExecutionException(e.getCause());
×
94
    }
95
  }
96

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

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

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

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

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

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

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

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

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

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

155
  final class AsMapView extends ForwardingConcurrentMap<K, V> {
×
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);
×
162
    }
163
    @Override public boolean containsValue(@Nullable Object value) {
164
      return (value != null) && delegate().containsValue(value);
×
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);
×
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);
×
175
    }
176
    @Override public V compute(K key,
177
        BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
178
      return delegate().compute(key, remappingFunction);
×
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;
×
186
    }
187
    @Override public Collection<V> values() {
188
      return (values == null) ? (values = new ValuesView()) : values;
×
189
    }
190
    @Override public Set<Entry<K, V>> entrySet() {
191
      return (entrySet == null) ? (entrySet = new EntrySetView()) : entrySet;
×
192
    }
193
    @Override protected ConcurrentMap<K, V> delegate() {
194
      return cache.asMap();
×
195
    }
196
  }
197

198
  final class KeySetView extends ForwardingSet<K> {
×
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);
×
204
    }
205
    @Override protected Set<K> delegate() {
206
      return cache.asMap().keySet();
×
207
    }
208
  }
209

210
  final class ValuesView extends ForwardingCollection<V> {
×
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);
×
216
    }
217
    @Override protected Collection<V> delegate() {
218
      return cache.asMap().values();
×
219
    }
220
  }
221

222
  final class EntrySetView extends ForwardingSet<Entry<K, V>> {
×
223
    @Override public boolean add(Entry<K, V> entry) {
224
      throw new UnsupportedOperationException();
×
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();
×
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);
×
242
    }
×
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