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

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
today-context/src/main/java/infra/cache/support/AbstractValueAdaptingCache.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.cache.support;
19

20
import org.jspecify.annotations.Nullable;
21

22
import infra.cache.Cache;
23
import infra.lang.NullValue;
24

25
/**
26
 * Common base class for {@link Cache} implementations that need to adapt
27
 * {@code null} values (and potentially other such special values) before
28
 * passing them on to the underlying store.
29
 *
30
 * <p>Transparently replaces given {@code null} user values with an internal
31
 * {@link NullValue#INSTANCE}, if configured to support {@code null} values
32
 * (as indicated by {@link #isAllowNullValues()}.
33
 *
34
 * @author Juergen Hoeller
35
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
36
 * @since 4.0 2022/3/9 20:45
37
 */
38
public abstract class AbstractValueAdaptingCache implements Cache {
39

40
  protected final boolean allowNullValues;
41

42
  /**
43
   * Create an {@code AbstractValueAdaptingCache} with the given setting.
44
   *
45
   * @param allowNullValues whether to allow for {@code null} values
46
   */
47
  protected AbstractValueAdaptingCache(boolean allowNullValues) {
2✔
48
    this.allowNullValues = allowNullValues;
3✔
49
  }
1✔
50

51
  /**
52
   * Return whether {@code null} values are allowed in this cache.
53
   */
54
  public final boolean isAllowNullValues() {
55
    return this.allowNullValues;
×
56
  }
57

58
  @Override
59
  @Nullable
60
  public ValueWrapper get(Object key) {
61
    return toValueWrapper(lookup(key));
6✔
62
  }
63

64
  @Override
65
  @SuppressWarnings("unchecked")
66
  @Nullable
67
  public <T> T get(Object key, @Nullable Class<T> type) {
68
    Object value = fromStoreValue(lookup(key));
6✔
69
    if (value != null && type != null && !type.isInstance(value)) {
8✔
70
      throw new IllegalStateException(
8✔
71
              "Cached value is not of required type [%s]: %s".formatted(type.getName(), value));
9✔
72
    }
73
    return (T) value;
2✔
74
  }
75

76
  /**
77
   * Perform an actual lookup in the underlying store.
78
   *
79
   * @param key the key whose associated value is to be returned
80
   * @return the raw store value for the key, or {@code null} if none
81
   */
82
  @Nullable
83
  protected abstract Object lookup(Object key);
84

85
  /**
86
   * Convert the given value from the internal store to a user value
87
   * returned from the get method (adapting {@code null}).
88
   *
89
   * @param storeValue the store value
90
   * @return the value to return to the user
91
   */
92
  @Nullable
93
  protected Object fromStoreValue(@Nullable Object storeValue) {
94
    if (this.allowNullValues && storeValue == NullValue.INSTANCE) {
6✔
95
      return null;
2✔
96
    }
97
    return storeValue;
2✔
98
  }
99

100
  /**
101
   * Convert the given user value, as passed into the put method,
102
   * to a value in the internal store (adapting {@code null}).
103
   *
104
   * @param userValue the given user value
105
   * @return the value to store
106
   */
107
  protected Object toStoreValue(@Nullable Object userValue) {
108
    if (userValue == null) {
2✔
109
      if (this.allowNullValues) {
3✔
110
        return NullValue.INSTANCE;
2✔
111
      }
112
      throw new IllegalArgumentException(
8✔
113
              "Cache '%s' is configured to not allow null values but null was provided".formatted(getName()));
5✔
114
    }
115
    return userValue;
2✔
116
  }
117

118
  /**
119
   * Wrap the given store value with a {@link SimpleValueWrapper}, also going
120
   * through {@link #fromStoreValue} conversion. Useful for {@link #get(Object)}
121
   * and {@link #putIfAbsent(Object, Object)} implementations.
122
   *
123
   * @param storeValue the original value
124
   * @return the wrapped value
125
   */
126
  @Nullable
127
  protected ValueWrapper toValueWrapper(@Nullable Object storeValue) {
128
    return storeValue != null ? new SimpleValueWrapper(fromStoreValue(storeValue)) : null;
11✔
129
  }
130

131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc