• 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/Weigher.java
1
/*
2
 * Copyright 2014 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.Caffeine.requireArgument;
19
import static java.util.Objects.requireNonNull;
20

21
import java.io.Serializable;
22

23
import org.jspecify.annotations.NullMarked;
24

25
/**
26
 * Calculates the weights of cache entries. The total weight threshold is used to determine when an
27
 * eviction is required.
28
 *
29
 * @param <K> the type of keys
30
 * @param <V> the type of values
31
 * @author ben.manes@gmail.com (Ben Manes)
32
 */
33
@NullMarked
34
@FunctionalInterface
35
public interface Weigher<K, V> {
36

37
  /**
38
   * Returns the weight of a cache entry. There is no unit for entry weights; rather they are simply
39
   * relative to each other.
40
   *
41
   * @param key the key to weigh
42
   * @param value the value to weigh
43
   * @return the weight of the entry; must be non-negative
44
   */
45
  int weigh(K key, V value);
46

47
  /**
48
   * Returns a weigher where an entry has a weight of {@code 1}.
49
   *
50
   * @param <K> the type of keys
51
   * @param <V> the type of values
52
   * @return a weigher where an entry has a weight of {@code 1}
53
   */
54
  static <K, V> Weigher<K, V> singletonWeigher() {
55
    @SuppressWarnings("unchecked")
56
    var instance = (Weigher<K, V>) SingletonWeigher.INSTANCE;
×
57
    return instance;
×
58
  }
59

60
  /**
61
   * Returns a weigher that enforces that the weight is non-negative.
62
   *
63
   * @param delegate the weigher to weighs the entry
64
   * @param <K> the type of keys
65
   * @param <V> the type of values
66
   * @return a weigher that enforces that the weight is non-negative
67
   */
68
  static <K, V> Weigher<K, V> boundedWeigher(Weigher<K, V> delegate) {
69
    return new BoundedWeigher<>(delegate);
×
70
  }
71
}
72

73
enum SingletonWeigher implements Weigher<Object, Object> {
×
74
  INSTANCE;
×
75

76
  @Override public int weigh(Object key, Object value) {
77
    return 1;
×
78
  }
79
}
80

81
final class BoundedWeigher<K, V> implements Weigher<K, V>, Serializable {
82
  private static final long serialVersionUID = 1;
83

84
  @SuppressWarnings("serial")
85
  final Weigher<? super K, ? super V> delegate;
86

87
  BoundedWeigher(Weigher<? super K, ? super V> delegate) {
×
88
    this.delegate = requireNonNull(delegate);
×
89
  }
×
90

91
  @Override
92
  public int weigh(K key, V value) {
93
    int weight = delegate.weigh(key, value);
×
94
    requireArgument(weight >= 0);
×
95
    return weight;
×
96
  }
97

98
  Object writeReplace() {
99
    return delegate;
×
100
  }
101
}
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