-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathDefaultCacheMap.java
More file actions
104 lines (90 loc) · 2.42 KB
/
DefaultCacheMap.java
File metadata and controls
104 lines (90 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2016 The original author or authors
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package org.dataloader.impl;
import org.dataloader.CacheMap;
import org.dataloader.annotations.Internal;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
/**
* Default implementation of {@link CacheMap} that is based on a regular {@link java.util.HashMap}.
*
* @param <K> type parameter indicating the type of the cache keys
* @param <V> type parameter indicating the type of the data that is cached
*
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
*/
@Internal
@NullMarked
public class DefaultCacheMap<K, V> implements CacheMap<K, V> {
private final ConcurrentHashMap<K, CompletableFuture<V>> cache;
/**
* Default constructor
*/
public DefaultCacheMap() {
cache = new ConcurrentHashMap<>();
}
/**
* {@inheritDoc}
*/
@Override
public boolean containsKey(K key) {
return cache.containsKey(key);
}
/**
* {@inheritDoc}
*/
@Override
public @Nullable CompletableFuture<V> get(K key) {
return cache.get(key);
}
/**
* {@inheritDoc}
*/
@Override
public Collection<CompletableFuture<V>> getAll() {
return cache.values();
}
/**
* {@inheritDoc}
*/
@Override
public @Nullable CompletableFuture<V> putIfAbsentAtomically(K key, CompletableFuture<V> value) {
return cache.putIfAbsent(key, value);
}
/**
* {@inheritDoc}
*/
@Override
public CacheMap<K, V> delete(K key) {
cache.remove(key);
return this;
}
/**
* {@inheritDoc}
*/
@Override
public CacheMap<K, V> clear() {
cache.clear();
return this;
}
@Override
public int size() {
return cache.size();
}
}