diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata')
5 files changed, 124 insertions, 82 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java index d7bb0de..49382bc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java @@ -62,8 +62,8 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { } @Override - public int getSize() { - return store.getSize() + delegate.getSize(); + public int size() { + return store.size() + delegate.size(); } @Override @@ -72,7 +72,7 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { } @Override - public <MappedValue> IMap<KeyType, MappedValue> mapValues(Function<ValueType, MappedValue> transformer) { + public <MappedValue> IMap<KeyType, MappedValue> transform(Function<ValueType, MappedValue> transformer) { return new TransformedValueMap<>(this, transformer); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java index 10a727c..b4e5981 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java @@ -98,7 +98,7 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp } @Override - public int getSize() { + public int size() { return wrappedMap.size(); } @@ -114,7 +114,7 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp } @Override - public <MappedValue> IMap<KeyType, MappedValue> mapValues(Function<ValueType, MappedValue> transformer) { + public <MappedValue> IMap<KeyType, MappedValue> transform(Function<ValueType, MappedValue> transformer) { if(transformer == null) throw new NullPointerException("Transformer must not be null"); return new TransformedValueMap<>(this, transformer); 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; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/SentryList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/SentryList.java index 060f69e..f52a286 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/SentryList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/SentryList.java @@ -2,11 +2,27 @@ package bjc.utils.funcdata; import java.util.List; +/** + * A list that logs when items are inserted into it. + * + * @author bjculkin + * + * @param <T> The type of item in the list. + */ public class SentryList<T> extends FunctionalList<T> { + /** + * Create a new sentry list. + */ public SentryList() { super(); } + /** + * Create a new sentry list backed by an existing list. + * + * @param backing + * The backing list. + */ public SentryList(List<T> backing) { super(backing); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java index d4e1762..89ea96d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java @@ -65,8 +65,8 @@ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldK } @Override - public int getSize() { - return backing.getSize(); + public int size() { + return backing.size(); } @Override @@ -75,7 +75,7 @@ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldK } @Override - public <MappedValue> IMap<OldKey, MappedValue> mapValues(Function<NewValue, MappedValue> transform) { + public <MappedValue> IMap<OldKey, MappedValue> transform(Function<NewValue, MappedValue> transform) { return new TransformedValueMap<>(this, transform); } |
