From 77fcc58d1facffbc3af50be8c05985350e9f1355 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 17 Apr 2016 15:01:44 -0400 Subject: Code maintenace and changes --- .../main/java/bjc/utils/funcdata/ExtendedMap.java | 96 ++++++++++++++ .../java/bjc/utils/funcdata/FunctionalList.java | 13 +- .../java/bjc/utils/funcdata/FunctionalMap.java | 138 ++++----------------- .../java/bjc/utils/funcdata/IFunctionalList.java | 8 +- .../java/bjc/utils/funcdata/IFunctionalMap.java | 11 +- .../bjc/utils/funcdata/TransformedValueMap.java | 100 +++++++++++++++ .../src/main/java/bjc/utils/funcdata/Tree.java | 46 +++++-- 7 files changed, 287 insertions(+), 125 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java new file mode 100644 index 0000000..7e4c7fd --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java @@ -0,0 +1,96 @@ +package bjc.utils.funcdata; + +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; + +import bjc.utils.funcutils.ListUtils; + +class ExtendedMap + implements IFunctionalMap { + private IFunctionalMap delegate; + + private IFunctionalMap store; + + public ExtendedMap(IFunctionalMap delegate, + IFunctionalMap store) { + this.delegate = delegate; + this.store = store; + } + + @Override + public ValueType put(KeyType key, ValueType val) { + return store.put(key, val); + } + + @Override + public ValueType get(KeyType key) { + if (store.containsKey(key)) { + return store.get(key); + } + + return delegate.get(key); + } + + @Override + public IFunctionalMap mapValues( + Function transformer) { + return new TransformedValueMap<>(this, transformer); + } + + @Override + public boolean containsKey(KeyType key) { + if (store.containsKey(key)) { + return true; + } + + return delegate.containsKey(key); + } + + @Override + public IFunctionalList keyList() { + return ListUtils.mergeLists(store.keyList(), delegate.keyList()); + } + + @Override + public void forEach(BiConsumer action) { + store.forEach(action); + + delegate.forEach(action); + } + + @Override + public ValueType remove(KeyType key) { + return store.remove(key); + } + + @Override + public int getSize() { + return store.getSize() + delegate.getSize(); + } + + @Override + public void forEachKey(Consumer action) { + store.forEachKey(action); + + delegate.forEachKey(action); + } + + @Override + public void forEachValue(Consumer action) { + store.forEachValue(action); + + delegate.forEachValue(action); + } + + @Override + public IFunctionalList valueList() { + return ListUtils.mergeLists(store.valueList(), + delegate.valueList()); + } + + @Override + public IFunctionalMap extend() { + return new ExtendedMap<>(this, new FunctionalMap<>()); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java index 9b388b2..70d04cc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -11,10 +11,10 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; -import bjc.utils.data.experimental.IHolder; -import bjc.utils.data.experimental.IPair; -import bjc.utils.data.experimental.Identity; -import bjc.utils.data.experimental.Pair; +import bjc.utils.data.IHolder; +import bjc.utils.data.IPair; +import bjc.utils.data.Identity; +import bjc.utils.data.Pair; import java.util.ArrayList; @@ -590,4 +590,9 @@ public class FunctionalList implements Cloneable, IFunctionalList { public E[] toArray(E[] arrType) { return wrappedList.toArray(arrType); } + + @Override + public IFunctionalList tail() { + return new FunctionalList<>(wrappedList.subList(1, getSize())); + } } \ No newline at end of file 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 da30064..eaa425b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java @@ -6,110 +6,21 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; -import bjc.utils.data.experimental.IPair; +import bjc.utils.data.IPair; /** * Basic implementation of {@link IFunctionalMap} * * @author ben * - * @param + * @param * The type of the map's keys - * @param + * @param * The type of the map's values */ -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 TransformedValueMap(IFunctionalMap destMap, - Function transform) { - mapToTransform = destMap; - transformer = transform; - } - - @Override - public V2 get(K key) { - return transformer.apply(mapToTransform.get(key)); - } - - @Override - public boolean containsKey(K key) { - return mapToTransform.containsKey(key); - } - - @Override - 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(); - } - - @Override - public void forEachKey(Consumer action) { - mapToTransform.forEachKey(action); - } - - @Override - public void forEachValue(Consumer action) { - mapToTransform.forEachValue((val) -> { - action.accept(transformer.apply(val)); - }); - } - - @Override - public IFunctionalList valueList() { - return mapToTransform.valueList().map(transformer); - } - } - - private Map wrappedMap; +public class FunctionalMap + implements IFunctionalMap { + private Map wrappedMap; /** * Create a new blank functional map @@ -124,7 +35,7 @@ public class FunctionalMap implements IFunctionalMap { * @param wrap * The map to wrap */ - public FunctionalMap(Map wrap) { + public FunctionalMap(Map wrap) { if (wrap == null) { throw new NullPointerException("Map to wrap must not be null"); } @@ -139,10 +50,10 @@ public class FunctionalMap implements IFunctionalMap { * The entries to put into the map */ @SafeVarargs - public FunctionalMap(IPair... entries) { + public FunctionalMap(IPair... entries) { this(); - for (IPair entry : entries) { + for (IPair entry : entries) { entry.doWith((key, val) -> { wrappedMap.put(key, val); }); @@ -155,7 +66,7 @@ public class FunctionalMap implements IFunctionalMap { * @see bjc.utils.funcdata.IFunctionalMap#put(K, V) */ @Override - public V put(K key, V val) { + public ValueType put(KeyType key, ValueType val) { if (key == null) { throw new NullPointerException("Key must not be null"); } @@ -169,7 +80,7 @@ public class FunctionalMap implements IFunctionalMap { * @see bjc.utils.funcdata.IFunctionalMap#get(K) */ @Override - public V get(K key) { + public ValueType get(KeyType key) { if (key == null) { throw new NullPointerException("Key must not be null"); } @@ -189,8 +100,8 @@ public class FunctionalMap implements IFunctionalMap { * Function) */ @Override - public IFunctionalMap mapValues( - Function transformer) { + public IFunctionalMap mapValues( + Function transformer) { if (transformer == null) { throw new NullPointerException("Transformer must not be null"); } @@ -204,7 +115,7 @@ public class FunctionalMap implements IFunctionalMap { * @see bjc.utils.funcdata.IFunctionalMap#containsKey(K) */ @Override - public boolean containsKey(K key) { + public boolean containsKey(KeyType key) { return wrappedMap.containsKey(key); } @@ -214,8 +125,8 @@ public class FunctionalMap implements IFunctionalMap { } @Override - public IFunctionalList keyList() { - FunctionalList keys = new FunctionalList<>(); + public IFunctionalList keyList() { + FunctionalList keys = new FunctionalList<>(); wrappedMap.keySet().forEach((key) -> { keys.add(key); @@ -225,12 +136,12 @@ public class FunctionalMap implements IFunctionalMap { } @Override - public void forEach(BiConsumer action) { + public void forEach(BiConsumer action) { wrappedMap.forEach(action); } @Override - public V remove(K key) { + public ValueType remove(KeyType key) { return wrappedMap.remove(key); } @@ -240,18 +151,18 @@ public class FunctionalMap implements IFunctionalMap { } @Override - public void forEachKey(Consumer action) { + public void forEachKey(Consumer action) { wrappedMap.keySet().forEach(action); } @Override - public void forEachValue(Consumer action) { + public void forEachValue(Consumer action) { wrappedMap.values().forEach(action); } @Override - public IFunctionalList valueList() { - FunctionalList values = new FunctionalList<>(); + public IFunctionalList valueList() { + FunctionalList values = new FunctionalList<>(); wrappedMap.values().forEach((value) -> { values.add(value); @@ -259,4 +170,9 @@ public class FunctionalMap implements IFunctionalMap { return values; } + + @Override + public IFunctionalMap extend() { + return new ExtendedMap<>(this, new FunctionalMap<>()); + } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java index 4be7277..f22df57 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java @@ -7,7 +7,7 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; -import bjc.utils.data.experimental.IPair; +import bjc.utils.data.IPair; /** * A wrapper over another list that provides eager functional operations @@ -325,4 +325,10 @@ public interface IFunctionalList { * @return An iterable view onto the list */ Iterable toIterable(); + + /** + * Get the tail of this list (the list without the first element + * @return The list without the first element + */ + public IFunctionalList tail(); } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java index c5fe559..0c96a9f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java @@ -58,7 +58,8 @@ public interface IFunctionalMap { * The function to use to transform values * @return The map where each value will be transformed after lookup */ - IFunctionalMap mapValues(Function transformer); + IFunctionalMap mapValues( + Function transformer); /** * Check if this map contains the specified key @@ -125,4 +126,12 @@ public interface IFunctionalMap { * @return A list of values in this map */ IFunctionalList valueList(); + + /** + * Extends this map, creating a new map that will delegate queries to + * the map, but store any added values itself + * + * @return An extended map + */ + IFunctionalMap extend(); } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java new file mode 100644 index 0000000..75557fa --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java @@ -0,0 +1,100 @@ +package bjc.utils.funcdata; + +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * 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 + */ +final class TransformedValueMap + implements IFunctionalMap { + private IFunctionalMap mapToTransform; + private Function transformer; + + public TransformedValueMap(IFunctionalMap destMap, + Function transform) { + mapToTransform = destMap; + transformer = transform; + } + + @Override + public NewValue get(OldKey key) { + return transformer.apply(mapToTransform.get(key)); + } + + @Override + public boolean containsKey(OldKey key) { + return mapToTransform.containsKey(key); + } + + @Override + public String toString() { + return mapToTransform.toString(); + } + + @Override + public NewValue put(OldKey key, NewValue 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 NewValue remove(OldKey key) { + return transformer.apply(mapToTransform.remove(key)); + } + + @Override + public int getSize() { + return mapToTransform.getSize(); + } + + @Override + public void forEachKey(Consumer action) { + mapToTransform.forEachKey(action); + } + + @Override + public void forEachValue(Consumer action) { + mapToTransform.forEachValue((val) -> { + action.accept(transformer.apply(val)); + }); + } + + @Override + public IFunctionalList valueList() { + return mapToTransform.valueList().map(transformer); + } + + @Override + public IFunctionalMap extend() { + return new ExtendedMap<>(this, new FunctionalMap<>()); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java index d9938d4..5ee4200 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java @@ -6,6 +6,7 @@ import java.util.function.Predicate; import java.util.function.UnaryOperator; import bjc.utils.funcdata.bst.TreeLinearizationMethod; +import bjc.utils.funcutils.StringUtils; /** * A node in a homogenous tree. @@ -119,11 +120,11 @@ public class Tree implements ITree { Function leafTransform, Function, NewType>> nodeCollapser) { if (hasChildren) { - Function, NewType> nodeTransformer = nodeCollapser - .apply(data); + Function, NewType> nodeTransformer = + nodeCollapser.apply(data); - IFunctionalList collapsedChildren = children - .map((child) -> { + IFunctionalList collapsedChildren = + children.map((child) -> { return child.collapse(leafTransform, nodeCollapser, (subTreeVal) -> subTreeVal); }); @@ -164,8 +165,9 @@ public class Tree implements ITree { public ITree transformTree( Function transformer) { if (hasChildren) { - IFunctionalList> transformedChildren = children - .map((child) -> child.transformTree(transformer)); + IFunctionalList> transformedChildren = + children.map( + (child) -> child.transformTree(transformer)); return new Tree<>(transformer.apply(data), transformedChildren); @@ -219,8 +221,8 @@ public class Tree implements ITree { Function leafTransformer, Function operatorTransformer) { if (hasChildren) { - IFunctionalList> mappedChildren = children - .map((child) -> { + IFunctionalList> mappedChildren = + children.map((child) -> { return child.rebuildTree(leafTransformer, operatorTransformer); }); @@ -232,4 +234,32 @@ public class Tree implements ITree { return new Tree<>(leafTransformer.apply(data)); } + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + internalToString(builder, 1, true); + + builder.deleteCharAt(builder.length() - 1); + + return builder.toString(); + } + + protected void internalToString(StringBuilder builder, int indentLevel, + boolean initial) { + if (!initial) { + StringUtils.indentNLevels(builder, indentLevel); + } + + builder.append("Node: "); + builder.append(data == null ? "(null)" : data.toString()); + builder.append("\n"); + + if (hasChildren) { + children.forEach((child) -> { + ((Tree) child).internalToString(builder, + indentLevel + 2, false); + }); + } + } } -- cgit v1.2.3