• 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
/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Async.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.cache;
17

18
import static com.github.benmanes.caffeine.cache.BoundedLocalCache.MAXIMUM_EXPIRY;
19
import static java.util.Objects.requireNonNull;
20

21
import java.io.Serializable;
22
import java.lang.System.Logger;
23
import java.lang.System.Logger.Level;
24
import java.util.concurrent.CancellationException;
25
import java.util.concurrent.CompletableFuture;
26
import java.util.concurrent.CompletionException;
27
import java.util.concurrent.Executor;
28

29
import org.jspecify.annotations.Nullable;
30

31
/**
32
 * Static utility methods and classes pertaining to asynchronous operations.
33
 *
34
 * @author ben.manes@gmail.com (Ben Manes)
35
 */
36
@SuppressWarnings("serial")
37
final class Async {
38
  static final long ASYNC_EXPIRY = (Long.MAX_VALUE >> 1) + (Long.MAX_VALUE >> 2); // 220 years
39
  static final Logger logger = System.getLogger(Async.class.getName());
×
40

41
  private Async() {}
42

43
  /** Returns if the future has successfully completed. */
44
  static boolean isReady(@Nullable CompletableFuture<?> future) {
45
    return (future != null) && future.isDone()
×
46
        && !future.isCompletedExceptionally()
×
47
        && (future.join() != null);
×
48
  }
49

50
  /** Returns the current value or null if either not done or failed. */
51
  static <V> @Nullable V getIfReady(@Nullable CompletableFuture<V> future) {
52
    return isReady(future) ? requireNonNull(future).join() : null;
×
53
  }
54

55
  /** Returns the value when completed successfully or null if failed. */
56
  static <V> @Nullable V getWhenSuccessful(@Nullable CompletableFuture<V> future) {
57
    try {
58
      return (future == null) ? null : future.join();
×
59
    } catch (CancellationException | CompletionException e) {
×
60
      return null;
×
61
    }
62
  }
63

64
  /**
65
   * A removal listener that asynchronously forwards the value stored in a {@link CompletableFuture}
66
   * if successful to the user-supplied removal listener.
67
   */
68
  static final class AsyncRemovalListener<K, V>
69
      implements RemovalListener<K, CompletableFuture<@Nullable V>>, Serializable {
70
    private static final long serialVersionUID = 1L;
71

72
    final RemovalListener<K, V> delegate;
73
    final Executor executor;
74

75
    AsyncRemovalListener(RemovalListener<K, V> delegate, Executor executor) {
×
76
      this.delegate = requireNonNull(delegate);
×
77
      this.executor = requireNonNull(executor);
×
78
    }
×
79

80
    @Override
81
    @SuppressWarnings("FutureReturnValueIgnored")
82
    public void onRemoval(@Nullable K key,
83
        @Nullable CompletableFuture<@Nullable V> future, RemovalCause cause) {
84
      if (future != null) {
×
85
        future.thenAcceptAsync(value -> {
×
86
          if (value != null) {
×
87
            try {
88
              delegate.onRemoval(key, value, cause);
×
89
            } catch (Throwable t) {
×
90
              logger.log(Level.WARNING, "Exception thrown by removal listener", t);
×
91
            }
×
92
          }
93
        }, executor);
×
94
      }
95
    }
×
96

97
    Object writeReplace() {
98
      return delegate;
×
99
    }
100
  }
101

102
  /**
103
   * An eviction listener that forwards the value stored in a {@link CompletableFuture} to the
104
   * user-supplied eviction listener.
105
   */
106
  static final class AsyncEvictionListener<K, V>
107
      implements RemovalListener<K, CompletableFuture<V>>, Serializable {
108
    private static final long serialVersionUID = 1L;
109

110
    final RemovalListener<K, V> delegate;
111

112
    AsyncEvictionListener(RemovalListener<K, V> delegate) {
×
113
      this.delegate = requireNonNull(delegate);
×
114
    }
×
115

116
    @Override
117
    public void onRemoval(@Nullable K key,
118
        @Nullable CompletableFuture<V> future, RemovalCause cause) {
119
      // Must have been completed and be non-null to be eligible for eviction
120
      V value = Async.getIfReady(future);
×
121
      if (value != null) {
×
122
        delegate.onRemoval(key, value, cause);
×
123
      }
124
    }
×
125

126
    Object writeReplace() {
127
      return delegate;
×
128
    }
129
  }
130

131
  /**
132
   * A weigher for asynchronous computations. When the value is being loaded this weigher returns
133
   * {@code 0} to indicate that the entry should not be evicted due to a size constraint. If the
134
   * value is computed successfully then the entry must be reinserted so that the weight is updated
135
   * and the expiration timeouts reflect the value once present. This can be done safely using
136
   * {@link java.util.Map#replace(Object, Object, Object)}.
137
   */
138
  static final class AsyncWeigher<K, V> implements Weigher<K, CompletableFuture<V>>, Serializable {
139
    private static final long serialVersionUID = 1L;
140

141
    final Weigher<K, V> delegate;
142

143
    AsyncWeigher(Weigher<K, V> delegate) {
×
144
      this.delegate = requireNonNull(delegate);
×
145
    }
×
146

147
    @Override
148
    public int weigh(K key, CompletableFuture<V> future) {
149
      return isReady(future) ? delegate.weigh(key, future.join()) : 0;
×
150
    }
151

152
    Object writeReplace() {
153
      return delegate;
×
154
    }
155
  }
156

157
  /**
158
   * An expiry for asynchronous computations. When the value is being loaded this expiry returns
159
   * {@code ASYNC_EXPIRY} to indicate that the entry should not be evicted due to an expiry
160
   * constraint. If the value is computed successfully then the entry must be reinserted so that the
161
   * expiration is updated and the expiration timeouts reflect the value once present. The
162
   * duration's maximum range is reserved to coordinate with the asynchronous life cycle.
163
   */
164
  static final class AsyncExpiry<K, V> implements Expiry<K, CompletableFuture<V>>, Serializable {
165
    private static final long serialVersionUID = 1L;
166

167
    final Expiry<? super K, ? super V> delegate;
168

169
    AsyncExpiry(Expiry<? super K, ? super V> delegate) {
×
170
      this.delegate = requireNonNull(delegate);
×
171
    }
×
172

173
    @Override
174
    public long expireAfterCreate(K key, CompletableFuture<V> future, long currentTime) {
175
      if (isReady(future)) {
×
176
        long duration = delegate.expireAfterCreate(key, future.join(), currentTime);
×
177
        return Math.min(duration, MAXIMUM_EXPIRY);
×
178
      }
179
      return ASYNC_EXPIRY;
×
180
    }
181

182
    @Override
183
    public long expireAfterUpdate(K key, CompletableFuture<V> future,
184
        long currentTime, long currentDuration) {
185
      if (isReady(future)) {
×
186
        long duration = (currentDuration > MAXIMUM_EXPIRY)
×
187
            ? delegate.expireAfterCreate(key, future.join(), currentTime)
×
188
            : delegate.expireAfterUpdate(key, future.join(), currentTime, currentDuration);
×
189
        return Math.min(duration, MAXIMUM_EXPIRY);
×
190
      }
191
      return ASYNC_EXPIRY;
×
192
    }
193

194
    @Override
195
    public long expireAfterRead(K key, CompletableFuture<V> future,
196
        long currentTime, long currentDuration) {
197
      if (isReady(future)) {
×
198
        long duration = delegate.expireAfterRead(key, future.join(), currentTime, currentDuration);
×
199
        return Math.min(duration, MAXIMUM_EXPIRY);
×
200
      }
201
      return ASYNC_EXPIRY;
×
202
    }
203

204
    Object writeReplace() {
205
      return delegate;
×
206
    }
207
  }
208
}
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