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 14:53:31 UTC

svn commit: r1691554 - in /pivot/trunk/core: src/org/apache/pivot/functional/monad/Option.java test/org/apache/pivot/functional/monad/test/OptionTest.java

Author: smartini
Date: Fri Jul 17 12:53:30 2015
New Revision: 1691554

URL: http://svn.apache.org/r1691554
Log:
PIVOT-799, make Option Iterable, and related tests

Modified:
    pivot/trunk/core/src/org/apache/pivot/functional/monad/Option.java
    pivot/trunk/core/test/org/apache/pivot/functional/monad/test/OptionTest.java

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=1691554&r1=1691553&r2=1691554&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 Fri Jul 17 12:53:30 2015
@@ -16,10 +16,12 @@
  */
 package org.apache.pivot.functional.monad;
 
+import java.util.Iterator;
+
 /**
  * Definition of a generic Option container, to hold an invariant value (derived from Monad).
  */
-public abstract class Option<T> extends Monad<T> {
+public abstract class Option<T> extends Monad<T> implements Iterable<T> {
     final T value;
 
     /**
@@ -102,4 +104,37 @@ public abstract class Option<T> extends
         return true;
     }
 
+    /**
+     * Return an Iterator
+     * @see java.lang.Iterable#iterator()
+     */
+    @Override
+    public Iterator<T> iterator() {
+        return new MonadIterator();
+    }
+
+    
+    /**
+     * Immutable iterator on the value contained in the Option (if any).
+     */
+    private class MonadIterator implements Iterator<T> {
+        private int cursor = 0;
+
+        @Override
+        public boolean hasNext() {
+            return (hasValue() && cursor == 0);
+        }
+
+        @Override
+        public T next() {
+            cursor++;
+            return getValue();
+        }
+
+        @Override
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+
 }

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=1691554&r1=1691553&r2=1691554&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 Fri Jul 17 12:53:30 2015
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Iterator;
 import java.util.NoSuchElementException;
 import java.util.Random;
 
@@ -152,7 +153,7 @@ public class OptionTest {
     public void optionSomeTest() {
         // sample by direct instancing of Some/None classes, but discouraged
         Option<String> os = null;
-        String tsValue = null;
+        String osValue = null;
 
         // store the value in the Option instance (Some if not null, otherwise None)
         os = new Some<>("Optional value");
@@ -163,14 +164,14 @@ public class OptionTest {
         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);
+        osValue = os.getValue();
+        System.out.println("optionSomeTest(), value stored is " + osValue);
+        assertTrue(osValue != null);
         // test with alternative value
-        tsValue = os.getValueOrElse("Alternative value");
-        assertEquals("Optional value", tsValue);
-        tsValue = os.getValueOrNull();
-        assertEquals("Optional value", tsValue);
+        osValue = os.getValueOrElse("Alternative value");
+        assertEquals("Optional value", osValue);
+        osValue = os.getValueOrNull();
+        assertEquals("Optional value", osValue);
     }
 
     @Test
@@ -229,4 +230,66 @@ public class OptionTest {
         assertTrue(on1 == on2);
     }
 
+    @Test
+    public void optionSomeIteratorTest() {
+        // sample by direct instancing of Some/None classes, but discouraged
+        Option<String> os = new Some<>("Optional value");
+        System.out.println("optionSomeIteratorTest(), instance variable is " + os);
+
+        // iterate and verify on the value stored
+        Iterator<String> it = os.iterator();
+        assertNotNull(it);
+        int i = 0;
+        while (it.hasNext()) {
+            String value = it.next();
+            System.out.println("optionSomeIteratorTest(), value " + i + " from iterator is " + value);
+            assertNotNull(value);
+            assertEquals("Optional value", value);
+            i++;
+        }
+        assertEquals(i, 1);
+
+        // another test
+        i = 0;
+        System.out.println("optionSomeIteratorTest(), another test");
+        for (String value : os) {
+            System.out.println("optionSomeIteratorTest(), value " + i + " from iterator is " + value);
+            assertNotNull(value);
+            assertEquals("Optional value", value);
+            i++;
+        }
+        assertEquals(i, 1);
+    }
+
+    @Test
+    public void optionNoneIteratorTest() {
+        // sample by direct instancing of Some/None classes, but discouraged
+        None<String> on = None.getInstance();
+        System.out.println("optionNoneIteratorTest(), instance variable is " + on);
+
+        // iterate and verify on the value stored
+        Iterator<String> it = on.iterator();
+        assertNotNull(it);
+        int i = 0;
+        while (it.hasNext()) {
+            // never executed in this case
+            String value = it.next();
+            System.out.println("optionNoneIteratorTest(), value " + i + " from iterator is " + value);
+            assertEquals(null, value);
+            i++;
+        }
+        assertEquals(i, 0);
+
+        // another test
+        i = 0;
+        System.out.println("optionNoneIteratorTest(), another test");
+        for (String value : on) {
+            // never executed in this case
+            System.out.println("optionNoneIteratorTest(), value " + i + " from iterator is " + value);
+            assertEquals(null, value);
+            i++;
+        }
+        assertEquals(i, 0);
+    }
+
 }