From 275a627719fc2231b16caea41130ff09f0f2b6a1 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 8 Apr 2016 13:28:09 -0400 Subject: Switch functional data to use interfaces --- .../java/bjc/utils/funcdata/FunctionalMap.java | 147 ++++++++++++++------- 1 file changed, 102 insertions(+), 45 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java') 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 0453988..fc4c4de 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java @@ -2,27 +2,40 @@ package bjc.utils.funcdata; import java.util.HashMap; import java.util.Map; +import java.util.function.BiConsumer; import java.util.function.Function; import bjc.utils.data.Pair; /** - * Functional wrapper over map providing some useful things + * Basic implementation of {@link IFunctionalMap} * * @author ben - * + * * @param - * The type of this map's keys + * The type of the map's keys * @param - * The type of this map's values - * + * The type of the map's values */ -public class FunctionalMap { - private final class TransformedMap extends FunctionalMap { - private FunctionalMap mapToTransform; - private Function transformer; +public class FunctionalMap implements IFunctionalMap { + /** + * A map that transforms values from one type to another + * + * @author ben + * + * @param + * The type of the map's keys + * @param + * The type of the map's values + * @param + * The type of the transformed values + */ + private static final class TransformedValueMap + implements IFunctionalMap { + private IFunctionalMap mapToTransform; + private Function transformer; - public TransformedMap(FunctionalMap destMap, + public TransformedValueMap(IFunctionalMap destMap, Function transform) { mapToTransform = destMap; transformer = transform; @@ -42,6 +55,40 @@ public class FunctionalMap { public String toString() { return mapToTransform.toString(); } + + @Override + public V2 put(K key, V2 val) { + throw new UnsupportedOperationException( + "Can't add items to transformed map"); + } + + @Override + public IFunctionalMap mapValues( + Function transform) { + return new TransformedValueMap<>(this, transform); + } + + @Override + public IFunctionalList keyList() { + return mapToTransform.keyList(); + } + + @Override + public void forEach(BiConsumer action) { + mapToTransform.forEach((key, val) -> { + action.accept(key, transformer.apply(val)); + }); + } + + @Override + public V2 remove(K key) { + return transformer.apply(mapToTransform.remove(key)); + } + + @Override + public int getSize() { + return mapToTransform.getSize(); + } } private Map wrappedMap; @@ -84,18 +131,12 @@ public class FunctionalMap { } } - /** - * 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. + /* + * (non-Javadoc) * + * @see bjc.utils.funcdata.IFunctionalMap#put(K, V) */ + @Override public V put(K key, V val) { if (key == null) { throw new NullPointerException("Key must not be null"); @@ -104,15 +145,12 @@ public class FunctionalMap { return wrappedMap.put(key, val); } - /** - * Get the value assigned to the given key - * - * @param key - * The key to look for a value under - * @return The value of the key - * + /* + * (non-Javadoc) * + * @see bjc.utils.funcdata.IFunctionalMap#get(K) */ + @Override public V get(K key) { if (key == null) { throw new NullPointerException("Key must not be null"); @@ -126,35 +164,28 @@ public class FunctionalMap { return wrappedMap.get(key); } - /** - * Transform the values returned by this map. + /* + * (non-Javadoc) * - * NOTE: This transform is applied once for each lookup of a value, so - * the transform passed should be a proper function, or things will - * likely not work as expected. - * - * @param - * 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 + * @see bjc.utils.funcdata.IFunctionalMap#mapValues(java.util.function. + * Function) */ - public FunctionalMap mapValues( + @Override + public IFunctionalMap mapValues( Function transformer) { if (transformer == null) { throw new NullPointerException("Transformer must not be null"); } - return new TransformedMap<>(this, transformer); + return new TransformedValueMap<>(this, transformer); } - /** - * Check if this map contains the specified key + /* + * (non-Javadoc) * - * @param key - * The key to check - * @return Whether or not the map contains the key + * @see bjc.utils.funcdata.IFunctionalMap#containsKey(K) */ + @Override public boolean containsKey(K key) { return wrappedMap.containsKey(key); } @@ -163,4 +194,30 @@ public class FunctionalMap { public String toString() { return wrappedMap.toString(); } + + @Override + public IFunctionalList keyList() { + FunctionalList keys = new FunctionalList<>(); + + wrappedMap.keySet().forEach((key) -> { + keys.add(key); + }); + + return keys; + } + + @Override + public void forEach(BiConsumer action) { + wrappedMap.forEach(action); + } + + @Override + public V remove(K key) { + return wrappedMap.remove(key); + } + + @Override + public int getSize() { + return wrappedMap.size(); + } } \ No newline at end of file -- cgit v1.2.3