This commit is contained in:
Looly
2022-06-10 21:28:08 +08:00
parent c2c67338e7
commit abea2f5739
2 changed files with 9 additions and 1 deletions

View File

@@ -96,7 +96,7 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
*/ */
public V get(final K key, final Predicate<V> validPredicate, final Func0<V> supplier) { public V get(final K key, final Predicate<V> validPredicate, final Func0<V> supplier) {
V v = get(key); V v = get(key);
if((null != validPredicate && false == validPredicate.test(v))){ if((null != validPredicate && null != v && false == validPredicate.test(v))){
v = null; v = null;
} }
if (null == v && null != supplier) { if (null == v && null != supplier) {

View File

@@ -46,6 +46,14 @@ public class SimpleCacheTest {
Assert.assertEquals("value6", cache.get("key6", ()-> "value6")); Assert.assertEquals("value6", cache.get("key6", ()-> "value6"));
} }
@Test
public void getWithPredicateTest(){
// 检查predicate空指针
final SimpleCache<String, String> cache = new SimpleCache<>();
final String value = cache.get("abc", (v)-> v.equals("1"), () -> "123");
Assert.assertEquals("123", value);
}
@Test @Test
public void getConcurrencyTest(){ public void getConcurrencyTest(){
final SimpleCache<String, String> cache = new SimpleCache<>(); final SimpleCache<String, String> cache = new SimpleCache<>();