From: Hannu Niemistö Date: Wed, 16 Aug 2017 06:06:31 +0000 (+0300) Subject: (refs #7433) Make Either type available in Java X-Git-Tag: v1.31.0~240 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=refs%2Fchanges%2F52%2F852%2F2 (refs #7433) Make Either type available in Java Change-Id: I9ee7e4ca0bb2bf682b00447d8a44568276e74083 --- 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; + } +}