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

ben-manes / caffeine / #5157

03 Dec 2025 06:30AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5157

push

github

ben-manes
add loading type to parameterized test dimensions to reduce task size

0 of 3834 branches covered (0.0%)

0 of 7848 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<K> implements InternalReference<K> {
185
    private final int hashCode;
186

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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