-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegistry.java
More file actions
123 lines (109 loc) · 4.79 KB
/
Registry.java
File metadata and controls
123 lines (109 loc) · 4.79 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.terminalvelocitycabbage.engine.registry;
import com.terminalvelocitycabbage.engine.debug.Log;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Set;
public class Registry<T> {
protected final LinkedHashMap<Identifier, T> registryContents; //The contents of this registry
protected final T defaultItem; //The default item if an attempted retrieval finds no results
/**
* Creates a new registry with type T.
* @param defaultItem The item to be returned by this registry if none other is found
*/
public Registry(T defaultItem) {
this.registryContents = new LinkedHashMap<>();
this.defaultItem = defaultItem;
}
/**
* Creates a new registry with type T.
* The default item of this constructor is null
*/
public Registry() {
this(null);
}
/**
* Registers an item to this registry for retrieval later by its identifier or name
* @param item The item to be registered (if it extends Identifiable)
*/
public RegistryPair<T> register(T item) {
if (item instanceof Identifiable identifiable) {
var identifier = identifiable.getIdentifier();
if (registryContents.containsKey(identifier)) {
Log.warn("Tried to register item of same identifier " + identifier.toString() + " twice, the second addition has been ignored.");
return null;
}
registryContents.put(identifier, item);
return new RegistryPair<>(identifier, item);
} else {
Log.warn("Tried to register item of type " + item.getClass().getName() + " which does not implement Identifiable, the item will not be registered. Try registering with a declared Identifier instead of implicit with this method");
return null;
}
}
/**
* Registers an item to this registry for retrieval later by its identifier or name
* @param identifier The identifier of this registered item
* @param item The item to be registered
*/
public RegistryPair<T> register(Identifier identifier, T item) {
if (registryContents.containsKey(identifier)) {
Log.warn("Tried to register item of same identifier " + identifier.toString() + " twice, the second addition has been ignored.");
return null;
}
registryContents.put(identifier, item);
return new RegistryPair<>(identifier, item);
}
/**
* Replaces the specified identifier with a new value
* @param identifier The identifier of the object you want to replace
* @param newItem The object to replace it with
* @return A registry pair representing the new object and it's registry identifier
*/
public RegistryPair<T> replace(Identifier identifier, T newItem) {
if (!registryContents.containsKey(identifier)) {
Log.warn("Cannot replace registry item with ID: " + identifier.toString() + " since it does not exist in this registry.");
return null;
}
registryContents.replace(identifier, newItem);
return new RegistryPair<>(identifier, newItem);
}
/**
* Retrieves a specific resource by its identifier
* @param identifier The identifier of the specific resource you wish to retrieve
* @return The requested item or the default item if not found
*/
public T get(Identifier identifier) {
return registryContents.get(identifier);
}
/**
* @param identifier The key identifier that is requested
* @return a boolean of true if it exists in this registry or false if not
*/
public boolean contains(Identifier identifier) {
return registryContents.containsKey(identifier);
}
/**
* @param namespace the namespace (portion before the : in an identifier) to search this registry for
* @return All identifiers in this registry with that namespace
*/
public Set<Identifier> getIdentifiersWithNamespace(String namespace) {
Set<Identifier> identifiers = new LinkedHashSet<>();
registryContents.keySet().forEach(identifier -> {
if (identifier.getNamespace().equals(namespace)) identifiers.add(identifier);
});
return identifiers;
}
/**
* @param name the name (portion after the : in an identifier) to search this registry for
* @return All identifiers in this registry with that name
*/
public Set<Identifier> getIdentifiersWithName(String name) {
Set<Identifier> identifiers = new LinkedHashSet<>();
registryContents.keySet().forEach(identifier -> {
if (identifier.getName().equals(name)) identifiers.add(identifier);
});
return identifiers;
}
public LinkedHashMap<Identifier, T> getRegistryContents() {
return registryContents;
}
}