From 5c1163df17c46f7d3e15b6c7949c38843ec56146 Mon Sep 17 00:00:00 2001 From: EVE Date: Tue, 14 Mar 2017 12:06:43 -0400 Subject: Directory work --- .../src/main/java/bjc/utils/esodata/Directory.java | 225 ++++++++++----------- .../java/bjc/utils/esodata/SimpleDirectory.java | 58 ++++++ .../java/bjc/utils/esodata/UnifiedDirectory.java | 68 +++++++ 3 files changed, 234 insertions(+), 117 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/esodata') diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java index 3d02c9c..6fdfe16 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java @@ -1,117 +1,108 @@ -package bjc.utils.esodata; - -import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IMap; - -/** - * Represents a hierarchical map. - * - * What's useful about this is that you can hand sub-directories to people and - * be able to ensure that they can't write outside of it. - * - * @param K - * The key type of the map. - * @param V - * The value type of the map. - */ -public class Directory { - private IMap> children; - - private IMap data; - - /** - * Create a new directory. - */ - public Directory() { - children = new FunctionalMap<>(); - data = new FunctionalMap<>(); - } - - /** - * Create a new sub-directory. - * - * Will fail if a sub-directory of that name already exists. - * - * @param key - * The name of the new sub-directory. - * - * @return The new sub-directory, or null if one by that name already - * exists. - */ - public Directory newSubdirectory(K key) { - if (children.containsKey(key)) - return null; - - Directory kid = new Directory<>(); - children.put(key, kid); - return kid; - } - - /** - * Check if a given sub-directory exists. - * - * @param key - * The key to look for the sub-directory under. - * - * @return Whether or not a sub-directory of that name exists. - */ - public boolean hasSubdirectory(K key) { - return children.containsKey(key); - } - - /** - * Retrieves a given sub-directory. - * - * @param key - * The key to retrieve the sub-directory for. - * - * @return The sub-directory under that name. - * - * @throws IllegalArgumentException - * If the given sub-directory doesn't exist. - */ - public Directory getSubdirectory(K key) { - return children.get(key); - } - - /** - * Insert a data-item into the directory. - * - * @param key - * The key to insert into. - * @param val - * The value to insert. - * - * @return The old value of key, or null if such a value didn't exist. - */ - public V put(K key, V val) { - return data.put(key, val); - } - - /** - * Check if the directory contains a data-item under the given key. - * - * @param key - * The key to check for. - * - * @return Whether or not there is a data item for the given key. - */ - public boolean containsKey(K key) { - return data.containsKey(key); - } - - /** - * Retrive a given data-item from the directory. - * - * @param key - * The key to retrieve data for. - * - * @return The value for the given key. - * - * @throws IllegalArgumentException - * If no value exists for the given key. - */ - public V get(K key) { - return data.get(key); - } -} +package bjc.utils.esodata; + +/** + * Represents a hierarchical map. + * + * What's useful about this is that you can hand sub-directories to people and + * be able to ensure that they can't write outside of it. + * + * @param K + * The key type of the map. + * @param V + * The value type of the map. + */ +public interface Directory { + /** + * Retrieves a given sub-directory. + * + * @param key + * The key to retrieve the sub-directory for. + * + * @return The sub-directory under that name. + * + * @throws IllegalArgumentException + * If the given sub-directory doesn't exist. + */ + Directory getSubdirectory(K key); + + /** + * Check if a given sub-directory exists. + * + * @param key + * The key to look for the sub-directory under. + * + * @return Whether or not a sub-directory of that name exists. + */ + boolean hasSubdirectory(K key); + + /** + * Insert a sub-directory into the dictionary. + * + * @param key + * The name of the new sub-directory + * @param value + * The sub-directory to insert + * + * @return The old sub-directory attached to this key, or null if such a + * sub-directory didn't exist + */ + Directory putSubdirectory(K key, Directory value); + + /** + * Create a new sub-directory. + * + * Will fail if a sub-directory of that name already exists. + * + * @param key + * The name of the new sub-directory. + * + * @return The new sub-directory, or null if one by that name already + * exists. + */ + default Directory newSubdirectory(K key) { + if(hasSubdirectory(key)) { + return null; + } + + Directory dir = new SimpleDirectory<>(); + + putSubdirectory(key, dir); + + return dir; + } + + /** + * Check if the directory contains a data-item under the given key. + * + * @param key + * The key to check for. + * + * @return Whether or not there is a data item for the given key. + */ + boolean containsKey(K key); + + /** + * Retrieve a given data-item from the directory. + * + * @param key + * The key to retrieve data for. + * + * @return The value for the given key. + * + * @throws IllegalArgumentException + * If no value exists for the given key. + */ + V getKey(K key); + + /** + * Insert a data-item into the directory. + * + * @param key + * The key to insert into. + * @param val + * The value to insert. + * + * @return The old value of key, or null if such a value didn't exist. + */ + V putKey(K key, V val); +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java new file mode 100644 index 0000000..22a7e56 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java @@ -0,0 +1,58 @@ +package bjc.utils.esodata; + +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IMap; + +/** + * Simple implementation of {@link Directory}. + * + * Has a split namespace for data and children. + * + * @author EVE + * + * @param The key type of the directory. + * @param The value type of the directory. + */ +public class SimpleDirectory implements Directory { + private IMap> children; + + private IMap data; + + /** + * Create a new directory. + */ + public SimpleDirectory() { + children = new FunctionalMap<>(); + data = new FunctionalMap<>(); + } + + @Override + public Directory getSubdirectory(K key) { + return children.get(key); + } + + @Override + public boolean hasSubdirectory(K key) { + return children.containsKey(key); + } + + @Override + public Directory putSubdirectory(K key, Directory val) { + return children.put(key, val); + } + + @Override + public boolean containsKey(K key) { + return data.containsKey(key); + } + + @Override + public V getKey(K key) { + return data.get(key); + } + + @Override + public V putKey(K key, V val) { + return data.put(key, val); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java new file mode 100644 index 0000000..187474c --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java @@ -0,0 +1,68 @@ +package bjc.utils.esodata; + +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IMap; + +/** + * Simple implementation of {@link Directory}. + * + * Has a unified namespace for data and children. + * + * @author EVE + * + * @param + * The key type of the directory. + * @param + * The value type of the directory. + */ +public class UnifiedDirectory implements Directory { + private IMap> children; + + private IMap data; + + /** + * Create a new directory. + */ + public UnifiedDirectory() { + children = new FunctionalMap<>(); + data = new FunctionalMap<>(); + } + + @Override + public Directory getSubdirectory(K key) { + return children.get(key); + } + + @Override + public boolean hasSubdirectory(K key) { + return children.containsKey(key); + } + + @Override + public Directory putSubdirectory(K key, Directory val) { + if(data.containsKey(key)) { + throw new IllegalArgumentException("Key " + key + " is already used for data."); + } + + return children.put(key, val); + } + + @Override + public boolean containsKey(K key) { + return data.containsKey(key); + } + + @Override + public V getKey(K key) { + return data.get(key); + } + + @Override + public V putKey(K key, V val) { + if(children.containsKey(key)) { + throw new IllegalArgumentException("Key " + key + " is already used for sub-directories."); + } + + return data.put(key, val); + } +} \ No newline at end of file -- cgit v1.2.3