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/17 16:45:14 UTC

svn commit: r1691580 - in /pivot/trunk/core: src/org/apache/pivot/functional/monad/OptionCompanion.java src/org/apache/pivot/functional/monad/TryCompanion.java test/org/apache/pivot/functional/monad/test/TryTest.java

Author: smartini
Date: Fri Jul 17 14:45:14 2015
New Revision: 1691580

URL: http://svn.apache.org/r1691580
Log:
PIVOT-799, better alignment with Java 7 not null check for method arguments, a little cleanup, and some small enhancements and related tests

Modified:
    pivot/trunk/core/src/org/apache/pivot/functional/monad/OptionCompanion.java
    pivot/trunk/core/src/org/apache/pivot/functional/monad/TryCompanion.java
    pivot/trunk/core/test/org/apache/pivot/functional/monad/test/TryTest.java

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=1691580&r1=1691579&r2=1691580&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 Fri Jul 17 14:45:14 2015
@@ -16,6 +16,8 @@
  */
 package org.apache.pivot.functional.monad;
 
+import java.util.Objects;
+
 /**
  * Utility class for additional Option methods.
  */
@@ -68,9 +70,7 @@ public final class OptionCompanion<T> {
      * @return the value if any, or null
      */
     public T toValue(final Option<T> o) {
-        if (o == null) {
-            throw new IllegalArgumentException("option is null.");
-        }
+        Objects.requireNonNull(o);
 
         if (!o.hasValue()) {
             return null;
@@ -87,9 +87,7 @@ public final class OptionCompanion<T> {
      * @return value if set, otherwise alternativeValue
      */
     public T toValueOrElse(final Option<T> o, final T alternativeValue) {
-        if (o == null) {
-            throw new IllegalArgumentException("option is null.");
-        }
+        Objects.requireNonNull(o);
 
         return o.getValueOrElse(alternativeValue);
     }
@@ -100,9 +98,7 @@ public final class OptionCompanion<T> {
      * @return true if it contains at least one nested Option, otherwise false
      */
     public boolean hasNestedOptions(final Option<T> o) {
-        if (o == null) {
-            throw new IllegalArgumentException("option is null.");
-        }
+        Objects.requireNonNull(o);
 
         if (o instanceof None) {
             return false;
@@ -111,62 +107,4 @@ public final class OptionCompanion<T> {
         return o.getValue() instanceof Option;
     }
 
-/*
-// TODO: temp ...
-    // flatten the given Option, and return a transformed copy of it
-    public Option<T> flatten(final Option<T> o) {
-        if (o == null) {
-            throw new IllegalArgumentException("option is null.");
-        }
-
-        T val = o.getValue();
-        Option child;
-        boolean valueIsOption = isOption(val);
-        if (!isOption(val))
-            return fromValue(val);
-        // else ...
-        child = (Option) val;
-
-int i = 0  // temp
-        while (isOption(child) && !child.isNone()) {
-            val = child.getValue();
-            if (!isOption(val))
-                return fromValue(val);
-            // else ...
-            child = (Option) val;
-
-// temp, to avoid infinite loops with wrong implementation here (during development) ...
-if (i > maxDepth) {
-    System.err.println("Force loop exiting, infinite loop found ...")
-    break
-    }
-i++
-// temp
-        }
-
-        val = child.getValue();
-        valueIsOption = isOption(val);
-        if (valueIsOption) {
-            return (Option) val;
-        }
-        return fromValue(val);
-    }
-
-    public Option<T> fromValueFlatten(final T val) {
-        if (!isOption(val)) {
-            return fromValue(val);
-        }
-        return flatten((Option) val);
-    }
-
-    public T toValueFlatten(final Option<T> o) {
-        if (o == null) {
-            throw new IllegalArgumentException("option is null.");
-        }
-
-        Option flat = flatten(o);
-        return flat.getValue();
-    }
- */
-
 }

Modified: pivot/trunk/core/src/org/apache/pivot/functional/monad/TryCompanion.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/functional/monad/TryCompanion.java?rev=1691580&r1=1691579&r2=1691580&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/functional/monad/TryCompanion.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/functional/monad/TryCompanion.java Fri Jul 17 14:45:14 2015
@@ -16,6 +16,8 @@
  */
 package org.apache.pivot.functional.monad;
 
+import java.util.Objects;
+
 /**
  * Utility class for additional Try methods.
  */
@@ -74,11 +76,48 @@ public final class TryCompanion<T> {
      * @return the Success instance containing the value, or the Failure instance containing the exception
      */
     public T toValue(final Try<T> t) {
-        if (t == null) {
-            throw new IllegalArgumentException("try is null.");
+        Objects.requireNonNull(t);
+        return t.getValue();
+    }
+
+    /**
+     * Utility method to return the value contained,
+     * or an alternate value if not present.
+     * @param t the Try
+     * @param alternativeValue the alternative value (null could be used here)
+     * @return value if set, otherwise alternativeValue
+     */
+    public T toValueOrElse(final Try<T> t, final T alternativeValue) {
+        Objects.requireNonNull(t);
+        return t.getValueOrElse(alternativeValue);
+    }
+
+    /**
+     * Utility method that tell if the given Try has nested Try instances.
+     * @param t the Try to test
+     * @return true if it contains at least one nested Try, otherwise false
+     */
+    public boolean hasNestedOptions(final Try<T> t) {
+        Objects.requireNonNull(t);
+
+        if (t instanceof Failure) {
+            return false;
         }
 
-        return t.getValue();
+        return t.getValue() instanceof Try;
     }
 
+    /**
+     * Utility method to return the value contained in the Try, in a Option instance
+     * @param t the Try
+     * @return an Option instance: Some(value) if success, None otherwise
+     */
+    @SuppressWarnings("unchecked")
+    public Option<T> toOption(final Try<T> t) {
+        Objects.requireNonNull(t);
+        if (t.isSuccess()) {
+            return (Option<T>) OptionCompanion.getInstance().fromValue(t.getValue());
+        }
+        return None.getInstance();
+    }
 }

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=1691580&r1=1691579&r2=1691580&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 Fri Jul 17 14:45:14 2015
@@ -24,6 +24,9 @@ import java.util.Iterator;
 import java.util.Random;
 
 import org.apache.pivot.functional.monad.Failure;
+import org.apache.pivot.functional.monad.None;
+import org.apache.pivot.functional.monad.Option;
+import org.apache.pivot.functional.monad.Some;
 import org.apache.pivot.functional.monad.Success;
 import org.apache.pivot.functional.monad.Try;
 import org.apache.pivot.functional.monad.TryCompanion;
@@ -210,10 +213,10 @@ public class TryTest {
     }
 
     @Test
-    public void optionTryIteratorTest() {
+    public void trySuccessIteratorTest() {
         // sample by direct instancing of Success/Failure classes, but discouraged
         Try<String> ts = new Success<>("Hello with Success");
-        System.out.println("optionTryIteratorTest(), instance variable is " + ts);
+        System.out.println("trySuccessIteratorTest(), instance variable is " + ts);
 
         // iterate and verify on the value stored
         Iterator<String> it = ts.iterator();
@@ -221,7 +224,7 @@ public class TryTest {
         int i = 0;
         while (it.hasNext()) {
             String value = it.next();
-            System.out.println("optionTryIteratorTest(), value " + i + " from iterator is " + value);
+            System.out.println("trySuccessIteratorTest(), value " + i + " from iterator is " + value);
             assertNotNull(value);
             assertEquals("Hello with Success", value);
             i++;
@@ -230,9 +233,9 @@ public class TryTest {
 
         // another test
         i = 0;
-        System.out.println("optionTryIteratorTest(), another test");
+        System.out.println("trySuccessIteratorTest(), another test");
         for (String value : ts) {
-            System.out.println("optionTryIteratorTest(), value " + i + " from iterator is " + value);
+            System.out.println("trySuccessIteratorTest(), value " + i + " from iterator is " + value);
             assertNotNull(value);
             assertEquals("Hello with Success", value);
             i++;
@@ -241,7 +244,7 @@ public class TryTest {
     }
 
     @Test
-    public void optionFailureIteratorTest() {
+    public void tryFailureIteratorTest() {
         // sample by direct instancing of Success/Failure classes, but discouraged
         Try<String> tf = null;
 
@@ -251,7 +254,7 @@ public class TryTest {
         } catch (RuntimeException e) {
             tf = new Failure<>(e);
         }
-        System.out.println("optionFailureIteratorTest(), instance variable is " + tf);
+        System.out.println("tryFailureIteratorTest(), instance variable is " + tf);
 
         // iterate and verify on the value stored
         Iterator<String> it = tf.iterator();
@@ -260,7 +263,7 @@ public class TryTest {
         while (it.hasNext()) {
             // never executed in this case
             String value = it.next();
-            System.out.println("optionFailureIteratorTest(), value " + i + " from iterator is " + value);
+            System.out.println("tryFailureIteratorTest(), value " + i + " from iterator is " + value);
             assertEquals(null, value);
             i++;
         }
@@ -268,14 +271,70 @@ public class TryTest {
 
         // another test
         i = 0;
-        System.out.println("optionFailureIteratorTest(), another test");
+        System.out.println("tryFailureIteratorTest(), another test");
         for (String value : tf) {
             // never executed in this case
-            System.out.println("optionFailureIteratorTest(), value " + i + " from iterator is " + value);
+            System.out.println("tryFailureIteratorTest(), value " + i + " from iterator is " + value);
             assertEquals(null, value);
             i++;
         }
         assertEquals(i, 0);
     }
 
+    @Test
+    public void companionSuccessToOptionTest() {
+        TryCompanion<String> t = TryCompanion.getInstance();
+        assertNotNull(t);
+
+        Try<String> ts = t.fromValue("Hello");
+        Option<String> os = t.toOption(ts);
+
+        // verify the value stored
+        System.out.println("companionSuccessToOptionTest(), Try instance is " + ts);
+        System.out.println("companionSuccessToOptionTest(), Option instance is " + os);
+
+        assertNotNull(ts);
+        assertTrue(ts instanceof Success);
+        assertTrue(ts.isSuccess() == true);
+
+        assertNotNull(os);
+        assertTrue(os instanceof Some);
+        assertTrue(os.hasValue() == true);
+
+        String osValue = os.getValue();
+        assertTrue(osValue != null);
+        assertTrue(osValue.equals("Hello"));
+        System.out.println("companionSuccessToOptionTest(), value stored is " + osValue);
+    }
+
+    @Test
+    public void companionFailureToOptionTest() {
+        TryCompanion<String> t = TryCompanion.getInstance();
+        assertNotNull(t);
+
+        RuntimeException re = new IllegalArgumentException("Sample RuntimeException");
+        Try<String> tf = t.fromValue(re);
+        Option<String> on = t.toOption(tf);
+
+        // verify the value stored
+        System.out.println("companionFailureToOptionTest(), Try instance is " + tf);
+        System.out.println("companionFailureToOptionTest(), Option instance is " + on);
+
+        assertNotNull(tf);
+        assertTrue(tf instanceof Failure);
+        assertTrue(tf.isSuccess() == false);
+
+        assertNotNull(on);
+        assertTrue(on instanceof None);
+        assertTrue(on.hasValue() == false);
+
+        try {
+            String onValue = on.getValue();  // this will throw a RuntimeException for the non-value of None
+            assertTrue(onValue != null);  // never called
+        } catch (RuntimeException e) {
+            System.err.println("companionFailureToOptionTest(), got RuntimeException " + e);
+        }
+
+    }
+
 }