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/14 12:57:05 UTC

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

Author: smartini
Date: Tue Jul 14 10:57:04 2015
New Revision: 1690899

URL: http://svn.apache.org/r1690899
Log:
PIVOT-799, finish initial version of base Monads: Try (Success/Failure) and Option (Some/None) classes, and related JUnit tests

Added:
    pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicOperations.java
      - copied, changed from r1690680, pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java
Removed:
    pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java
Modified:
    pivot/trunk/core/src/org/apache/pivot/functional/monad/Failure.java
    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/Some.java
    pivot/trunk/core/src/org/apache/pivot/functional/monad/Success.java
    pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java
    pivot/trunk/core/test/org/apache/pivot/functional/monad/test/TryTest.java

Modified: pivot/trunk/core/src/org/apache/pivot/functional/monad/Failure.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/Failure.java?rev=1690899&r1=1690898&r2=1690899&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/Failure.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/Failure.java Tue Jul 14 10:57:04 2015
@@ -17,7 +17,7 @@
 package org.apache.pivot.functional.monad;
 
 /**
- * Generic container, to hold the unsuccessful result of a computation (a Throwable).
+ * Generic container, to hold the unsuccessful result of a computation (a RuntimeException).
  */
 public class Failure<T> extends Try<T> {
     final RuntimeException value;
@@ -25,7 +25,7 @@ public class Failure<T> extends Try<T> {
     /**
      * Default constructor, do not use because it set null as invariant value to hold.
      */
-    Failure() {
+    public Failure() {
         value = new IllegalArgumentException("failure exception set to null.");
     }
 
@@ -33,7 +33,7 @@ public class Failure<T> extends Try<T> {
      * Constructor with a value to set
      * @param val the RuntimeException to set
      */
-    Failure(final RuntimeException val) {
+    public Failure(final RuntimeException val) {
         this.value = val;
     }
 

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=1690899&r1=1690898&r2=1690899&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 Tue Jul 14 10:57:04 2015
@@ -19,7 +19,7 @@ package org.apache.pivot.functional.mona
 /**
  * Definition of a generic Monad.
  */
-public abstract class Monad<T> implements MonadicInterface<T> {
+public abstract class Monad<T> implements MonadicOperations<T> {
 
     /** Default constructor */
     protected Monad() {

Copied: pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicOperations.java (from r1690680, 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/MonadicOperations.java?p2=pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicOperations.java&p1=pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java&r1=1690680&r2=1690899&rev=1690899&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicInterface.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/MonadicOperations.java Tue Jul 14 10:57:04 2015
@@ -20,7 +20,7 @@ package org.apache.pivot.functional.mona
  * Definition of generic Monadic methods.
  */
 @SuppressWarnings("unused")
-public interface MonadicInterface<T> {
+public interface MonadicOperations<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=1690899&r1=1690898&r2=1690899&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 Tue Jul 14 10:57:04 2015
@@ -21,7 +21,7 @@ import java.util.NoSuchElementException;
 /**
  * Container for an immutable no-value, derived from Option.
  */
-@SuppressWarnings({"rawtypes"})
+@SuppressWarnings({"unchecked", "rawtypes"})
 public final class None<T> extends Option<T> {
     private static final None INSTANCE = new None<>();
 
@@ -36,7 +36,7 @@ public final class None<T> extends Optio
     /**
      * Default constructor, does nothing.
      */
-    None() {
+    public None() {
         // super(null);
         // no-op
     }

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=1690899&r1=1690898&r2=1690899&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 Tue Jul 14 10:57:04 2015
@@ -25,7 +25,7 @@ public abstract class Option<T> extends
     /**
      * Default constructor, do not use because it set null as invariant value to hold.
      */
-    Option() {
+    public Option() {
         this(null);
     }
 
@@ -33,7 +33,7 @@ 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) {
+    public Option(final T val) {
         this.value = val;
     }
 

Modified: pivot/trunk/core/src/org/apache/pivot/functional/monad/Some.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/Some.java?rev=1690899&r1=1690898&r2=1690899&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/Some.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/Some.java Tue Jul 14 10:57:04 2015
@@ -25,7 +25,7 @@ public final class Some<T> extends Optio
      * Default constructor, do not use here or an IllegalArgumentException will
      * be thrown.
      */
-    Some() {
+    public Some() {
         throw new IllegalArgumentException("Some must have a value in the constructor");
     }
 
@@ -34,7 +34,7 @@ public final class Some<T> extends Optio
      *
      * @param val the value to set
      */
-    Some(T val) {
+    public Some(T val) {
         super(val);
     }
 

Modified: pivot/trunk/core/src/org/apache/pivot/functional/monad/Success.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/Success.java?rev=1690899&r1=1690898&r2=1690899&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/Success.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/Success.java Tue Jul 14 10:57:04 2015
@@ -25,7 +25,7 @@ public class Success<T> extends Try<T> {
     /**
      * Default constructor, do not use because it set null as invariant value to hold.
      */
-    Success() {
+    public Success() {
         throw new IllegalArgumentException("Success must have a value in the constructor");
     }
 
@@ -33,7 +33,7 @@ public class Success<T> extends Try<T> {
      * Constructor with a value to set in the Option
      * @param val the value to set
      */
-    Success(final T val) {
+    public Success(final T val) {
         this.value = val;
     }
 

Modified: 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=1690899&r1=1690898&r2=1690899&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java Tue Jul 14 10:57:04 2015
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertTrue;
 
 import java.util.NoSuchElementException;
+import java.util.Random;
 
 import org.apache.pivot.functional.monad.None;
 import org.apache.pivot.functional.monad.Option;
@@ -91,4 +92,110 @@ public class OptionTest {
         System.out.println("companionNumberTest(), value stored is " + onValue);
     }
 
+    @Test
+    public void companionRealUsageRandomTest() {
+        OptionCompanion<String> o = OptionCompanion.getInstance();
+        assertNotNull(o);
+
+        Option<String> os = null;
+        String value;
+
+        // randomizing this test
+        Random randomGenerator = new Random();
+        int randomInt = randomGenerator.nextInt(100);
+
+        // store the value in the Option instance (Some if not null, otherwise None)
+        // note that try/catch block here are unnecessary, but probably near to a real-world usage
+        try {
+            // randomizing this test:
+            // for even numbers a value will be generated,
+            // but for odd numbers the value will be null so a call on it will throw a RuntimeException
+            if (randomInt % 2 == 0) {
+                value = String.valueOf(randomInt);
+            } else {
+                value = null;
+            }
+
+            os = o.fromValue(value);
+        } catch (RuntimeException e) {
+            System.err.println("companionRealUsageRandomTest(), got RuntimeException " + e);
+            os = o.fromValue(null);
+        }
+
+        // verify the value stored
+        System.out.println("companionRealUsageRandomTest(), stored element has a value " + os.hasValue());
+        try {
+            String tsValue;  // = os.getValue();  // this will throw a RuntimeException if os is a None
+            // System.out.println("companionRealUsageRandomTest(), value stored is " + tsValue);
+
+            if (randomInt % 2 == 0) {
+                assertTrue(os instanceof Some);
+                assertTrue(os.hasValue() == true);
+                tsValue = os.getValue();
+                System.out.println("companionRealUsageRandomTest(), value stored is " + tsValue);
+                assertTrue(tsValue != null);
+            } else {
+                assertTrue(os instanceof None);
+                assertTrue(os.hasValue() == false);
+                tsValue = os.getValue();  // will throw a RuntimeException when called in the case
+                assertTrue(tsValue == null);  // never called
+            }
+
+        } catch (RuntimeException e) {
+            System.err.println("companionRealUsageRandomTest(), got RuntimeException " + e);
+            assertTrue(os.hasValue() == false);
+        }
+    }
+
+    @Test
+    public void optionSomeTest() {
+        OptionCompanion<String> o = OptionCompanion.getInstance();
+        assertNotNull(o);
+
+        // sample by direct instancing of Some/None classes, but discouraged
+
+        Option<String> os = null;
+        String tsValue = null;
+
+        // store the value in the Option instance (Some if not null, otherwise None)
+        os = new Some<>("Optional value");
+        assertTrue(os != null);
+
+        // verify the value stored
+        System.out.println("optionSomeTest(), stored element has a value " + os.hasValue());
+        assertTrue(os instanceof Some);
+        assertTrue(os.hasValue() == true);
+        tsValue = os.getValue();
+        System.out.println("optionSomeTest(), value stored is " + tsValue);
+        assertTrue(tsValue != null);
+    }
+
+    @Test
+    public void optionNoneTest() {
+        OptionCompanion<String> o = OptionCompanion.getInstance();
+        assertNotNull(o);
+
+        // sample by direct instancing of Some/None classes, but discouraged
+
+        Option<String> os = null;
+        String tsValue = null;
+
+        // store the value in the Option instance (Some if not null, otherwise None)
+        // os = new None<>();  // discouraged
+        os = None.getInstance();  // better
+        assertTrue(os != null);
+
+        // verify the value stored
+        System.out.println("optionNoneTest(), stored element has a value " + os.hasValue());
+        assertTrue(os instanceof None);
+        assertTrue(os.hasValue() == false);
+        try {
+            tsValue = os.getValue();  // will throw a RuntimeException when called in the case
+            assertTrue(tsValue == null);  // never called
+        } catch (RuntimeException e) {
+            System.err.println("optionNoneTest(), got RuntimeException " + e);
+            assertTrue(os.hasValue() == false);
+        }
+    }
+
 }

Modified: pivot/trunk/core/test/org/apache/pivot/functional/monad/test/TryTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/functional/monad/test/TryTest.java?rev=1690899&r1=1690898&r2=1690899&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/functional/monad/test/TryTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/functional/monad/test/TryTest.java Tue Jul 14 10:57:04 2015
@@ -19,6 +19,8 @@ package org.apache.pivot.functional.mona
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Random;
+
 import org.apache.pivot.functional.monad.Failure;
 import org.apache.pivot.functional.monad.Success;
 import org.apache.pivot.functional.monad.Try;
@@ -73,7 +75,7 @@ public class TryTest {
         assertTrue(ts.isSuccess() == true);
         String tsValue = ts.getValue();
         assertTrue(tsValue != null);
-        // assertTrue(tsValue instanceof String);
+        // assertTrue(tsValue instanceof String);  // unnecessary
         assertTrue(tsValue.equals("Hello"));
         System.out.println("companionStringTest(), value stored is " + tsValue);
     }
@@ -83,17 +85,122 @@ public class TryTest {
         TryCompanion<String> t = TryCompanion.getInstance();
         assertNotNull(t);
 
-        RuntimeException re = new IllegalArgumentException("Illegal argument");
+        RuntimeException re = new IllegalArgumentException("Sample RuntimeException");
         Try<String> tf = t.fromValue(re);
         assertNotNull(tf);
         assertTrue(tf instanceof Failure);
         assertTrue(tf.isSuccess() == false);
-// TODO: fix later ...
-//        Exception tfValue = tf.getValue();
-//        assertTrue(tfValue != null);
-//        // assertTrue(tfValue instanceof Throwable);
-//        // assertTrue(tfValue instanceof Exception);
-//        System.out.println("companionStringTest(), value stored is " + tfValue);
+        try {
+            String tfValue = tf.getValue();  // this will throw the RuntimeException stored inside the Failure
+            assertTrue(tfValue != null);  // never called
+        } catch (RuntimeException e) {
+            System.err.println("companionFailureTest(), value stored is " + e);
+            assertTrue(e != null);  // unnecessary
+        }
+    }
+
+    @Test
+    public void companionRealUsageRandomTest() {
+        TryCompanion<String> t = TryCompanion.getInstance();
+        assertNotNull(t);
+
+        Try<String> ts = null;
+        String value;
+
+        // randomizing this test
+        Random randomGenerator = new Random();
+        int randomInt = randomGenerator.nextInt(100);
+
+        // store the value (or the RuntimeException) in the Try instance
+        try {
+            // randomizing this test:
+            // for even numbers a value will be generated,
+            // but for odd numbers the value will be null so a call on it will throw a RuntimeException
+            if (randomInt % 2 == 0) {
+                value = String.valueOf(randomInt);
+            } else {
+                value = null;
+                throw new NullPointerException("Sample RuntimeException");  // simulate an exception here
+            }
+
+            ts = t.fromValue(value);
+        } catch (RuntimeException e) {
+            ts = t.fromValue(e);
+        }
+
+        // verify the value stored
+        System.out.println("companionRealUsageRandomTest(), value stored is a success " + ts.isSuccess());
+        try {
+            String tsValue = ts.getValue();  // this will throw the RuntimeException stored inside the Failure
+            assertTrue(tsValue != null);  // called only when a real value is stored (and not a RuntimeException)
+            System.out.println("companionRealUsageRandomTest(), value stored is " + tsValue);
+            assertTrue(ts.isSuccess() == true);
+        } catch (RuntimeException e) {
+            System.err.println("companionRealUsageRandomTest(), exception stored is " + e);
+            assertTrue(ts.isSuccess() == false);
+        }
+    }
+
+    @Test
+    public void trySuccessTest() {
+        TryCompanion<String> t = TryCompanion.getInstance();
+        assertNotNull(t);
+
+        // sample by direct instancing of Success/Failure classes, but discouraged
+
+        Try<String> ts = null;
+
+        // store the value (or the RuntimeException) in the Try instance
+        try {
+            ts = new Success<>("Hello with Success");
+        } catch (RuntimeException e) {
+            // ts = new Success<>(e);  // compile error, ok
+            ts = new Failure<>(e);
+            assertTrue(e != null);  // unnecessary
+        }
+
+        // verify the value stored
+        System.out.println("trySuccessTest(), value stored is a success " + ts.isSuccess());
+        try {
+            String tsValue = ts.getValue();  // this will throw the RuntimeException stored inside the Failure
+            assertTrue(tsValue != null);  // called only when a real value is stored (and not a RuntimeException)
+            System.out.println("trySuccessTest(), value stored is " + tsValue);
+            assertTrue(ts.isSuccess() == true);
+        } catch (RuntimeException e) {
+            System.err.println("trySuccessTest(), exception stored is " + e);
+            assertTrue(ts.isSuccess() == false);
+        }
+    }
+
+    @Test
+    public void tryFailureTest() {
+        TryCompanion<String> t = TryCompanion.getInstance();
+        assertNotNull(t);
+
+        // sample by direct instancing of Success/Failure classes, but discouraged
+
+        Try<String> ts = null;
+
+        // store the value (or the RuntimeException) in the Try instance
+        try {
+            throw new NullPointerException("Sample RuntimeException");  // simulate an exception here
+        } catch (RuntimeException e) {
+            // ts = new Success<>(e);  // compile error, ok
+            ts = new Failure<>(e);
+            assertTrue(e != null);  // unnecessary
+        }
+
+        // verify the value stored
+        System.out.println("tryFailureTest(), value stored is a success " + ts.isSuccess());
+        try {
+            String tsValue = ts.getValue();  // this will throw the RuntimeException stored inside the Failure
+            assertTrue(tsValue != null);  // called only when a real value is stored (and not a RuntimeException)
+            System.out.println("tryFailureTest(), value stored is " + tsValue);
+            assertTrue(ts.isSuccess() == true);
+        } catch (RuntimeException e) {
+            System.err.println("tryFailureTest(), exception stored is " + e);
+            assertTrue(ts.isSuccess() == false);
+        }
     }
 
 }