aboutsummaryrefslogtreecommitdiff
path: root/Mammoth/Couscous/java/util/Optional.cs
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-03-30 23:09:24 +0100
committerMichael Williamson <mike@zwobble.org>2017-04-01 16:15:41 +0100
commit5b174110627839214c1413255e1c6715ff7d0aab (patch)
tree3f2a97c071f82c0e44d9846a931f813474250764 /Mammoth/Couscous/java/util/Optional.cs
parent99ed1b53f084322c06975a2183d812db5f257d34 (diff)
Update java-mammoth to 1.3.1
Diffstat (limited to 'Mammoth/Couscous/java/util/Optional.cs')
-rw-r--r--Mammoth/Couscous/java/util/Optional.cs56
1 files changed, 50 insertions, 6 deletions
diff --git a/Mammoth/Couscous/java/util/Optional.cs b/Mammoth/Couscous/java/util/Optional.cs
index 0f02c89..1ae8e00 100644
--- a/Mammoth/Couscous/java/util/Optional.cs
+++ b/Mammoth/Couscous/java/util/Optional.cs
@@ -1,4 +1,3 @@
-using System;
using Mammoth.Couscous.java.util.function;
namespace Mammoth.Couscous.java.util
@@ -8,16 +7,28 @@ namespace Mammoth.Couscous.java.util
bool isPresent();
Optional<U> map<U>(Function<T, U> function);
Optional<U> flatMap<U>(Function<T, Optional<U>> function);
+ Optional<T> filter(Predicate<T> predicate);
T orElse(T value);
T orElseGet(Supplier<T> supplier);
- T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : Exception;
+ T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception;
T get();
void ifPresent(Consumer<T> consumer);
}
+
+ internal interface None {
+ }
- internal struct None<T> : Optional<T> {
+ internal struct None<T> : None, Optional<T> {
internal static readonly None<T> Instance = new None<T>();
+ public override bool Equals(object other) {
+ return other is None;
+ }
+
+ public override int GetHashCode() {
+ return 0;
+ }
+
public bool isPresent() {
return false;
}
@@ -29,6 +40,10 @@ namespace Mammoth.Couscous.java.util
public Optional<U> flatMap<U>(Function<T, Optional<U>> function) {
return new None<U>();
}
+
+ public Optional<T> filter(Predicate<T> predicate) {
+ return this;
+ }
public T orElse(T value) {
return value;
@@ -38,7 +53,7 @@ namespace Mammoth.Couscous.java.util
return supplier.get();
}
- public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : Exception {
+ public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception {
throw exceptionSupplier.get();
}
@@ -49,13 +64,30 @@ namespace Mammoth.Couscous.java.util
public void ifPresent(Consumer<T> consumer) {
}
}
+
+ internal interface Some {
+ object getObject();
+ }
- internal struct Some<T> : Optional<T> {
+ internal struct Some<T> : Some, Optional<T> {
private readonly T _value;
internal Some(T value) {
_value = value;
}
+
+ public override bool Equals(object other) {
+ var otherSome = other as Some;
+ if (otherSome == null) {
+ return false;
+ } else {
+ return _value.Equals(otherSome.getObject());
+ }
+ }
+
+ public override int GetHashCode() {
+ return _value.GetHashCode();
+ }
public bool isPresent() {
return true;
@@ -68,6 +100,14 @@ namespace Mammoth.Couscous.java.util
public Optional<U> flatMap<U>(Function<T, Optional<U>> function) {
return function.apply(_value);
}
+
+ public Optional<T> filter(Predicate<T> predicate) {
+ if (predicate.test(_value)) {
+ return this;
+ } else {
+ return None<T>.Instance;
+ }
+ }
public T orElse(T value) {
return _value;
@@ -77,13 +117,17 @@ namespace Mammoth.Couscous.java.util
return _value;
}
- public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : Exception {
+ public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception {
return _value;
}
public T get() {
return _value;
}
+
+ public object getObject() {
+ return _value;
+ }
public void ifPresent(Consumer<T> consumer) {
consumer.accept(_value);