summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/funcdata/FunctionalMap.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/bjc/funcdata/FunctionalMap.java')
-rw-r--r--src/main/java/bjc/funcdata/FunctionalMap.java149
1 files changed, 74 insertions, 75 deletions
diff --git a/src/main/java/bjc/funcdata/FunctionalMap.java b/src/main/java/bjc/funcdata/FunctionalMap.java
index aba3dd1..3427a91 100644
--- a/src/main/java/bjc/funcdata/FunctionalMap.java
+++ b/src/main/java/bjc/funcdata/FunctionalMap.java
@@ -1,15 +1,12 @@
package bjc.funcdata;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.function.BiConsumer;
-import java.util.function.Consumer;
-import java.util.function.Function;
+import java.util.*;
+import java.util.function.*;
-import bjc.data.IPair;
+import bjc.data.*;
/**
- * Basic implementation of {@link IMap}
+ * Basic implementation of {@link MapEx}
*
* @author ben
*
@@ -19,10 +16,13 @@ import bjc.data.IPair;
* @param <ValueType>
* The type of the map's values.
*/
-public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueType> {
+public class FunctionalMap<KeyType, ValueType> implements MapEx<KeyType, ValueType> {
/* Our backing store. */
private Map<KeyType, ValueType> wrappedMap;
+ private boolean isFrozen = false;
+ private boolean thawEnabled = true;
+
/** Create a new blank functional map */
public FunctionalMap() {
wrappedMap = new HashMap<>();
@@ -35,13 +35,11 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp
* The entries to put into the map.
*/
@SafeVarargs
- public FunctionalMap(final IPair<KeyType, ValueType>... entries) {
+ public FunctionalMap(final Pair<KeyType, ValueType>... entries) {
this();
- for (final IPair<KeyType, ValueType> entry : entries) {
- entry.doWith((key, val) -> {
- wrappedMap.put(key, val);
- });
+ for (final Pair<KeyType, ValueType> entry : entries) {
+ entry.doWith(wrappedMap::put);
}
}
@@ -52,14 +50,15 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp
* The map to wrap.
*/
public FunctionalMap(final Map<KeyType, ValueType> wrap) {
- if (wrap == null)
- throw new NullPointerException("Map to wrap must not be null");
+ if (wrap == null) throw new NullPointerException("Map to wrap must not be null");
wrappedMap = wrap;
}
@Override
public void clear() {
+ if (isFrozen) throw new ObjectFrozen("Can't clear frozen map");
+
wrappedMap.clear();
}
@@ -69,37 +68,19 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp
}
@Override
- public IMap<KeyType, ValueType> extend() {
- return new ExtendedMap<>(this, new FunctionalMap<>());
- }
-
- @Override
public void forEach(final BiConsumer<KeyType, ValueType> action) {
wrappedMap.forEach(action);
}
@Override
- public void forEachKey(final Consumer<KeyType> action) {
- wrappedMap.keySet().forEach(action);
- }
+ public Optional<ValueType> get(final KeyType key) {
+ if (key == null) throw new NullPointerException("Key must not be null");
- @Override
- public void forEachValue(final Consumer<ValueType> action) {
- wrappedMap.values().forEach(action);
- }
-
- @Override
- public ValueType get(final KeyType key) {
- if (key == null)
- throw new NullPointerException("Key must not be null");
-
- if (!wrappedMap.containsKey(key)) {
- final String msg = String.format("Key %s is not present in the map", key);
-
- throw new IllegalArgumentException(msg);
+ if (wrappedMap.containsKey(key)) {
+ return Optional.of(wrappedMap.get(key));
+ } else {
+ return Optional.empty();
}
-
- return wrappedMap.get(key);
}
@Override
@@ -108,35 +89,26 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp
}
@Override
- public IList<KeyType> keyList() {
+ public ListEx<KeyType> keyList() {
final FunctionalList<KeyType> keys = new FunctionalList<>();
- wrappedMap.keySet().forEach(key -> {
- keys.add(key);
- });
+ wrappedMap.keySet().forEach(keys::add);
return keys;
}
@Override
- public <MappedValue> IMap<KeyType, MappedValue>
- transform(final Function<ValueType, MappedValue> transformer) {
- if (transformer == null)
- throw new NullPointerException("Transformer must not be null");
-
- return new TransformedValueMap<>(this, transformer);
- }
-
- @Override
public ValueType put(final KeyType key, final ValueType val) {
- if (key == null)
- throw new NullPointerException("Key must not be null");
+ if (isFrozen) throw new ObjectFrozen("Can't put key " + key + " into frozen map");
+ if (key == null) throw new NullPointerException("Key must not be null");
return wrappedMap.put(key, val);
}
@Override
public ValueType remove(final KeyType key) {
+ if (isFrozen) throw new ObjectFrozen("Can't remove key " + key + " from frozen map");
+
return wrappedMap.remove(key);
}
@@ -144,18 +116,7 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp
public String toString() {
return wrappedMap.toString();
}
-
- @Override
- public IList<ValueType> valueList() {
- final FunctionalList<ValueType> values = new FunctionalList<>();
-
- wrappedMap.values().forEach(value -> {
- values.add(value);
- });
-
- return values;
- }
-
+
@Override
public int hashCode() {
final int prime = 31;
@@ -166,20 +127,58 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp
@Override
public boolean equals(final Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (!(obj instanceof FunctionalMap))
- return false;
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (!(obj instanceof FunctionalMap)) return false;
final FunctionalMap<?, ?> other = (FunctionalMap<?, ?>) obj;
if (wrappedMap == null) {
- if (other.wrappedMap != null)
- return false;
- } else if (!wrappedMap.equals(other.wrappedMap))
+ if (other.wrappedMap != null) return false;
+ } else if (!wrappedMap.equals(other.wrappedMap)) {
return false;
+ }
+
+ return true;
+ }
+
+ // IFreezable support
+ @Override
+ public boolean freeze() {
+ isFrozen = true;
+
return true;
}
+
+ @Override
+ public boolean thaw() {
+ if (thawEnabled) {
+ isFrozen = false;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public boolean deepFreeze() {
+ thawEnabled = false;
+
+ return freeze();
+ }
+
+ @Override
+ public boolean canFreeze() {
+ return true;
+ }
+
+ @Override
+ public boolean canThaw() {
+ return thawEnabled;
+ }
+
+ @Override
+ public boolean isFrozen() {
+ return isFrozen;
+ }
}