From 0f9ef005f063198ea2e16ca36beaead4bd5e3824 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 14 Apr 2016 21:12:07 +0100 Subject: Add .NET output --- dotnet/Mammoth/Couscous/java/util/Optional.cs | 103 ++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 dotnet/Mammoth/Couscous/java/util/Optional.cs (limited to 'dotnet/Mammoth/Couscous/java/util/Optional.cs') diff --git a/dotnet/Mammoth/Couscous/java/util/Optional.cs b/dotnet/Mammoth/Couscous/java/util/Optional.cs new file mode 100644 index 0000000..f295688 --- /dev/null +++ b/dotnet/Mammoth/Couscous/java/util/Optional.cs @@ -0,0 +1,103 @@ +using System; +using Mammoth.Couscous.java.util.function; + +namespace Mammoth.Couscous.java.util +{ + internal interface Optional + { + bool isPresent(); + Optional map(Function function); + Optional flatMap(Function> function); + T orElse(T value); + T orElseGet(Supplier supplier); + T orElseThrow(Supplier exceptionSupplier) where TException : Exception; + T get(); + void ifPresent(Consumer consumer); + } + + internal class None : Optional { + public bool isPresent() { + return false; + } + + public Optional map(Function function) { + return new None(); + } + + public Optional flatMap(Function> function) { + return new None(); + } + + public T orElse(T value) { + return value; + } + + public T orElseGet(Supplier supplier) { + return supplier.get(); + } + + public T orElseThrow(Supplier exceptionSupplier) where TException : Exception { + throw exceptionSupplier.get(); + } + + public T get() { + throw new NoSuchElementException(); + } + + public void ifPresent(Consumer consumer) { + } + } + + internal class Some : Optional { + private readonly T _value; + + internal Some(T value) { + _value = value; + } + + public bool isPresent() { + return true; + } + + public Optional map(Function function) { + return new Some(function.apply(_value)); + } + + public Optional flatMap(Function> function) { + return function.apply(_value); + } + + public T orElse(T value) { + return _value; + } + + public T orElseGet(Supplier supplier) { + return _value; + } + + public T orElseThrow(Supplier exceptionSupplier) where TException : Exception { + return _value; + } + + public T get() { + return _value; + } + + public void ifPresent(Consumer consumer) { + consumer.accept(_value); + } + } + + internal static class Optional + { + internal static Optional empty() + { + return new None(); + } + + internal static Optional of(T value) + { + return new Some (value); + } + } +} -- cgit v1.2.3