You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/08/29 13:22:27 UTC

[isis] 02/03: ISIS-1841: Internal API: add utility for streaming of enumerations

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 78056049399b8a32332dbc15c2ca8273df919b8c
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Aug 29 13:54:57 2018 +0200

    ISIS-1841: Internal API: add utility for streaming of enumerations
---
 .../isis/commons/internal/base/_NullSafe.java      | 34 ++++++++++++++++++----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/base/_NullSafe.java b/core/commons/src/main/java/org/apache/isis/commons/internal/base/_NullSafe.java
index ae39a5a..e28d83f 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/base/_NullSafe.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/base/_NullSafe.java
@@ -20,9 +20,12 @@ package org.apache.isis.commons.internal.base;
 
 import java.util.Collection;
 import java.util.Comparator;
+import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Spliterator;
+import java.util.Spliterators;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 
@@ -99,13 +102,34 @@ public final class _NullSafe {
      */
     public static <T> Stream<T> stream(final Iterator<T> iterator){
         return iterator!=null
-                ? StreamSupport.stream(toIterable(iterator).spliterator(), false) //not parallel
+                ? StreamSupport.stream(
+                        Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), 
+                        false) //not parallel
                         : Stream.empty();
     }
-
-    // [ahuber] not public, since one time use of iterator only!
-    private static <T> Iterable<T> toIterable(final Iterator<T> iterator){
-        return ()->iterator;
+    
+    /**
+     * If {@code enumeration} is {@code null} returns the empty stream,
+     * otherwise returns a stream of the enumeration's elements.
+     * @param enumeration
+     * @return non-null stream object
+     */
+    public static <T> Stream<T> stream(final Enumeration<T> enumeration){
+        return enumeration!=null
+                ? stream(toIterator(enumeration))
+                        : Stream.empty();
+    }
+    
+    // [ahuber] not public, since one time use of enumeration only!
+    private static <T> Iterator<T> toIterator(final Enumeration<T> e){
+        return new Iterator<T>() {
+            public T next() {
+                return e.nextElement();
+            }
+            public boolean hasNext() {
+                return e.hasMoreElements();
+            }
+        };
     }