summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/funcdata/IMap.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-11-21 22:53:56 -0500
committerBen Culkin <scorpress@gmail.com>2020-11-21 22:53:56 -0500
commit86ad50b4df6166c6f99b08d91d49fe59ee754619 (patch)
tree75465a1fa0a99b9c83026953b6a2ed0d736c0a93 /src/main/java/bjc/funcdata/IMap.java
parentc3954bbe63378324762a6cdd5caf363405340973 (diff)
Change IMap to use Optional, not exceptions
IMap now returns optionals, instead of throwing an exception
Diffstat (limited to 'src/main/java/bjc/funcdata/IMap.java')
-rw-r--r--src/main/java/bjc/funcdata/IMap.java48
1 files changed, 13 insertions, 35 deletions
diff --git a/src/main/java/bjc/funcdata/IMap.java b/src/main/java/bjc/funcdata/IMap.java
index 4eaa9c0..ca9ef11 100644
--- a/src/main/java/bjc/funcdata/IMap.java
+++ b/src/main/java/bjc/funcdata/IMap.java
@@ -1,5 +1,6 @@
package bjc.funcdata;
+import java.util.*;
import java.util.function.*;
/**
@@ -60,31 +61,7 @@ public interface IMap<KeyType, ValueType> extends IFreezable {
*
* @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.
- *
- * @param key
- * The key to attempt to retrieve.
- *
- * @param defaultValue
- * 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.
- */
- default ValueType getOrDefault(final KeyType key, final ValueType defaultValue) {
- try {
- return get(key);
- } catch (final IllegalArgumentException iaex) {
- /*
- * We don't care about this, because it indicates a key is missing.
- */
- return defaultValue;
- }
- }
+ Optional<ValueType> get(KeyType key);
/**
* Add an entry to the map.
@@ -150,7 +127,7 @@ public interface IMap<KeyType, ValueType> extends IFreezable {
final IList<ValueType> returns = new FunctionalList<>();
for (final KeyType key : keyList()) {
- returns.add(get(key));
+ returns.add(get(key).orElse(null));
}
return returns;
@@ -191,17 +168,18 @@ public interface IMap<KeyType, ValueType> extends IFreezable {
return extend(new FunctionalMap<>());
};
+
/**
- * Extends this map, creating a new map that will delegate queries to this map,
- * but store any added values in the provided map.
- *
- * @param backer The map to store added values in.
- *
- * @return An extended map.
- */
+ * Extend this map, creating a new map that will delegate queries to
+ * the current map but store any added values in the provided map.
+ *
+ * @param backer The map to store values in.
+ *
+ * @return An extended map, with the specified backing map.
+ */
default IMap<KeyType, ValueType> extend(IMap<KeyType, ValueType> backer) {
- return new ExtendedMap<>(this, backer);
- };
+ return new ExtendedMap<>(this, backer);
+ };
/**
* Static method to create a basic instance of IMap.