From 5b1f5568f25146b397bcdb7b570ddad74246969f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Wed, 16 Aug 2017 09:06:31 +0300 Subject: [PATCH] (refs #7433) Make Either type available in Java Change-Id: I9ee7e4ca0bb2bf682b00447d8a44568276e74083 --- .../META-INF/MANIFEST.MF | 1 + .../org.simantics.scl.runtime/scl/Prelude.scl | 9 +++++- .../simantics/scl/runtime/either/Either.java | 4 +++ .../simantics/scl/runtime/either/Left.java | 29 +++++++++++++++++++ .../simantics/scl/runtime/either/Right.java | 29 +++++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Either.java create mode 100644 bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Left.java create mode 100644 bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Right.java diff --git a/bundles/org.simantics.scl.runtime/META-INF/MANIFEST.MF b/bundles/org.simantics.scl.runtime/META-INF/MANIFEST.MF index aec2f1b85..382af3fc4 100644 --- a/bundles/org.simantics.scl.runtime/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.scl.runtime/META-INF/MANIFEST.MF @@ -7,6 +7,7 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Export-Package: org.simantics.scl.runtime, org.simantics.scl.runtime.chr, org.simantics.scl.runtime.collection, + org.simantics.scl.runtime.either, org.simantics.scl.runtime.exceptions, org.simantics.scl.runtime.function, org.simantics.scl.runtime.io, diff --git a/bundles/org.simantics.scl.runtime/scl/Prelude.scl b/bundles/org.simantics.scl.runtime/scl/Prelude.scl index eb1e8cd6f..818c50543 100644 --- a/bundles/org.simantics.scl.runtime/scl/Prelude.scl +++ b/bundles/org.simantics.scl.runtime/scl/Prelude.scl @@ -1273,7 +1273,14 @@ The Either type represents values with two possibilities: a value of type `Eithe The `Either` type is sometimes used to represent a value which is either correct or an error; by convention, the `Left` constructor is used to hold an error value and the `Right` constructor is used to hold a correct value (mnemonic: "right" also means "correct"). """ -data Either a b = Left a | Right b +@JavaType "org.simantics.scl.runtime.either.Either" +data Either a b = + @JavaType "org.simantics.scl.runtime.either.Left" + @FieldNames [value] + Left a + | @JavaType "org.simantics.scl.runtime.either.Right" + @FieldNames [value] + Right b deriving instance (Ord a, Ord b) => Ord (Either a b) deriving instance (Show a, Show b) => Show (Either a b) diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Either.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Either.java new file mode 100644 index 000000000..c79290782 --- /dev/null +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Either.java @@ -0,0 +1,4 @@ +package org.simantics.scl.runtime.either; + +public interface Either { +} diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Left.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Left.java new file mode 100644 index 000000000..1e0c29b1f --- /dev/null +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Left.java @@ -0,0 +1,29 @@ +package org.simantics.scl.runtime.either; + +public class Left implements Either { + public final Object value; + + public Left(Object value) { + this.value = value; + } + + @Override + public String toString() { + return "Left " + value; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) + return true; + if(obj == null || obj.getClass() != getClass()) + return false; + Right other = (Right)obj; + return value == null ? other.value == null : value.equals(other.value); + } + + @Override + public int hashCode() { + return 31 * (value == null ? 0 : value.hashCode()) + 13532; + } +} diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Right.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Right.java new file mode 100644 index 000000000..27aeec914 --- /dev/null +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Right.java @@ -0,0 +1,29 @@ +package org.simantics.scl.runtime.either; + +public class Right implements Either { + public final Object value; + + public Right(Object value) { + this.value = value; + } + + @Override + public String toString() { + return "Right " + value; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) + return true; + if(obj == null || obj.getClass() != getClass()) + return false; + Right other = (Right)obj; + return value == null ? other.value == null : value.equals(other.value); + } + + @Override + public int hashCode() { + return 31 * (value == null ? 0 : value.hashCode()) + 13533; + } +} -- 2.47.1