• 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/CaffeinatedGuava.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 java.lang.reflect.Method;
19

20
import org.jspecify.annotations.NullMarked;
21

22
import com.github.benmanes.caffeine.cache.Caffeine;
23
import com.github.benmanes.caffeine.guava.CaffeinatedGuavaLoadingCache.ExternalBulkLoader;
24
import com.github.benmanes.caffeine.guava.CaffeinatedGuavaLoadingCache.ExternalSingleLoader;
25
import com.github.benmanes.caffeine.guava.CaffeinatedGuavaLoadingCache.InternalBulkLoader;
26
import com.github.benmanes.caffeine.guava.CaffeinatedGuavaLoadingCache.InternalSingleLoader;
27
import com.google.common.cache.Cache;
28
import com.google.common.cache.CacheLoader;
29
import com.google.common.cache.LoadingCache;
30

31
/**
32
 * Static utility methods pertaining to adapting between Caffeine and Guava cache interfaces.
33
 *
34
 * @author ben.manes@gmail.com (Ben Manes)
35
 */
36
@NullMarked
37
public final class CaffeinatedGuava {
38

39
  private CaffeinatedGuava() {}
40

41
  /**
42
   * Returns a Caffeine cache wrapped in a Guava {@link Cache} facade.
43
   *
44
   * @param builder the configured cache builder
45
   * @param <K> the most general key type to create caches for
46
   * @param <V> the most general value type to create caches for
47
   * @param <K1> the key type of the cache
48
   * @param <V1> the value type of the cache
49
   * @return a cache exposed under the Guava APIs
50
   */
51
  @SuppressWarnings("PMD.TypeParameterNamingConventions")
52
  public static <K, V, K1 extends K, V1 extends V> Cache<K1, V1> build(Caffeine<K, V> builder) {
53
    return new CaffeinatedGuavaCache<>(builder.build());
×
54
  }
55

56
  /**
57
   * Returns a Caffeine cache wrapped in a Guava {@link LoadingCache} facade.
58
   *
59
   * @param builder the configured cache builder
60
   * @param loader the cache loader used to obtain new values
61
   * @param <K> the most general key type to create caches for
62
   * @param <V> the most general value type to create caches for
63
   * @param <K1> the key type of the cache
64
   * @param <V1> the value type of the cache
65
   * @return a cache exposed under the Guava APIs
66
   */
67
  @SuppressWarnings("PMD.TypeParameterNamingConventions")
68
  public static <K, V, K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
69
      Caffeine<K, V> builder, CacheLoader<? super K1, V1> loader) {
70
    return build(builder, hasLoadAll(loader)
×
71
        ? new InternalBulkLoader<>(loader)
×
72
        : new InternalSingleLoader<>(loader));
×
73
  }
74

75
  /**
76
   * Returns a Caffeine cache wrapped in a Guava {@link LoadingCache} facade.
77
   *
78
   * @param builder the configured cache builder
79
   * @param loader the cache loader used to obtain new values
80
   * @param <K> the most general key type to create caches for
81
   * @param <V> the most general value type to create caches for
82
   * @param <K1> the key type of the cache
83
   * @param <V1> the value type of the cache
84
   * @return a cache exposed under the Guava APIs
85
   */
86
  @SuppressWarnings("PMD.TypeParameterNamingConventions")
87
  public static <K, V, K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
88
      Caffeine<K, V> builder,
89
      com.github.benmanes.caffeine.cache.CacheLoader<? super K1, V1> loader) {
90
    return new CaffeinatedGuavaLoadingCache<>(builder.build(loader));
×
91
  }
92

93
  /**
94
   * Returns a Caffeine cache loader that delegates to a Guava cache loader.
95
   *
96
   * @param loader the cache loader used to obtain new values
97
   * @param <K> the type of keys
98
   * @param <V> the type of values
99
   * @return a cache loader exposed under the Caffeine APIs
100
   */
101
  public static <K, V> com.github.benmanes.caffeine.cache.CacheLoader<K, V> caffeinate(
102
      CacheLoader<K, V> loader) {
103
    return hasLoadAll(loader)
×
104
        ? new ExternalBulkLoader<>(loader)
×
105
        : new ExternalSingleLoader<>(loader);
×
106
  }
107

108
  static boolean hasLoadAll(CacheLoader<?, ?> cacheLoader) {
109
    return hasMethod(cacheLoader, "loadAll", Iterable.class);
×
110
  }
111

112
  static boolean hasMethod(CacheLoader<?, ?> cacheLoader, String name, Class<?>... paramTypes) {
113
    try {
114
      Method method = cacheLoader.getClass().getMethod(name, paramTypes);
×
115
      return (method.getDeclaringClass() != CacheLoader.class);
×
116
    } catch (NoSuchMethodException | SecurityException e) {
×
117
      return false;
×
118
    }
119
  }
120
}
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