Repository navigation
Expand file tree
/
Copy pathSequenceRegistry.java
More file actions
70 lines (59 loc) · 2.37 KB
/
Copy pathSequenceRegistry.java
File metadata and controls
70 lines (59 loc) · 2.37 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
package com.abilityapi.sequenceapi;
import com.abilityapi.sequenceapi.origin.Origin;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
public abstract class SequenceRegistry<T> implements Iterable<SequenceBlueprint<T>> {
private final BiMap<Class<?>, SequenceBlueprint<T>> registry = HashBiMap.create();
/**
* Puts the {@link SequenceBlueprint} into this registry.
*
* <p>Default implementation takes into account the {@link Origin#OWNER}
* with the {@link Origin#ROOT} set as the blueprint if it does not exist
* already.</p>
*
* @param origin the origin
* @param sequenceBlueprint the sequence blueprint
* @return the updated origin, or the original if it did not get added
*/
public Origin put(final Origin origin, final SequenceBlueprint<T> sequenceBlueprint) {
final Origin updatedOrigin = Origin.from(origin).root(sequenceBlueprint).build();
if (this.registry.containsKey(sequenceBlueprint.getClass())) return updatedOrigin;
this.registry.put(sequenceBlueprint.getClass(), sequenceBlueprint);
return updatedOrigin;
}
/**
* Returns the {@link SequenceBlueprint} from this registry using
* the corresponding key.
*
* @param key the key
* @return the sequence blueprint
*/
public Optional<SequenceBlueprint<T>> get(final Class<?> key) {
if (!this.registry.containsKey(key)) return Optional.empty();
return Optional.ofNullable(this.registry.get(key));
}
/**
* Returns the {@link Class} key that represents its {@link SequenceBlueprint}.
*
* @param sequenceBlueprint the blueprint
* @return the sequence key, or {@code null} if they check is not contained in this registry
*/
public Class<?> key(final SequenceBlueprint<T> sequenceBlueprint) {
return this.registry.inverse().get(sequenceBlueprint);
}
/**
* Returns the key that represents its {@link SequenceBlueprint}.
*
* @return the sequence key, or {@code null} if they check is not contained in this registry
*/
public Set<Class<?>> keySet() {
return this.registry.keySet();
}
@Override
public Iterator<SequenceBlueprint<T>> iterator() {
return this.registry.values().iterator();
}
}