You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by sm...@apache.org on 2015/07/13 16:40:41 UTC

svn commit: r1690680 - in /pivot/trunk/core: src/org/apache/pivot/functional/monad/ test/org/apache/pivot/functional/ test/org/apache/pivot/functional/monad/ test/org/apache/pivot/functional/monad/test/

Author: smartini
Date: Mon Jul 13 14:40:40 2015
New Revision: 1690680

URL: http://svn.apache.org/r1690680
Log:
PIVOT-799, update Monads (mainly Option-related classes) and add a JUnit test for OptionCompanion

Added:
    pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java
    pivot/trunk/core/test/org/apache/pivot/functional/
    pivot/trunk/core/test/org/apache/pivot/functional/monad/
    pivot/trunk/core/test/org/apache/pivot/functional/monad/test/
    pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java
Modified:
    pivot/trunk/core/src/org/apache/pivot/functional/monad/Monad.java
    pivot/trunk/core/src/org/apache/pivot/functional/monad/None.java
    pivot/trunk/core/src/org/apache/pivot/functional/monad/Option.java
    pivot/trunk/core/src/org/apache/pivot/functional/monad/OptionCompanion.java

Modified: pivot/trunk/core/src/org/apache/pivot/functional/monad/Monad.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/Monad.java?rev=1690680&r1=1690679&r2=1690680&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/Monad.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/Monad.java Mon Jul 13 14:40:40 2015
@@ -19,7 +19,7 @@ package org.apache.pivot.functional.mona
 /**
  * Definition of a generic Monad.
  */
-public abstract class Monad<T> {
+public abstract class Monad<T> implements MonadicInterface<T> {
 
     /** Default constructor */
     protected Monad() {
@@ -31,7 +31,4 @@ public abstract class Monad<T> {
         return "Monad()";
     }
 
-// TODO: check if add other abstract (generic, typical of Monads: apply, unit, map, flatten, etc)
-//       methods here, and see what implement in Option  ...
-
 }

Added: pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java?rev=1690680&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java Mon Jul 13 14:40:40 2015
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.functional.monad;
+
+/**
+ * Definition of generic Monadic methods.
+ */
+@SuppressWarnings("unused")
+public interface MonadicInterface<T> {
+
+// TODO: check if add other abstract (generic, typical of Monads: apply, unit, map, flatten, etc)
+//       methods here (MonadicInterface), and see what to implement in Option and in Try ...
+
+}

Modified: pivot/trunk/core/src/org/apache/pivot/functional/monad/None.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/None.java?rev=1690680&r1=1690679&r2=1690680&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/None.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/None.java Mon Jul 13 14:40:40 2015
@@ -21,7 +21,7 @@ import java.util.NoSuchElementException;
 /**
  * Container for an immutable no-value, derived from Option.
  */
-@SuppressWarnings({"unchecked", "rawtypes"})
+@SuppressWarnings({"rawtypes"})
 public final class None<T> extends Option<T> {
     private static final None INSTANCE = new None<>();
 

Modified: pivot/trunk/core/src/org/apache/pivot/functional/monad/Option.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/Option.java?rev=1690680&r1=1690679&r2=1690680&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/Option.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/Option.java Mon Jul 13 14:40:40 2015
@@ -31,7 +31,6 @@ public abstract class Option<T> extends
 
     /**
      * Constructor with a value to set in the Option
-     *
      * @param val the value to set in the Option
      */
     Option(final T val) {
@@ -51,7 +50,6 @@ public abstract class Option<T> extends
     public abstract T getValue();
 
     /**
-     *
      * Return the value contained in the Option, or an alternative value if not set.
      * @param alternativeValue the value to return as alternative (if value wasn't set in the Option)
      * @return value if set, otherwise alternativeValue

Modified: pivot/trunk/core/src/org/apache/pivot/functional/monad/OptionCompanion.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/OptionCompanion.java?rev=1690680&r1=1690679&r2=1690680&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/OptionCompanion.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/OptionCompanion.java Mon Jul 13 14:40:40 2015
@@ -19,7 +19,7 @@ package org.apache.pivot.functional.mona
 /**
  * Utility class for additional Option methods.
  */
-@SuppressWarnings({"unchecked", "rawtypes"})
+@SuppressWarnings({"rawtypes"})
 public final class OptionCompanion<T> {
     private static final OptionCompanion INSTANCE = new OptionCompanion<>();
 

Added: pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java?rev=1690680&view=auto
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java (added)
+++ pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java Mon Jul 13 14:40:40 2015
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.functional.monad.test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.NoSuchElementException;
+
+import org.apache.pivot.functional.monad.None;
+import org.apache.pivot.functional.monad.Option;
+import org.apache.pivot.functional.monad.OptionCompanion;
+import org.junit.Test;
+
+public class OptionTest {
+    @Test
+    public void companionTest() {
+        OptionCompanion<Object> o = OptionCompanion.getInstance();
+        assertNotNull(o);
+    }
+
+    @Test(expected=NoSuchElementException.class)
+    public void companionNoneTest() {
+        OptionCompanion<Object> o = OptionCompanion.getInstance();
+        assertNotNull(o);
+
+        Option<Object> on = o.fromValue(null);
+        assertNotNull(on);
+        assertTrue(on.hasValue() == false);
+        System.out.println("companionNoneTest(), has value is " + on.hasValue());
+        Object onValue = on.getValue();  // throw Exception here
+        assertTrue(onValue instanceof None);  // never called
+    }
+
+    @Test
+    public void companionObjectTest() {
+        OptionCompanion<Object> o = OptionCompanion.getInstance();
+        assertNotNull(o);
+
+        Option<Object> oo = o.fromValue(new String("Hello"));
+        assertNotNull(oo);
+        assertTrue(oo.hasValue() == true);
+        Object ooValue = oo.getValue();
+        assertTrue(ooValue instanceof String);
+        System.out.println("companionObjectTest(), value stored is " + ooValue);
+    }
+
+    @Test
+    public void companionStringTest() {
+        OptionCompanion<String> o = OptionCompanion.getInstance();
+        assertNotNull(o);
+
+        Option<String> os = o.fromValue("Hello");
+        assertNotNull(os);
+        assertTrue(os.hasValue() == true);
+        Object osValue = os.getValue();
+        assertTrue(osValue instanceof String);
+        System.out.println("companionStringTest(), value stored is " + osValue);
+    }
+
+    @Test
+    public void companionNumberTest() {
+        OptionCompanion<Number> o = OptionCompanion.getInstance();
+        assertNotNull(o);
+
+        Option<Number> on = o.fromValue(new Double(3.14149));
+        assertNotNull(on);
+        assertTrue(on.hasValue() == true);
+        Object onValue = on.getValue();
+        assertTrue(onValue instanceof Number);
+        assertTrue(onValue instanceof Double);
+        System.out.println("companionNumberTest(), value stored is " + onValue);
+    }
+
+}