diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-21 22:53:56 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-21 22:53:56 -0500 |
| commit | 86ad50b4df6166c6f99b08d91d49fe59ee754619 (patch) | |
| tree | 75465a1fa0a99b9c83026953b6a2ed0d736c0a93 /src/main/java/bjc/esodata/PushdownMap.java | |
| parent | c3954bbe63378324762a6cdd5caf363405340973 (diff) | |
Change IMap to use Optional, not exceptions
IMap now returns optionals, instead of throwing an exception
Diffstat (limited to 'src/main/java/bjc/esodata/PushdownMap.java')
| -rw-r--r-- | src/main/java/bjc/esodata/PushdownMap.java | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/main/java/bjc/esodata/PushdownMap.java b/src/main/java/bjc/esodata/PushdownMap.java index a30161c..410eb53 100644 --- a/src/main/java/bjc/esodata/PushdownMap.java +++ b/src/main/java/bjc/esodata/PushdownMap.java @@ -1,7 +1,9 @@ package bjc.esodata; +import java.util.*; import java.util.function.*; +import bjc.data.*; import bjc.funcdata.*; /** @@ -48,8 +50,8 @@ public class PushdownMap<KeyType, ValueType> implements IMap<KeyType, ValueType> } @Override - public ValueType get(final KeyType key) { - return backing.get(key).top(); + public Optional<ValueType> get(final KeyType key) { + return backing.get(key).map((stack) -> stack.top()); } @Override @@ -67,7 +69,7 @@ public class PushdownMap<KeyType, ValueType> implements IMap<KeyType, ValueType> if (isFrozen) throw new ObjectFrozen("Can't insert key " + key + " into frozen map"); if (backing.containsKey(key)) { - final Stack<ValueType> stk = backing.get(key); + final Stack<ValueType> stk = backing.get(key).get(); final ValueType vl = stk.top(); @@ -86,12 +88,18 @@ public class PushdownMap<KeyType, ValueType> implements IMap<KeyType, ValueType> @Override public ValueType remove(final KeyType key) { if (isFrozen) throw new ObjectFrozen("Can't remove key " + key + " from frozen map"); - - final Stack<ValueType> stk = backing.get(key); - - if (stk.size() > 1) return stk.pop(); - return backing.remove(key).top(); + IHolder<ValueType> result = IHolder.of(null); + + backing.get(key).ifPresent((stk) -> { + if (stk.size() > 1) { + result.replace(stk.pop()); + } else { + result.replace(backing.remove(key).top()); + } + }); + + return result.getValue(); } @Override |
