Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Orme2011-05-31 22:17:08 +0000
committerDave Orme2011-05-31 22:17:08 +0000
commitd62c693b8ed2e13097606159ada1f84637fbfab7 (patch)
tree12d3ee4941842bc2db1b0a4c8f923380c77de7ee
parent4acfe1a1ece260c9e2b6e296b46ebc6a9146e688 (diff)
downloadorg.eclipse.e4.utils-d62c693b8ed2e13097606159ada1f84637fbfab7.tar.gz
org.eclipse.e4.utils-d62c693b8ed2e13097606159ada1f84637fbfab7.tar.xz
org.eclipse.e4.utils-d62c693b8ed2e13097606159ada1f84637fbfab7.zip
Added an option to return an IStatus from an Option. The IStatus can provide
extra information (that one might want to log) about successful operations, and should provide the error status and exception on failure.
-rw-r--r--bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/None.java31
-rw-r--r--bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Option.java15
-rw-r--r--bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Some.java35
3 files changed, 79 insertions, 2 deletions
diff --git a/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/None.java b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/None.java
index b81a640..b18214d 100644
--- a/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/None.java
+++ b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/None.java
@@ -10,17 +10,28 @@
******************************************************************************/
package org.eclipse.e4.core.functionalprog.optionmonad;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+
/**
* An Option instance that does not contain any value.
*
* @param <T> The type that this Option is encapsulating.
*/
public final class None<T> implements Option<T> {
+ private final IStatus additionalInfo;
+
+ /**
+ * Construct a None<T>.
+ */
+ public None() { additionalInfo = Status.CANCEL_STATUS; }
/**
* Construct a None<T>.
*/
- public None() {}
+ public None(IStatus additionalInfo) {
+ this.additionalInfo = Nulls.valueOrSubstitute(additionalInfo, Status.CANCEL_STATUS);
+ }
/**
* A convenience factory method meant to be imported statically and that
@@ -33,6 +44,17 @@ public final class None<T> implements Option<T> {
*/
public static <T> Option<T> none() { return new None<T>(); }
+ /**
+ * A convenience factory method meant to be imported statically and that
+ * eliminates a lot of the boilerplate that Java generics impose.
+ *
+ * @param <T> The type of Option object to create. Usually inferred
+ * automatically by the compiler.
+ *
+ * @return a new None<T>.
+ */
+ public static <T> Option<T> none(IStatus additionalInfo) { return new None<T>(additionalInfo); }
+
/* (non-Javadoc)
* @see org.eclipse.e4.core.functionalprog.optionmonad.Option#get()
*/
@@ -60,4 +82,11 @@ public final class None<T> implements Option<T> {
public boolean hasValue() {
return false;
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.core.functionalprog.optionmonad.Option#getStatus()
+ */
+ public IStatus getStatus() {
+ return additionalInfo;
+ }
} \ No newline at end of file
diff --git a/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Option.java b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Option.java
index 7b5ec43..09a5175 100644
--- a/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Option.java
+++ b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Option.java
@@ -9,6 +9,8 @@
* David Orme - initial API and implementation
******************************************************************************/
package org.eclipse.e4.core.functionalprog.optionmonad;
+
+import org.eclipse.core.runtime.IStatus;
/**
* A Java implementation of the "Option Monad" design pattern. (Note that this
@@ -64,6 +66,17 @@ public interface Option<T> {
*
* @return true if this Option contains a value or false if it is empty.
*/
- public boolean hasValue();
+ public boolean hasValue();
+
+ /**
+ * Returns an IStatus providing further information on this result. If
+ * clients do not explicitly provide an IStatus result, then Some(value)
+ * will return Status.OK_STATUS and None will return Status.CANCEL_STATUS.
+ * Alternatively, clients can supply their own IStatus to return along with
+ * Some(value) or None.
+ *
+ * @return IStatus additional information on this operation.
+ */
+ public IStatus getStatus();
}
diff --git a/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Some.java b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Some.java
index f6521d7..40eb1e6 100644
--- a/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Some.java
+++ b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Some.java
@@ -10,6 +10,9 @@
******************************************************************************/
package org.eclipse.e4.core.functionalprog.optionmonad;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+
/**
* An Option instance that contains a value.
*
@@ -17,6 +20,7 @@ package org.eclipse.e4.core.functionalprog.optionmonad;
*/
public final class Some<T> implements Option<T> {
private final T value;
+ private final IStatus additionalInfo;
/**
* Construct an Option with Some(value).
@@ -25,6 +29,17 @@ public final class Some<T> implements Option<T> {
*/
public Some(T value) {
this.value = value;
+ this.additionalInfo = Status.OK_STATUS;
+ }
+
+ /**
+ * Construct an Option with Some(value).
+ *
+ * @param value The value to encapsulate.
+ */
+ public Some(T value, IStatus additionalInfo) {
+ this.value = value;
+ this.additionalInfo = Nulls.valueOrSubstitute(additionalInfo, Status.OK_STATUS);
}
/**
@@ -38,6 +53,19 @@ public final class Some<T> implements Option<T> {
*/
public static <T> Option<T> some(T value) { return new Some<T>(value); }
+ /**
+ * A convenience factory method meant to be imported statically and that
+ * eliminates a lot of the boilerplate that Java generics impose.
+ *
+ * @param <T> The type of Option object to create. Usually inferred
+ * automatically by the compiler.
+ * @param additionalInfo An IStatus providing more information on this operation.
+ *
+ * @return a new Some<T> containing the specified value.
+ */
+ public static <T> Option<T> some(T value, IStatus additionalInfo) { return new Some<T>(value, additionalInfo); }
+
+
/* (non-Javadoc)
* @see org.eclipse.e4.core.functionalprog.optionmonad.Option#get()
*/
@@ -65,5 +93,12 @@ public final class Some<T> implements Option<T> {
public boolean hasValue() {
return true;
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.core.functionalprog.optionmonad.Option#getStatus()
+ */
+ public IStatus getStatus() {
+ return additionalInfo;
+ }
}

Back to the top