• 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/SnapshotEntry.java
1
/*
2
 * Copyright 2022 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.Objects.requireNonNull;
19

20
import java.util.Map;
21

22
import org.jspecify.annotations.Nullable;
23

24
import com.github.benmanes.caffeine.cache.Policy.CacheEntry;
25
import com.google.errorprone.annotations.Immutable;
26

27
/**
28
 * An immutable entry that includes a snapshot of the policy metadata at its time of creation.
29
 *
30
 * @author ben.manes@gmail.com (Ben Manes)
31
 */
32
@Immutable(containerOf = {"K", "V"})
33
class SnapshotEntry<K, V> implements CacheEntry<K, V> {
34
  private final long snapshot;
35
  private final V value;
36
  private final K key;
37

38
  SnapshotEntry(K key, V value, long snapshot) {
×
39
    this.snapshot = snapshot;
×
40
    this.key = requireNonNull(key);
×
41
    this.value = requireNonNull(value);
×
42
  }
×
43
  @Override public final K getKey() {
44
    return key;
×
45
  }
46
  @Override public final V getValue() {
47
    return value;
×
48
  }
49
  @Override public V setValue(V value) {
50
    throw new UnsupportedOperationException();
×
51
  }
52
  @Override public int weight() {
53
    return 1;
×
54
  }
55
  @Override public long expiresAt() {
56
    return snapshot + Long.MAX_VALUE;
×
57
  }
58
  @Override public long refreshableAt() {
59
    return snapshot + Long.MAX_VALUE;
×
60
  }
61
  @Override public final long snapshotAt() {
62
    return snapshot;
×
63
  }
64
  @Override public final boolean equals(@Nullable Object o) {
65
    if (o == this) {
×
66
      return true;
×
67
    } else if (!(o instanceof Map.Entry)) {
×
68
      return false;
×
69
    }
70
    var entry = (Map.Entry<?, ?>) o;
×
71
    return key.equals(entry.getKey()) && value.equals(entry.getValue());
×
72
  }
73
  @Override public final int hashCode() {
74
    return key.hashCode() ^ value.hashCode();
×
75
  }
76
  @Override public final String toString() {
77
    return key + "=" + value;
×
78
  }
79

80
  /** Returns a cache entry containing the given key, value, and snapshot. */
81
  public static <K, V> SnapshotEntry<K, V> forEntry(K key, V value) {
82
    return new SnapshotEntry<>(key, value, /* snapshot= */ 0);
×
83
  }
84

85
  /** Returns a cache entry with the specified metadata. */
86
  @SuppressWarnings("StatementSwitchToExpressionSwitch")
87
  public static <K, V> SnapshotEntry<K, V> forEntry(K key, V value,
88
      long snapshot, int weight, long expiresAt, long refreshableAt) {
89
    long unsetTicks = snapshot + Long.MAX_VALUE;
×
90
    boolean refresh = (refreshableAt != unsetTicks);
×
91
    boolean expires = (expiresAt != unsetTicks);
×
92
    boolean weights = (weight != 1);
×
93
    int features = // truth table
94
          (weights ? 0b001 : 0b000)
×
95
        | (expires ? 0b010 : 0b000)
×
96
        | (refresh ? 0b100 : 0b000);
×
97
    switch (features) { // optimized for common cases
×
98
      case 0b000: return new SnapshotEntry<>(key, value, snapshot);
×
99
      case 0b001: return new WeightedEntry<>(key, value, snapshot, weight);
×
100
      case 0b010: return new ExpirableEntry<>(key, value, snapshot, expiresAt);
×
101
      case 0b011: return new ExpirableWeightedEntry<>(key, value, snapshot, weight, expiresAt);
×
102
      case 0b110: return new RefreshableExpirableEntry<>(
×
103
          key, value, snapshot, expiresAt, refreshableAt);
104
      default: return new CompleteEntry<>(key, value, snapshot, weight, expiresAt, refreshableAt);
×
105
    }
106
  }
107

108
  static class WeightedEntry<K, V> extends SnapshotEntry<K, V> {
109
    final int weight;
110

111
    WeightedEntry(K key, V value, long snapshot, int weight) {
112
      super(key, value, snapshot);
×
113
      this.weight = weight;
×
114
    }
×
115
    @Override public final int weight() {
116
      return weight;
×
117
    }
118
  }
119

120
  static class ExpirableEntry<K, V> extends SnapshotEntry<K, V> {
121
    final long expiresAt;
122

123
    ExpirableEntry(K key, V value, long snapshot, long expiresAt) {
124
      super(key, value, snapshot);
×
125
      this.expiresAt = expiresAt;
×
126
    }
×
127
    @Override public final long expiresAt() {
128
      return expiresAt;
×
129
    }
130
  }
131

132
  static class ExpirableWeightedEntry<K, V> extends WeightedEntry<K, V> {
133
    final long expiresAt;
134

135
    ExpirableWeightedEntry(K key, V value, long snapshot, int weight, long expiresAt) {
136
      super(key, value, snapshot, weight);
×
137
      this.expiresAt = expiresAt;
×
138
    }
×
139
    @Override public final long expiresAt() {
140
      return expiresAt;
×
141
    }
142
  }
143

144
  static class RefreshableExpirableEntry<K, V> extends ExpirableEntry<K, V> {
145
    final long refreshableAt;
146

147
    RefreshableExpirableEntry(K key, V value, long snapshot, long expiresAt, long refreshableAt) {
148
      super(key, value, snapshot, expiresAt);
×
149
      this.refreshableAt = refreshableAt;
×
150
    }
×
151
    @Override public final long refreshableAt() {
152
      return refreshableAt;
×
153
    }
154
  }
155

156
  static final class CompleteEntry<K, V> extends ExpirableWeightedEntry<K, V> {
157
    final long refreshableAt;
158

159
    CompleteEntry(K key, V value, long snapshot, int weight, long expiresAt, long refreshableAt) {
160
      super(key, value, snapshot, weight, expiresAt);
×
161
      this.refreshableAt = refreshableAt;
×
162
    }
×
163
    @Override public long refreshableAt() {
164
      return refreshableAt;
×
165
    }
166
  }
167
}
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