• 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/CaffeinatedGuavaLoadingCache.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.HashMap;
22
import java.util.Map;
23
import java.util.Set;
24
import java.util.concurrent.CompletableFuture;
25
import java.util.concurrent.ExecutionException;
26
import java.util.concurrent.Executor;
27

28
import org.jspecify.annotations.Nullable;
29

30
import com.github.benmanes.caffeine.cache.CacheLoader;
31
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
32
import com.google.common.cache.LoadingCache;
33
import com.google.common.collect.ImmutableMap;
34
import com.google.common.util.concurrent.ExecutionError;
35
import com.google.common.util.concurrent.FutureCallback;
36
import com.google.common.util.concurrent.Futures;
37
import com.google.common.util.concurrent.ListenableFuture;
38
import com.google.common.util.concurrent.UncheckedExecutionException;
39

40
/**
41
 * A Caffeine-backed loading cache through a Guava facade.
42
 *
43
 * @author ben.manes@gmail.com (Ben Manes)
44
 */
45
@SuppressWarnings("serial")
46
final class CaffeinatedGuavaLoadingCache<K, V>
47
    extends CaffeinatedGuavaCache<K, V> implements LoadingCache<K, V> {
48
  private static final ThreadLocal<Boolean> nullBulkLoad = ThreadLocal.withInitial(() -> false);
×
49
  private static final long serialVersionUID = 1L;
50

51
  private final com.github.benmanes.caffeine.cache.LoadingCache<K, V> cache;
52

53
  CaffeinatedGuavaLoadingCache(com.github.benmanes.caffeine.cache.LoadingCache<K, V> cache) {
54
    super(cache);
×
55
    this.cache = cache;
×
56
  }
×
57

58
  @Override
59
  @SuppressWarnings("PMD.PreserveStackTrace")
60
  public V get(K key) throws ExecutionException {
61
    requireNonNull(key);
×
62
    try {
63
      return cache.get(key);
×
64
    } catch (InvalidCacheLoadException e) {
×
65
      throw e;
×
66
    } catch (CacheLoaderException e) {
×
67
      throw new ExecutionException(e.getCause());
×
68
    } catch (RuntimeException e) {
×
69
      throw new UncheckedExecutionException(e);
×
70
    } catch (Error e) {
×
71
      throw new ExecutionError(e);
×
72
    }
73
  }
74

75
  @Override
76
  @SuppressWarnings({"CatchingUnchecked", "PMD.PreserveStackTrace"})
77
  public V getUnchecked(K key) {
78
    try {
79
      return cache.get(key);
×
80
    } catch (NullPointerException | InvalidCacheLoadException e) {
×
81
      throw e;
×
82
    } catch (CacheLoaderException e) {
×
83
      throw new UncheckedExecutionException(e.getCause());
×
84
    } catch (Exception e) {
×
85
      throw new UncheckedExecutionException(e);
×
86
    } catch (Error e) {
×
87
      throw new ExecutionError(e);
×
88
    }
89
  }
90

91
  @Override
92
  @SuppressWarnings({"CatchingUnchecked", "PMD.PreserveStackTrace"})
93
  public ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {
94
    try {
95
      Map<K, V> result = cache.getAll(keys);
×
96
      if (nullBulkLoad.get()) {
×
97
        nullBulkLoad.set(false);
×
98
        throw new InvalidCacheLoadException("null key or value");
×
99
      }
100
      for (K key : keys) {
×
101
        if (!result.containsKey(key)) {
×
102
          throw new InvalidCacheLoadException("loadAll failed to return a value for " + key);
×
103
        }
104
      }
×
105
      return ImmutableMap.copyOf(result);
×
106
    } catch (NullPointerException | InvalidCacheLoadException e) {
×
107
      throw e;
×
108
    } catch (CacheLoaderException e) {
×
109
      throw new ExecutionException(e.getCause());
×
110
    } catch (Exception e) {
×
111
      throw new UncheckedExecutionException(e);
×
112
    } catch (Error e) {
×
113
      throw new ExecutionError(e);
×
114
    }
115
  }
116

117
  @Override
118
  @SuppressWarnings("deprecation")
119
  public V apply(K key) {
120
    return cache.get(key);
×
121
  }
122

123
  @Override
124
  @SuppressWarnings("FutureReturnValueIgnored")
125
  public void refresh(K key) {
126
    cache.refresh(key);
×
127
  }
×
128

129
  abstract static class CaffeinatedLoader<K, V> implements CacheLoader<K, V>, Serializable {
130
    private static final long serialVersionUID = 1L;
131

132
    final com.google.common.cache.CacheLoader<K, V> cacheLoader;
133

134
    CaffeinatedLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
×
135
      this.cacheLoader = requireNonNull(cacheLoader);
×
136
    }
×
137
    @Override public CompletableFuture<V> asyncReload(K key, V oldValue, Executor executor) {
138
      var future = new CompletableFuture<V>();
×
139
      try {
140
        ListenableFuture<V> reloader = cacheLoader.reload(key, oldValue);
×
141
        if (reloader == null) {
×
142
          future.completeExceptionally(new InvalidCacheLoadException("null future"));
×
143
        } else {
144
          Futures.addCallback(reloader, new FutureCompleter<>(future), Runnable::run);
×
145
        }
146
      } catch (Throwable t) {
×
147
        future.completeExceptionally(t);
×
148
      }
×
149
      return future;
×
150
    }
151
  }
