-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContext.java
More file actions
62 lines (45 loc) · 1.62 KB
/
Context.java
File metadata and controls
62 lines (45 loc) · 1.62 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
package com.github.nylle.javafixture;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Context {
private final Configuration configuration;
private final Map<SpecimenType<?>, Object> cache;
public Context(Configuration configuration) {
if (configuration == null) {
throw new IllegalArgumentException("configuration: null");
}
this.configuration = configuration;
this.cache = new ConcurrentHashMap<>();
}
public Context(Configuration configuration, Map<SpecimenType<?>, Object> predefinedInstances) {
if (configuration == null) {
throw new IllegalArgumentException("configuration: null");
}
this.configuration = configuration;
this.cache = new HashMap<>(predefinedInstances);
}
public Configuration getConfiguration() {
return configuration;
}
public boolean isCached(SpecimenType<?> type) {
return cache.containsKey(type);
}
public <T> T overwrite(SpecimenType<?> type, T instance) {
cache.put(type, instance);
return (T) cache.get(type);
}
public <T> T cached(SpecimenType<?> type, T instance) {
cache.putIfAbsent(type, instance);
return (T) cache.get(type);
}
public <T> T cached(SpecimenType<T> type) {
return (T) cache.get(type);
}
public <T> T preDefined(SpecimenType<T> type, T instance) {
return cache.containsKey(type) ? (T) cache.get(type) : instance;
}
public <T> T remove(SpecimenType<T> type) {
return (T) cache.remove(type);
}
}