From 0a3946e579a0c358a13aa1621fecc3e220cd6e38 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Wed, 25 Oct 2023 19:33:02 -0400 Subject: Tweaks --- src/main/java/bjc/data/FlatMapIterator.java | 52 +++++++++++ src/main/java/bjc/functypes/MapBuilder.java | 132 ++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 src/main/java/bjc/data/FlatMapIterator.java create mode 100644 src/main/java/bjc/functypes/MapBuilder.java diff --git a/src/main/java/bjc/data/FlatMapIterator.java b/src/main/java/bjc/data/FlatMapIterator.java new file mode 100644 index 0000000..47bc06c --- /dev/null +++ b/src/main/java/bjc/data/FlatMapIterator.java @@ -0,0 +1,52 @@ +package bjc.data; + +import java.util.*; +import java.util.function.Function; + +/** + * Create an iterator that applies a flat-map function + * + * @author bjcul + * + * @param The source type + * @param The output type + * + */ +public class FlatMapIterator implements Iterator { + private Function> action; + + private Iterator source; + private Iterator output; + /** + * Create a new flat-map iterator + * + * @param source The source iterator + * @param action The action to apply to the iterator + */ + public FlatMapIterator(Iterator source, Function> action) { + this.source = source; + this.action = action; + + } + + @Override + public boolean hasNext() { + if (output != null && output.hasNext()) return true; + while (source.hasNext()) { + output = action.apply(source.next()); + if (output.hasNext()) return true; + } + return false; + } + + @Override + public T2 next() { + if (output != null && output.hasNext()) return output.next(); + while (source.hasNext()) { + output = action.apply(source.next()); + if (output.hasNext()) return output.next(); + } + throw new NoSuchElementException(); + } + +} diff --git a/src/main/java/bjc/functypes/MapBuilder.java b/src/main/java/bjc/functypes/MapBuilder.java new file mode 100644 index 0000000..1fa98e2 --- /dev/null +++ b/src/main/java/bjc/functypes/MapBuilder.java @@ -0,0 +1,132 @@ +package bjc.functypes; + +import java.util.*; +import java.util.function.Function; + +import bjc.funcdata.Freezable; +import bjc.funcdata.ObjectFrozen; + +/** + * Functional interface for a generic builder using a map + * + * @author bjcul + * + * @param Key type of the map + * @param Value type of the map + * @param Result type of the builder + */ +public interface MapBuilder extends Freezable> { + /** + * Build an instance using the current map + * + * @return An instance constructed using the map + */ + R build(); + + /** + * Add a value to the map + * + * @param key The key to add + * @param value The value to add + * + * @return The builder, for chaining + */ + MapBuilder add(K key, V value); + + /** + * Remove a value from the map + * + * @param key The key to remove + * + * @return The removed value + */ + V remove(K key); + + /** + * Clear the contents of the map + */ + void clear(); + + /** + * Get the internal map. + * + * @return The internal map + */ + Map getMap(); + + /** + * Create a new map-builder from a function + * + * @param The key type of the map + * @param The value type of the map + * @param The result of the builder + * + * @param f The building function + * + * @return A map-builder using the function + */ + static MapBuilder from(Function, R> f) { + return new FunctionalMapBuilder<>(f); + } +} + +final class FunctionalMapBuilder implements MapBuilder { + private final Function, R> f; + private Map mep = new HashMap<>(); + + private boolean frozen = false; + + FunctionalMapBuilder(Function, R> f) { + this.f = f; + } + + @Override + public R build() { + return f.apply(mep); + } + + @Override + public MapBuilder add(K key, V value) { + if (frozen) throw new ObjectFrozen(); + + mep.put(key, value); + return this; + } + + @Override + public V remove(K key) { + if (frozen) throw new ObjectFrozen(); + + return mep.remove(key); + } + + @Override + public void clear() { + if (frozen) throw new ObjectFrozen(); + + mep.clear(); + } + + @Override + public Map getMap() { + if (frozen) return Collections.unmodifiableMap(mep); + return mep; + } + + @Override + public boolean freeze() { + frozen = true; + return true; + } + + @Override + public boolean thaw() { + frozen = false; + return true; + } + + @Override + public boolean isFrozen() { + return frozen; + } +} \ No newline at end of file -- cgit v1.2.3