diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-11 05:29:03 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-11 05:29:03 -0500 |
| commit | 6f4b1e1663660d46523804c1cbb707e31686b31d (patch) | |
| tree | ae64c1b5842242f6e197591a24fd95850f7fe596 /BJC-Utils2/src/main | |
| parent | 4631f2cec9d94a0b4869fe1fc5c3c036f42a8db0 (diff) | |
Directory finished.
The basics of the directory are finished.
Diffstat (limited to 'BJC-Utils2/src/main')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java | 107 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/esodata/TreeDict.java | 18 |
2 files changed, 107 insertions, 18 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java new file mode 100644 index 0000000..9753feb --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java @@ -0,0 +1,107 @@ +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<K, V> { + private IMap<K, TreeDict<K, V>> children; + + private IMap<K, V> 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<K, V> newSubdirectory(K key) { + if(children.containsKey(key)) return null; + + Directory<K, V> 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<K, V> 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); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/TreeDict.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/TreeDict.java deleted file mode 100644 index 231abbc..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/TreeDict.java +++ /dev/null @@ -1,18 +0,0 @@ -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 class TreeDict<K, V> { - private Map<K, TreeDict<K, V>> children; - - private Map<K, V> data; - - public T -} |
