From 5b174110627839214c1413255e1c6715ff7d0aab Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 30 Mar 2017 23:09:24 +0100 Subject: Update java-mammoth to 1.3.1 --- Mammoth/Couscous/java/util/Optional.cs | 56 ++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 6 deletions(-) (limited to 'Mammoth/Couscous/java/util/Optional.cs') 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 map(Function function); Optional flatMap(Function> function); + Optional filter(Predicate predicate); T orElse(T value); T orElseGet(Supplier supplier); - T orElseThrow(Supplier exceptionSupplier) where TException : Exception; + T orElseThrow(Supplier exceptionSupplier) where TException : System.Exception; T get(); void ifPresent(Consumer consumer); } + + internal interface None { + } - internal struct None : Optional { + internal struct None : None, Optional { internal static readonly None Instance = new None(); + 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 flatMap(Function> function) { return new None(); } + + public Optional filter(Predicate 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(Supplier exceptionSupplier) where TException : Exception { + public T orElseThrow(Supplier exceptionSupplier) where TException : System.Exception { throw exceptionSupplier.get(); } @@ -49,13 +64,30 @@ namespace Mammoth.Couscous.java.util public void ifPresent(Consumer consumer) { } } + + internal interface Some { + object getObject(); + } - internal struct Some : Optional { + internal struct Some : Some, Optional { 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 flatMap(Function> function) { return function.apply(_value); } + + public Optional filter(Predicate predicate) { + if (predicate.test(_value)) { + return this; + } else { + return None.Instance; + } + } public T orElse(T value) { return _value; @@ -77,13 +117,17 @@ namespace Mammoth.Couscous.java.util return _value; } - public T orElseThrow(Supplier exceptionSupplier) where TException : Exception { + public T orElseThrow(Supplier exceptionSupplier) where TException : System.Exception { return _value; } public T get() { return _value; } + + public object getObject() { + return _value; + } public void ifPresent(Consumer consumer) { consumer.accept(_value); -- cgit v1.2.3