• 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/References.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 java.util.Locale.US;
19
import static java.util.Objects.requireNonNull;
20

21
import java.lang.ref.ReferenceQueue;
22
import java.lang.ref.SoftReference;
23
import java.lang.ref.WeakReference;
24
import java.util.Objects;
25

26
import org.jspecify.annotations.Nullable;
27

28
/**
29
 * Static utility methods and classes pertaining to weak and soft references.
30
 *
31
 * @author ben.manes@gmail.com (Ben Manes)
32
 */
33
@SuppressWarnings({"EqualsWhichDoesntCheckParameterClass",
34
    "PMD.MissingStaticMethodInNonInstantiatableClass"})
35
final class References {
36

37
  private References() {}
38

39
  /** A weak or soft reference that includes the entry's key reference. */
40
  interface InternalReference<E> {
41

42
    /**
43
     * Returns this reference object's referent. If this reference object has been cleared, either
44
     * by the program or by the garbage collector, then this method returns {@code null}.
45
     *
46
     * @return The object to which this reference refers, or {@code null} if this reference object
47
     *         has been cleared
48
     */
49
    @Nullable
50
    E get();
51

52
    /**
53
     * Returns the key that is associated to the cache entry holding this reference. If the cache
54
     * holds keys strongly, this is that key instance. Otherwise, the cache holds keys weakly and
55
     * the {@link WeakKeyReference} is returned.
56
     *
57
     * @return the key that is associated to the cached entry
58
     */
59
    Object getKeyReference();
60

61
    /**
62
     * Returns {@code true} if the arguments is a {@linkplain InternalReference} that holds the
63
     * same element. A weakly or softly held element is compared using identity equality.
64
     *
65
     * @param object the reference object with which to compare
66
     * @return {@code true} if this object is the same as the argument; {@code false} otherwise
67
     */
68
    default boolean referenceEquals(@Nullable Object object) {
69
      if (object == this) {
×
70
        return true;
×
71
      } else if (object instanceof InternalReference<?>) {
×
72
        var referent = (InternalReference<?>) object;
×
73
        return (get() == referent.get());
×
74
      }
75
      return false;
×
76
    }
77

78
    /**
79
     * Returns {@code true} if the arguments is a {@linkplain InternalReference} that holds an
80
     * equivalent element as determined by {@link Object#equals}.
81
     *
82
     * @param object the reference object with which to compare
83
     * @return {@code true} if this object is equivalent by {@link Object#equals} as the argument;
84
     *         {@code false} otherwise
85
     */
86
    default boolean objectEquals(@Nullable Object object) {
87
      if (object == this) {
×
88
        return true;
×
89
      } else if (object instanceof InternalReference<?>) {
×
90
        var referent = (InternalReference<?>) object;
×
91
        return Objects.equals(get(), referent.get());
×
92
      }
93
      return false;
×
94
    }
95
  }
96

97
  /**
98
   * A short-lived adapter used for looking up an entry in the cache where the keys are weakly held.
99
   * This {@linkplain InternalReference} implementation is not suitable for storing in the cache as
100
   * the key is strongly held.
101
   */
102
  static final class LookupKeyReference<K> implements InternalReference<K> {
103
    private final int hashCode;
104
    private final K key;
105

106
    public LookupKeyReference(K key) {
×
107
      this.hashCode = System.identityHashCode(key);
×
108
      this.key = requireNonNull(key);
×
109
    }
×
110

111
    @Override
112
    public K get() {
113
      return key;
×
114
    }
115

116
    @Override
117
    public Object getKeyReference() {
118
      return this;
×
119
    }
120

121
    @Override
122
    public boolean equals(@Nullable Object object) {
123
      return referenceEquals(object);
×
124
    }
125

126
    @Override
127
    public int hashCode() {
128
      return hashCode;
×
129
    }
130

131
    @Override
132
    public String toString() {
133
      return String.format(US,
×
134
          "%s{key=%s, hashCode=%d}", getClass().getSimpleName(), get(), hashCode);
×
135
    }
136
  }
137

138
  /**
139
   * A short-lived adapter used for looking up an entry in the cache where the keys are weakly held.
140
   * This {@linkplain InternalReference} implementation is not suitable for storing in the cache as
141
   * the key is strongly held.
142
   */
143
  static final class LookupKeyEqualsReference<K> implements InternalReference<K> {
144
    private final int hashCode;
145
    private final K key;
146

147
    public LookupKeyEqualsReference(K key) {
×
148
      this.hashCode = key.hashCode();
×
149
      this.key = requireNonNull(key);
×
150
    }
×
151

152
    @Override
153
    public K get() {
154
      return key;
×
155
    }
156

157
    @Override
158
    public Object getKeyReference() {
159
      return this;
×
160
    }
161

162
    @Override
163
    public boolean equals(@Nullable Object object) {
164
      return objectEquals(object);
×
165
    }
166

167
    @Override
168
    public int hashCode() {
169
      return hashCode;
×
170
    }
171

172
    @Override
173
    public String toString() {
174
      return String.format(US,
×
175
          "%s{key=%s, hashCode=%d}", getClass().getSimpleName(), get(), hashCode);
×
176
    }
177
  }
178

179
  /**
180
   * The key in a cache that holds keys weakly. This class retains the key's identity hash code in
181
   * the advent that the key is reclaimed so that the entry can be removed from the cache in
182
   * constant time.
183
   */
184
  static class WeakKeyReference<K> extends WeakReference<@Nullable K>
185
      implements InternalReference<K> {
186
    private final int hashCode;
187

188
    public WeakKeyReference(@Nullable K key, @Nullable ReferenceQueue<K> queue) {
189
      super(key, queue);
×
190
      hashCode = System.identityHashCode(key);
×
191
    }
×
192

193
    @Override
194
    public final Object getKeyReference() {
195
      return this;
×
196
    }
197

198
    @Override
199
    public final boolean equals(@Nullable Object object) {
200
      return referenceEquals(object);
×
201
    }
202

203
    @Override
204
    public final int hashCode() {
205
      return hashCode;
×
206
    }
207

208
    @Override
209
    public final String toString() {
210
      return String.format(US,
×
211
          "%s{key=%s, hashCode=%d}", getClass().getSimpleName(), get(), hashCode);
×
212
    }
213
  }
214

215
  /**
216
   * The key in a cache that holds the key weakly and uses equals equivalence. This class retains
217
   * the key's hash code in the advent that the key is reclaimed so that the entry can be removed
218
   * from the cache in constant time.
219
   */
220
  static final class WeakKeyEqualsReference<K>
221
      extends WeakReference<K> implements InternalReference<K> {
222
    private final int hashCode;
223

224
    public WeakKeyEqualsReference(K key, @Nullable ReferenceQueue<K> queue) {
225
      super(key, queue);
×
226
      hashCode = key.hashCode();
×
227
    }
×
228

229
    @Override
230
    public Object getKeyReference() {
231
      return this;
×
232
    }
233

234
    @Override
235
    public boolean equals(@Nullable Object object) {
236
      return objectEquals(object);
×
237
    }
238

239
    @Override
240
    public int hashCode() {
241
      return hashCode;
×
242
    }
243

244
    @Override
245
    public String toString() {
246
      return String.format(US,
×
247
          "%s{key=%s, hashCode=%d}", getClass().getSimpleName(), get(), hashCode);
×
248
    }
249
  }
250

251
  /**
252
   * The value in a cache that holds values weakly. This class retains a reference to the key in
253
   * the advent that the value is reclaimed so that the entry can be removed from the cache in
254
   * constant time.
255
   */
256
  static final class WeakValueReference<V> extends WeakReference<@Nullable V>
257
      implements InternalReference<V> {
258
    private Object keyReference;
259

260
    public WeakValueReference(Object keyReference,
261
        @Nullable V value, @Nullable ReferenceQueue<V> queue) {
262
      super(value, queue);
×
263
      this.keyReference = keyReference;
×
264
    }
×
265

266
    @Override
267
    public Object getKeyReference() {
268
      return keyReference;
×
269
    }
270

271
    public void setKeyReference(Object keyReference) {
272
      this.keyReference = keyReference;
×
273
    }
×
274

275
    @Override
276
    public boolean equals(@Nullable Object object) {
277
      return referenceEquals(object);
×
278
    }
279

280
    @Override
281
    public int hashCode() {
282
      V value = get();
×
283
      return (value == null) ? 0 : value.hashCode();
×
284
    }
285

286
    @Override
287
    public String toString() {
288
      return String.format(US, "%s{value=%s}", getClass().getSimpleName(), get());
×
289
    }
290
  }
291

292
  /**
293
   * The value in a cache that holds values softly. This class retains a reference to the key in
294
   * the advent that the value is reclaimed so that the entry can be removed from the cache in
295
   * constant time.
296
   */
297
  static final class SoftValueReference<V> extends SoftReference<@Nullable V>
298
      implements InternalReference<V> {
299
    private Object keyReference;
300

301
    public SoftValueReference(Object keyReference,
302
        @Nullable V value, @Nullable ReferenceQueue<V> queue) {
303
      super(value, queue);
×
304
      this.keyReference = keyReference;
×
305
    }
×
306

307
    @Override
308
    public Object getKeyReference() {
309
      return keyReference;
×
310
    }
311

312
    public void setKeyReference(Object keyReference) {
313
      this.keyReference = keyReference;
×
314
    }
×
315

316
    @Override
317
    public boolean equals(@Nullable Object object) {
318
      return referenceEquals(object);
×
319
    }
320

321
    @Override
322
    public int hashCode() {
323
      V value = get();
×
324
      return (value == null) ? 0 : value.hashCode();
×
325
    }
326

327
    @Override
328
    public String toString() {
329
      return String.format(US, "%s{value=%s}", getClass().getSimpleName(), get());
×
330
    }
331
  }
332
}
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