CQ-4081: Initial import
diff --git a/bundles/org.eclipse.e4.core.functionalprog/.classpath b/bundles/org.eclipse.e4.core.functionalprog/.classpath
new file mode 100644
index 0000000..8a8f166
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/bundles/org.eclipse.e4.core.functionalprog/.project b/bundles/org.eclipse.e4.core.functionalprog/.project
new file mode 100644
index 0000000..e2d75da
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.e4.core.functionalprog</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/bundles/org.eclipse.e4.core.functionalprog/.settings/org.eclipse.jdt.core.prefs b/bundles/org.eclipse.e4.core.functionalprog/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..92071a7
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+#Tue Jan 12 15:23:43 CST 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.5
diff --git a/bundles/org.eclipse.e4.core.functionalprog/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.core.functionalprog/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..40f4870
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/META-INF/MANIFEST.MF
@@ -0,0 +1,9 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Fp Plug-in
+Bundle-SymbolicName: org.eclipse.e4.core.functionalprog
+Bundle-Version: 4.6.0.qualifier
+Require-Bundle: org.eclipse.core.runtime
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Bundle-ActivationPolicy: lazy
+Export-Package: org.eclipse.e4.core.functionalprog.optionmonad
diff --git a/bundles/org.eclipse.e4.core.functionalprog/about.html b/bundles/org.eclipse.e4.core.functionalprog/about.html
new file mode 100644
index 0000000..f77f378
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/about.html
@@ -0,0 +1,22 @@
+<h1>About This Content</h1>
+
+23 June, 2010
+
+<h2>License</h2>
+
+<p>The Eclipse Foundation makes available all content in this plug-in
+("Content"). Unless otherwise indicated below, the Content is provided
+to you under the terms and conditions of the Eclipse Public License
+Version 1.0 ("EPL"). A copy of the EPL is available at
+http://www.eclipse.org/legal/epl-v10.html. For purposes of the EPL,
+"Program" will mean the Content.</p>
+
+<p>If you did not receive this Content directly from the Eclipse
+Foundation, the Content is being redistributed by another party
+("Redistributor") and different terms and conditions may apply to your
+use of any object code in the Content. Check the Redistributor’s
+license that was provided with the Content. If no such license exists,
+contact the Redistributor. Unless otherwise indicated below, the terms
+and conditions of the EPL still apply to any source code in the
+Content and such source code may be obtained at
+http://www.eclipse.org.</p>
diff --git a/bundles/org.eclipse.e4.core.functionalprog/build.properties b/bundles/org.eclipse.e4.core.functionalprog/build.properties
new file mode 100644
index 0000000..41eb6ad
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/build.properties
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .
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
new file mode 100644
index 0000000..cccec42
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/None.java
@@ -0,0 +1,63 @@
+/******************************************************************************
+ * Copyright (c) David Orme and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * David Orme - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.e4.core.functionalprog.optionmonad;
+
+/**
+ * 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> {
+
+ /**
+ * Construct a None<T>.
+ */
+ public None() {}
+
+ /**
+ * 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() { return new None<T>(); }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.enterprise.utils.functionalprog.optionmonad.Option#get()
+ */
+ public T get() {
+ throw new UnsupportedOperationException("Cannot resolve value on None");
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.enterprise.utils.functionalprog.optionmonad.Option#getOrSubstitute(java.lang.Object)
+ */
+ public T getOrSubstitute(T defaultValue) {
+ return defaultValue;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.enterprise.utils.functionalprog.optionmonad.Option#getOrThrow(java.lang.Throwable)
+ */
+ public <E extends Throwable> T getOrThrow(E exception) throws E {
+ throw exception;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.enterprise.utils.functionalprog.optionmonad.Option#hasValue()
+ */
+ public boolean hasValue() {
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Nulls.java b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Nulls.java
new file mode 100644
index 0000000..84c3073
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Nulls.java
@@ -0,0 +1,84 @@
+/******************************************************************************
+ * Copyright (c) David Orme and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * David Orme - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.e4.core.functionalprog.optionmonad;
+
+import static org.eclipse.e4.core.functionalprog.optionmonad.None.none;
+import static org.eclipse.e4.core.functionalprog.optionmonad.Some.some;
+
+/**
+ * Convenience methods encoding the Option<T> style of dealing with an "empty"
+ * value when the value follows the traditional pattern of returning null
+ * as the "empty" value.
+ * <p>
+ * This is mainly related to Option monads in that it lets the programmer deal
+ * with null values similarly to the way s/he would use a proper Option monad
+ * with the goal of making null processing more explicit in the code.
+ * <p>
+ * However, we additionally supply a type-safe Option<T> factory for
+ * simply/easily wrapping a value that might or might not be null into Some<T>
+ * or None<T> without messy Java typecasts.
+ * <p>
+ * These methods are meant to be imported statically.
+ */
+public class Nulls {
+ /**
+ * A common way of processing results where a null value indicates
+ * failure is to throw an exception. This method makes this coding pattern
+ * explicit.
+ *
+ * @param <T>
+ * The type of value to process.
+ * @param <E>
+ * The type of exception we might throw.
+ * @param value
+ * The value to return if non-null.
+ * @param exception
+ * The exception to throw if value==null.
+ * @return value iff value != null.
+ * @throws E
+ * iff value == null.
+ */
+ public static <T, E extends Throwable> T valueOrThrow(T value, E exception) throws E {
+ if (value != null) return value;
+ throw exception;
+ }
+
+ /**
+ * A common way of processing results where a null value indicates
+ * failure is to substitute a default value. This method makes this coding
+ * pattern explicit.
+ *
+ * @param <T>
+ * The type of value we are processing.
+ * @param value
+ * The value to return iff value != null.
+ * @param defaultValue
+ * The default value to return iff value == null.
+ * @return value if value != null else return defaultValue.
+ */
+ public static <T> T valueOrSubstitute(T value, T defaultValue) {
+ if (value != null) return value;
+ return defaultValue;
+ }
+
+ /**
+ * A static factory for Options. Turns something that is either a value
+ * or null into a proper Option with Some(value) or None().
+ *
+ * @param <T> The type of thing to wrap up in an Option.
+ * @param value The value to wrap.
+ * @return Some(value) iff value != null or None() otherwise.
+ */
+ public static <T> Option<T> option(T value) {
+ if (value != null) return some(value);
+ else return none();
+ }
+}
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
new file mode 100644
index 0000000..7b5ec43
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Option.java
@@ -0,0 +1,69 @@
+/******************************************************************************
+ * Copyright (c) David Orme and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * David Orme - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.e4.core.functionalprog.optionmonad;
+
+/**
+ * A Java implementation of the "Option Monad" design pattern. (Note that this
+ * particular implementation isn't actually a full monad, but for our purposes
+ * it doesn't matter.) This is inspired by the following blog:
+ * http://www.codecommit.com/blog/scala/the-option-pattern and also
+ * Functional Java (http://www.functionaljava.org)'s Option implementation.
+ * <p>
+ * The Option pattern provides a unified method for coding functions that could
+ * return Some<T>(value) or also might fail to produce a value, and thus return
+ * None<T>.
+ * <p>
+ * This pattern makes explicit that a method might not return a value, eliminating
+ * guesswork about if the method might return null when reading/learning APIs.
+ *
+ * @param <T> The type the Option encapsulates
+ */
+public interface Option<T> {
+ /**
+ * Return the value inside the Option, or throw an UnsupportedOperationException
+ * if there is no value.
+ *
+ * @return the encapsulated T or throw UnsupportedOperationException if empty
+ */
+ public T get();
+
+ /**
+ * Return the encapsulated instance of T, if there is one, or alternatively
+ * return the defaultValue if there is no encapsulated T.
+ *
+ * @param defaultValue The default value to return if there is no T in the
+ * container.
+ *
+ * @return the encapsulated instance of T, if there is one, or alternatively
+ * return the defaultValue if there is no encapsulated T.
+ */
+ public T getOrSubstitute(T defaultValue);
+
+ /**
+ * Return the encapsulated instance of T, if there is one. If the
+ * container is empty, throw the passed exception.
+ *
+ * @param <E> The type of exception to throw.
+ * @param exception The exception to throw.
+ * @return the encapsulated instance of T, if there is one. If the
+ * container is empty, throw the passed exception.
+ * @throws E The exception type that could be thrown.
+ */
+ public <E extends Throwable> T getOrThrow(E exception) throws E;
+
+ /**
+ * Return true if this Option contains a value or false if it is empty.
+ *
+ * @return true if this Option contains a value or false if it is empty.
+ */
+ public boolean hasValue();
+}
+
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
new file mode 100644
index 0000000..d5ab2d7
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Some.java
@@ -0,0 +1,69 @@
+/******************************************************************************
+ * Copyright (c) David Orme and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * David Orme - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.e4.core.functionalprog.optionmonad;
+
+/**
+ * An Option instance that contains a value.
+ *
+ * @param <T> The type that this Option is encapsulating.
+ */
+public final class Some<T> implements Option<T> {
+ private final T value;
+
+ /**
+ * Construct an Option with Some(value).
+ *
+ * @param value The value to encapsulate.
+ */
+ public Some(T value) {
+ this.value = 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.
+ *
+ * @return a new Some<T> containing the specified value.
+ */
+ public static <T> Option<T> some(T value) { return new Some<T>(value); }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.enterprise.utils.functionalprog.optionmonad.Option#get()
+ */
+ public T get() {
+ return value;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.enterprise.utils.functionalprog.optionmonad.Option#getOrSubstitute(java.lang.Object)
+ */
+ public T getOrSubstitute(T defaultValue) {
+ return value;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.enterprise.utils.functionalprog.optionmonad.Option#getOrThrow(java.lang.Throwable)
+ */
+ public <E extends Throwable> T getOrThrow(E exception) {
+ return value;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.e4.enterprise.utils.functionalprog.optionmonad.Option#hasValue()
+ */
+ public boolean hasValue() {
+ return true;
+ }
+}
+
diff --git a/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/package.html b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/package.html
new file mode 100644
index 0000000..672e342
--- /dev/null
+++ b/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/package.html
@@ -0,0 +1,5 @@
+<p>This package implements the standard operations on an option monad.</p>
+
+<p>Note that this implementation isn't strictly a monad nor does it obey the
+monad laws; however we call it that because of its heritage and because one
+day we might decide to complete the implementation.</p>
\ No newline at end of file