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 2020/07/09 19:37:00 UTC

[isis] 01/02: ISIS-2340: fixes _NullSafe bug

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 0e21e30c50ff2f574202d4fb9591a13e2cb53a1b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Jul 9 21:35:30 2020 +0200

    ISIS-2340: fixes _NullSafe bug
---
 .../isis/core/commons/internal/base/_NullSafe.java | 50 ++++++++++++++---
 .../core/commons/internal/base/NullSafeTest.java   | 65 ++++++++++++++++++----
 2 files changed, 95 insertions(+), 20 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/core/commons/internal/base/_NullSafe.java b/core/commons/src/main/java/org/apache/isis/core/commons/internal/base/_NullSafe.java
index 34819a4..73c0962 100644
--- a/core/commons/src/main/java/org/apache/isis/core/commons/internal/base/_NullSafe.java
+++ b/core/commons/src/main/java/org/apache/isis/core/commons/internal/base/_NullSafe.java
@@ -28,6 +28,7 @@ import java.util.Objects;
 import java.util.Spliterator;
 import java.util.Spliterators;
 import java.util.function.Consumer;
+import java.util.stream.IntStream;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 
@@ -173,14 +174,14 @@ public final class _NullSafe {
         if(pojo.getClass().isArray()) {
             if(Array.getLength(pojo)==0) return Stream.empty(); 
             if(pojo instanceof Object[]) return Stream.of((Object[]) pojo);
-            if(pojo instanceof boolean[]) return Stream.of((boolean[]) pojo);
-            if(pojo instanceof byte[]) return Stream.of((byte[]) pojo);
-            if(pojo instanceof char[]) return Stream.of((char[]) pojo);
-            if(pojo instanceof double[]) return Stream.of((double[]) pojo);
-            if(pojo instanceof float[]) return Stream.of((float[]) pojo);
-            if(pojo instanceof int[]) return Stream.of((int[]) pojo);
-            if(pojo instanceof long[]) return Stream.of((long[]) pojo);
-            if(pojo instanceof short[]) return Stream.of((short[]) pojo);
+            if(pojo instanceof boolean[]) return primitiveStream((boolean[]) pojo);
+            if(pojo instanceof byte[]) return primitiveStream((byte[]) pojo);
+            if(pojo instanceof char[]) return primitiveStream((char[]) pojo);
+            if(pojo instanceof double[]) return primitiveStream((double[]) pojo);
+            if(pojo instanceof float[]) return primitiveStream((float[]) pojo);
+            if(pojo instanceof int[]) return primitiveStream((int[]) pojo);
+            if(pojo instanceof long[]) return primitiveStream((long[]) pojo);
+            if(pojo instanceof short[]) return primitiveStream((short[]) pojo);
         }
         if(pojo instanceof Iterable) {
             return stream((Iterable<?>)pojo);
@@ -190,8 +191,41 @@ public final class _NullSafe {
         }
         return Stream.of(pojo);
     }
+    
+    private static Stream<Boolean> primitiveStream(final boolean[] array) {
+        return IntStream.range(0, array.length).mapToObj(s -> array[s]);
+    }
+    
+    private static Stream<Byte> primitiveStream(final byte[] array) {
+        return IntStream.range(0, array.length).mapToObj(s -> array[s]);
+    }
+    
+    private static Stream<Character> primitiveStream(final char[] array) {
+        return IntStream.range(0, array.length).mapToObj(s -> array[s]);
+    }
+    
+    private static Stream<Float> primitiveStream(final float[] array) {
+        return IntStream.range(0, array.length).mapToObj(s -> array[s]);
+    }
+    
+    private static Stream<Double> primitiveStream(final double[] array) {
+        return IntStream.range(0, array.length).mapToObj(s -> array[s]);
+    }
+    
+    private static Stream<Short> primitiveStream(final short[] array) {
+        return IntStream.range(0, array.length).mapToObj(s -> array[s]);
+    }
+    
+    private static Stream<Integer> primitiveStream(final int[] array) {
+        return IntStream.range(0, array.length).mapToObj(s -> array[s]);
+    }
+    
+    private static Stream<Long> primitiveStream(final long[] array) {
+        return IntStream.range(0, array.length).mapToObj(s -> array[s]);
+    }
 
     // -- ABSENCE/PRESENCE PREDICATES
+    
 
     /**
      * Equivalent to {@link java.util.Objects#nonNull(Object)}.
diff --git a/core/commons/src/test/java/org/apache/isis/core/commons/internal/base/NullSafeTest.java b/core/commons/src/test/java/org/apache/isis/core/commons/internal/base/NullSafeTest.java
index 43d6137..470ac07 100644
--- a/core/commons/src/test/java/org/apache/isis/core/commons/internal/base/NullSafeTest.java
+++ b/core/commons/src/test/java/org/apache/isis/core/commons/internal/base/NullSafeTest.java
@@ -27,41 +27,82 @@ import java.util.List;
 import java.util.stream.Collectors;
 
 import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
-public class NullSafeTest {
+class NullSafeTest {
 
     @Test
-    public void isEmptyString() throws Exception {
+    void streamAutodetect() throws Exception {
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new boolean[] {true, false})
+                .count());
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new byte[] {1, 2})
+                .count());
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new char[] {'1', '2'})
+                .count());
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new short[] {1, 2})
+                .count());
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new float[] {1.f, 2.f})
+                .count());
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new double[] {1., 2.})
+                .count());
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new int[] {1, 2})
+                .count());
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new long[] {1L, 2L})
+                .count());
+        
+        assertEquals(2, 
+                _NullSafe.streamAutodetect(new String[] {"hi", "there"})
+                .count());
+    }
+    
+    @Test
+    void isEmptyString() throws Exception {
         Assert.assertThat(_NullSafe.isEmpty((String)null), is(true));
         Assert.assertThat(_NullSafe.isEmpty(""), is(true));
         Assert.assertThat(_NullSafe.isEmpty(" 12 aBc"), is(false));
     }
 
     @Test
-    public void isEmptyCollection() throws Exception {
+    void isEmptyCollection() throws Exception {
         Assert.assertThat(_NullSafe.isEmpty((Collection<?>)null), is(true));
         Assert.assertThat(_NullSafe.isEmpty(Collections.emptyList()), is(true));
         Assert.assertThat(_NullSafe.isEmpty(Arrays.asList(new String[] {"foo", "bar"})), is(false));
     }
 
     @Test
-    public void absence() throws Exception {
+    void absence() throws Exception {
         Assert.assertThat(_NullSafe.isAbsent(null), is(true));
         Assert.assertThat(_NullSafe.isAbsent(""), is(false));
     }
 
     @Test
-    public void presence() throws Exception {
+    void presence() throws Exception {
         Assert.assertThat(_NullSafe.isPresent(null), is(false));
         Assert.assertThat(_NullSafe.isPresent(""), is(true));
     }
 
 
     @Test
-    public void emptyStreamWithArray() throws Exception {
+    void emptyStreamWithArray() throws Exception {
 
         Assert.assertNotNull(_NullSafe.stream((String[])null));
 
@@ -70,7 +111,7 @@ public class NullSafeTest {
     }
 
     @Test
-    public void streamWithArray() throws Exception {
+    void streamWithArray() throws Exception {
         Assert.assertThat(
                 _NullSafe.stream(new String[] {"foo", "bar"})
                 .collect(Collectors.joining("|")),
@@ -78,7 +119,7 @@ public class NullSafeTest {
     }
 
     @Test
-    public void emptyStreamWithCollection() throws Exception {
+    void emptyStreamWithCollection() throws Exception {
 
         Assert.assertNotNull(_NullSafe.stream((List<?>)null));
 
@@ -87,7 +128,7 @@ public class NullSafeTest {
     }
 
     @Test
-    public void streamWithCollection() throws Exception {
+    void streamWithCollection() throws Exception {
         Assert.assertThat(
                 _NullSafe.stream(Arrays.asList(new String[] {"foo", "bar"}))
                 .collect(Collectors.joining("|")),
@@ -95,7 +136,7 @@ public class NullSafeTest {
     }
 
     @Test
-    public void emptyStreamWithIterator() throws Exception {
+    void emptyStreamWithIterator() throws Exception {
 
         Assert.assertNotNull(_NullSafe.stream((Iterator<?>)null));
 
@@ -104,7 +145,7 @@ public class NullSafeTest {
     }
 
     @Test
-    public void streamWithIterator() throws Exception {
+    void streamWithIterator() throws Exception {
         Assert.assertThat(
                 _NullSafe.stream(Arrays.asList(new String[] {"foo", "bar"}).iterator())
                 .collect(Collectors.joining("|")),