Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: TheMolkaPL <themolkapl@gmail.com>
- Loading branch information
1 parent
bbaf6ee
commit bb1a679
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package tc.oc.pgm.api.registry; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* A container that collects T by its ID as {@link String}. | ||
* | ||
* @param <T> type of objects to collect | ||
*/ | ||
// TODO should be extended to NamespacedKeys | ||
public interface IRegistry<T> { | ||
/** | ||
* Does this registry contain the given ID? | ||
* | ||
* @param id ID of the object | ||
* @return {@code true} whether this registry contains ID, otherwise {@link false} | ||
*/ | ||
boolean contains(String id); | ||
|
||
/** | ||
* Get T object from the given ID. | ||
* | ||
* @param id ID of the object | ||
* @return Object from this ID, never {@code null} | ||
* @throws NoSuchElementException When this registry does not contain the given ID | ||
*/ | ||
T get(String id) throws NoSuchElementException; | ||
|
||
/** | ||
* Get optional T object from the given ID. | ||
* | ||
* @param id ID of the object | ||
* @return Object from this ID, or {@link Optional#empty()} when this registry does not contain | ||
* the given ID | ||
*/ | ||
Optional<T> getMaybe(String id); | ||
|
||
/** | ||
* Get all keys registered in this registry. | ||
* | ||
* @return {@link Set} of all keys | ||
*/ | ||
Set<String> getKeys(); | ||
|
||
/** | ||
* Get all T objects registered in this registry. | ||
* | ||
* @return {@link Collection} of all objects | ||
*/ | ||
Collection<T> getAll(); | ||
|
||
/** | ||
* Convert this registry into a {@link Set} of entries. | ||
* | ||
* @return {@link Set} of all entries | ||
*/ | ||
Set<Map.Entry<String, T>> entrySet(); | ||
|
||
/** | ||
* Convert this registry into a {@link Map} where keys are string IDs. | ||
* | ||
* @return {@link Map} of all entries | ||
*/ | ||
Map<String, T> asKeyMap(); | ||
|
||
/** | ||
* Register the given T object in this registry. | ||
* | ||
* @param id ID of the object | ||
* @param object Object itself, may not be {@code null} | ||
*/ | ||
void register(String id, T object); | ||
|
||
/** | ||
* Unregister object from this registry. | ||
* | ||
* @param id ID of the object | ||
* @return {@code true} whether this registry contained this ID, otherwise {@code false} | ||
*/ | ||
boolean unregister(String id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package tc.oc.pgm.api.registry; | ||
|
||
import static com.google.common.base.Preconditions.*; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
public class Registry<T> implements IRegistry<T> { | ||
private final Map<String, T> map; | ||
|
||
public Registry(Map<String, T> map) { | ||
this.map = checkNotNull(map); | ||
} | ||
|
||
@Override | ||
public boolean contains(String id) { | ||
return map.containsKey(checkNotNull(id)); | ||
} | ||
|
||
@Override | ||
public T get(String id) throws NoSuchElementException { | ||
return getMaybe(checkNotNull(id)).orElseThrow(NoSuchElementException::new); | ||
} | ||
|
||
@Override | ||
public Optional<T> getMaybe(String id) { | ||
return Optional.ofNullable(map.get(checkNotNull(id))); | ||
} | ||
|
||
@Override | ||
public Set<String> getKeys() { | ||
return map.keySet(); | ||
} | ||
|
||
@Override | ||
public Collection<T> getAll() { | ||
return map.values(); | ||
} | ||
|
||
@Override | ||
public Set<Map.Entry<String, T>> entrySet() { | ||
return map.entrySet(); | ||
} | ||
|
||
@Override | ||
public Map<String, T> asKeyMap() { | ||
return Collections.unmodifiableMap(map); | ||
} | ||
|
||
@Override | ||
public void register(String id, T object) { | ||
checkNotNull(id); | ||
checkNotNull(object); | ||
map.put(id, object); | ||
} | ||
|
||
@Override | ||
public boolean unregister(String id) { | ||
return map.remove(checkNotNull(id)) != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters