diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-26 11:30:43 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-26 11:30:43 -0400 |
| commit | 98cdf435d4974f4cca8f7b4eb4026da2c88cbc4c (patch) | |
| tree | ce5edea29a4da719affe61afda30a50afae15e87 /BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java | |
| parent | b53c1ef47c438fb1144b961d1939c37a4bfe9120 (diff) | |
Update
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java | 174 |
1 files changed, 100 insertions, 74 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java index d83b5c2..6bb5de6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java @@ -5,109 +5,124 @@ import java.util.function.Consumer; import java.util.function.Function; /** - * Functional wrapper over map providing some useful things + * Functional wrapper over map providing some useful things. * * @author ben * * @param <KeyType> - * The type of this map's keys + * The type of this map's keys. + * * @param <ValueType> - * The type of this map's values - * + * The type of this map's values. */ public interface IMap<KeyType, ValueType> { /** - * Delete all the values in the map - */ - void clear(); - - /** - * Check if this map contains the specified key + * Execute an action for each entry in the map. * - * @param key - * The key to check - * @return Whether or not the map contains the key - */ - boolean containsKey(KeyType key); - - /** - * Extends this map, creating a new map that will delegate queries to - * the map, but store any added values itself - * - * @return An extended map + * @param action + * the action to execute for each entry in the map. */ - IMap<KeyType, ValueType> extend(); + void forEach(BiConsumer<KeyType, ValueType> action); /** - * Execute an action for each entry in the map + * Perform an action for each key in the map. * * @param action - * the action to execute for each entry in the map + * The action to perform on each key in the map. */ - void forEach(BiConsumer<KeyType, ValueType> action); + default void forEachKey(Consumer<KeyType> action) { + forEach((key, val) -> action.accept(key)); + } /** - * Perform an action for each key in the map + * Perform an action for each value in the map. * * @param action - * The action to perform on each key in the map + * The action to perform on each value in the map. */ - void forEachKey(Consumer<KeyType> action); + default void forEachValue(Consumer<ValueType> action) { + forEach((key, val) -> action.accept(val)); + } /** - * Perform an action for each value in the map + * Check if this map contains the specified key. * - * @param action - * The action to perform on each value in the map + * @param key + * The key to check. + * + * @return Whether or not the map contains the key. */ - void forEachValue(Consumer<ValueType> action); + boolean containsKey(KeyType key); /** - * Get the value assigned to the given key + * Get the value assigned to the given key. * * @param key - * The key to look for a value under - * @return The value of the key - * - * + * The key to look for a value under. + * + * @return The value of the key. */ ValueType get(KeyType key); /** * Get a value from the map, and return a default value if the key - * doesn't exist + * doesn't exist. * * @param key - * The key to attempt to retrieve + * The key to attempt to retrieve. + * * @param defaultValue - * The value to return if the key doesn't exist + * The value to return if the key doesn't exist. + * * @return The value associated with the key, or the default value if - * the key doesn't exist + * the key doesn't exist. */ default ValueType getOrDefault(KeyType key, ValueType defaultValue) { try { return get(key); - } catch(IllegalArgumentException iaex) { - // We don't care about this, because it indicates a key - // is - // missing + } catch (IllegalArgumentException iaex) { + /* + * We don't care about this, because it indicates a key + * is missing. + */ return defaultValue; } } /** - * Get the number of entries in this map + * Add an entry to the map. * - * @return The number of entries in this map + * @param key + * The key to put the value under. + * + * @param val + * The value to add. + * + * @return The previous value of the key in the map, or null if the key + * wasn't in the map. However, note that it may also return null + * if the key was set to null. + * + * @throws UnsupportedOperationException + * if the map implementation doesn't support modifying + * the map + */ + ValueType put(KeyType key, ValueType val); + + /** + * Delete all the values in the map. */ - int getSize(); + default void clear() { + keyList().forEach((key) -> remove(key)); + } /** - * Get a list of all the keys in this map + * Get the number of entries in this map. * - * @return A list of all the keys in this map + * @return The number of entries in this map. */ - IList<KeyType> keyList(); + default int size() { + return keyList().getSize(); + } /** * Transform the values returned by this map. @@ -117,46 +132,57 @@ public interface IMap<KeyType, ValueType> { * likely not work as expected. * * @param <V2> - * The new type of returned values + * The new type of returned values. + * * @param transformer - * The function to use to transform values - * @return The map where each value will be transformed after lookup + * The function to use to transform values. + * + * @return The map where each value will be transformed after lookup. */ - <V2> IMap<KeyType, V2> mapValues(Function<ValueType, V2> transformer); + default <V2> IMap<KeyType, V2> transform(Function<ValueType, V2> transformer) { + return new TransformedValueMap<>(this, transformer); + } /** - * Add an entry to the map - * - * @param key - * The key to put the value under - * @param val - * The value to add - * @return The previous value of the key in the map, or null if the key - * wasn't in the map. However, note that it may also return null - * if the key was set to null. + * Extends this map, creating a new map that will delegate queries to + * the map, but store any added values itself. * - * @throws UnsupportedOperationException - * if the map implementation doesn't support modifying - * the map + * @return An extended map. */ - ValueType put(KeyType key, ValueType val); + IMap<KeyType, ValueType> extend(); /** - * Remove the value bound to the key + * Remove the value bound to the key. * * @param key - * The key to remove from the map + * The key to remove from the map. + * * @return The previous value for the key in the map, or null if the key * wasn't in the class. NOTE: Just because you received null, * doesn't mean the map wasn't changed. It may mean that someone - * put a null value for that key into the map + * put a null value for that key into the map. */ ValueType remove(KeyType key); /** - * Get a list of the values in this map + * Get a list of all the keys in this map. * - * @return A list of values in this map + * @return A list of all the keys in this map. */ - IList<ValueType> valueList(); + IList<KeyType> keyList(); + + /** + * Get a list of the values in this map. + * + * @return A list of values in this map. + */ + default IList<ValueType> valueList() { + IList<ValueType> returns = new FunctionalList<>(); + + for (KeyType key : keyList()) { + returns.add(get(key)); + } + + return returns; + } } |