152

153
  static class InternalSingleLoader<K, V> extends CaffeinatedLoader<K, V> {
154
    private static final long serialVersionUID = 1L;
155

156
    InternalSingleLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
157
      super(cacheLoader);
×
158
    }
×
159
    @Override public V load(K key) {
160
      try {
161
        V value = cacheLoader.load(key);
×
162
        if (value == null) {
×
163
          throw new InvalidCacheLoadException("null value");
×
164
        }
165
        return value;
×
166
      } catch (RuntimeException | Error e) {
×
167
        throw e;
×
168
      } catch (InterruptedException e) {
×
169
        Thread.currentThread().interrupt();
×
170
        throw new CacheLoaderException(e);
×
171
      } catch (Exception e) {
×
172
        throw new CacheLoaderException(e);
×
173
      }
174
    }
175
  }
176

177
  static final class InternalBulkLoader<K, V> extends InternalSingleLoader<K, V> {
178
    private static final long serialVersionUID = 1L;
179

180
    InternalBulkLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
181
      super(cacheLoader);
×
182
    }
×
183
    @Override public Map<K, V> loadAll(Set<? extends K> keys) {
184
      try {
185
        Map<K, V> loaded = cacheLoader.loadAll(keys);
×
186
        if (loaded == null) {
×
187
          throw new InvalidCacheLoadException("null map");
×
188
        }
189
        var result = new HashMap<K, V>(loaded.size(), /* loadFactor= */ 1.0f);
×
190
        loaded.forEach((key, value) -> {
×
191
          if ((key == null) || (value == null)) {
×
192
            nullBulkLoad.set(true);
×
193
          } else {
194
            result.put(key, value);
×
195
          }
196
        });
×
197
        return result;
×
198
      } catch (RuntimeException | Error e) {
×
199
        throw e;
×
200
      } catch (InterruptedException e) {
×
201
        Thread.currentThread().interrupt();
×
202
        throw new CacheLoaderException(e);
×
203
      } catch (Exception e) {
×
204
        throw new CacheLoaderException(e);
×
205
      }
206
    }
207
  }
208

209
  static class ExternalSingleLoader<K, V> extends CaffeinatedLoader<K, V> {
210
    private static final long serialVersionUID = 1L;
211

212
    ExternalSingleLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
213
      super(cacheLoader);
×
214
    }
×
215
    @Override public V load(K key) throws Exception {
216
      V value = cacheLoader.load(key);
×
217
      if (value == null) {
×
218
        throw new InvalidCacheLoadException("null value");
×
219
      }
220
      return value;
×
221
    }
222
  }
223

224
  static final class ExternalBulkLoader<K, V> extends ExternalSingleLoader<K, V> {
225
    private static final long serialVersionUID = 1L;
226

227
    ExternalBulkLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
228
      super(cacheLoader);
×
229
    }
×
230
    @Override public Map<K, V> loadAll(Set<? extends K> keys) throws Exception {
231
      return cacheLoader.loadAll(keys);
×
232
    }
233
  }
234

235
  static final class FutureCompleter<V> implements FutureCallback<V> {
236
    final CompletableFuture<V> future;
237

238
    FutureCompleter(CompletableFuture<V> future) {
×
239
      this.future = future;
×
240
    }
×
241
    @Override public void onSuccess(@Nullable V value) {
242
      if (value == null) {
×
243
        future.completeExceptionally(new InvalidCacheLoadException("null value"));
×
244
      } else {
245
        future.complete(value);
×
246
      }
247
    }
×
248
    @Override public void onFailure(Throwable t) {
249
      future.completeExceptionally(t);
×
250
    }
×
251
  }
252
}
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