diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-12-01 20:20:27 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-12-01 20:20:27 -0500 |
| commit | 097a33bc2ecaa64a664550ddd62ccd8de47c51d0 (patch) | |
| tree | df9680c1f33fddf4dcbe538593ee73703afb91ce /src/main/java/bjc/data | |
| parent | c85db1bef75e5c9b7287ab7fdb6e1380d577c674 (diff) | |
An assortment of changes
Diffstat (limited to 'src/main/java/bjc/data')
| -rw-r--r-- | src/main/java/bjc/data/Contexts.java | 23 | ||||
| -rw-r--r-- | src/main/java/bjc/data/IContext.java | 44 |
2 files changed, 66 insertions, 1 deletions
diff --git a/src/main/java/bjc/data/Contexts.java b/src/main/java/bjc/data/Contexts.java index 0769447..75c8480 100644 --- a/src/main/java/bjc/data/Contexts.java +++ b/src/main/java/bjc/data/Contexts.java @@ -2,17 +2,38 @@ package bjc.data; import java.util.*; +/** + * Utility methods for dealing with contexts. + * + * @author Ben Culkin + * + */ public class Contexts { + /** + * The null context, which always throws an exception. + */ public static final IContext NULL = new NullContextImpl(); private Contexts() { throw new UnsupportedOperationException(); } + /** + * Create a new context with no parent. + * + * @return A context with no parent. + */ public static IContext create() { return new ContextImpl(NULL); } + /** + * Create a context with the specified parent. + * + * @param parent The parent of this context. + * + * @return A context with the given context as its parent. + */ public static IContext create(IContext parent) { return new ContextImpl(parent); } @@ -47,7 +68,7 @@ public class Contexts { public ContextImpl(IContext parent) { this.parent = parent; - this.objects = new HashMap<String, Object>(); + this.objects = new HashMap<>(); } @Override diff --git a/src/main/java/bjc/data/IContext.java b/src/main/java/bjc/data/IContext.java index a1073f1..e519501 100644 --- a/src/main/java/bjc/data/IContext.java +++ b/src/main/java/bjc/data/IContext.java @@ -1,14 +1,58 @@ package bjc.data; +/** + * Represents a 'context' which is a hierarchical set of objects. + * @author Ben Culkin + * + */ public interface IContext { + /** + * Register an object with this context. + * + * @param name The name of the object. + * @param o The object to register. + */ void register(String name, Object o); + /** + * Get the parent of this context. + * + * @return The parent of this context. + */ IContext getParent(); + /** + * Get an object from this context. + * + * @param name The name of the object. + * + * @return The object bound to that name. + */ Object get(String name); + /** + * Get an object which is an instance of the provided class or a subclass + * thereof. + * + * @param <T> The type of the object. + * + * @param contract The class of the object. + * + * @return An instance of the provided class. + */ <T> T get(Class<T> contract); + /** + * Get a named object which is an instance of the provided class or a subclass + * thereof. + * + * @param <T> The type of the object. + * + * @param name The name of the object + * @param contract The class of the object. + * + * @return An instance of the provided class, with the given name.. + */ default <T> T get(String name, Class<T> contract) { Object obj = get(name); return obj == null |
