package com.github.j9Stream;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static junit.framework.Assert.assertEquals;

public class StreamTest {

    @Test
    public void dropWhile() throws Exception {
        // arrange
        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4);

        // act
        List<Integer> actual = list.stream().dropWhile(x -> x < 3).collect(Collectors.toList());

        // assert
        assertEquals(2, actual.size());
        assertEquals(3, actual.get(0).intValue());
        assertEquals(4, actual.get(1).intValue());
    }

    @Test
    public void takeWhile() throws Exception {
        // arrange
        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4);

        // act
        List<Integer> actual = list.stream().takeWhile(x -> x < 3).collect(Collectors.toList());

        // assert
        assertEquals(3, actual.size());
        assertEquals(0, actual.get(0).intValue());
        assertEquals(1, actual.get(1).intValue());
        assertEquals(2, actual.get(2).intValue());
    }

    @Test
    public void ofNullable() throws Exception {
        // arrange
        int[] listA = null;
        int[] listB = new int[] { 0, 1 };

        // act
        Optional<int[]> actualA = Stream.ofNullable(listA).findFirst();
        Optional<int[]> actualB = Stream.ofNullable(listB).findFirst();

        // assert
        assertEquals(false, actualA.isPresent());
        assertEquals(true, actualB.isPresent());
        assertEquals(0, actualB.get()[0]);
        assertEquals(1, actualB.get()[1]);
    }
}

@@ -89,20 +89,23 @@
 va.util.
-List
+HashMap
 ;%0Aimport
@@ -115,24 +115,42 @@
 va.util.
-Optional
+List;%0Aimport java.util.Map
 ;%0Aimport
@@ -1263,66 +1263,255 @@
     
-int%5B%5D
+List%3CString%3E
  list
-A
  = 
-null;%0A        int%5B%5D listB = new int%5B%5D %7B 0, 1
+Arrays.asList(%22Angular%22, %22React%22);%0A        Map%3CString, Integer%3E map = new HashMap%3C%3E() %7B%0A            %7B%0A                put(%22Backbone%22, 1);%0A                put(%22Angular%22, 2);%0A                put(%22React%22, 3);%0A            %7D%0A       
  %7D;%0A
@@ -1534,38 +1534,36 @@
         
-Optional%3Cint%5B%5D
+List%3CInteger
 %3E actual
 A = Stre
@@ -1558,120 +1558,101 @@
 tual
-A
  = 
-Stream.ofNullable(listA).findFirst();%0A        Optional%3Cint%5B%5D%3E actualB = Stream.ofNullable(listB).findFir
+list.stream().flatMap(str -%3E Stream.ofNullable(map.get(str))).collect(Collectors.toLi
 st()
+)
 ;%0A%0A 
@@ -1693,21 +1693,17 @@
 als(
-false
+2
 , actual
 A.is
@@ -1702,68 +1702,24 @@
 tual
-A.isPresent());%0A        assertEquals(true, actualB.isPresent
+.get(0).intValue
 ());
@@ -1740,25 +1740,25 @@
 tEquals(
-0
+3
 , actual
 B.get()%5B
@@ -1753,61 +1753,26 @@
 tual
-B
 .get(
-)%5B0%5D);%0A        assertEquals(1, actualB.get()%5B1%5D
+1).intValue()
 );%0A 

fix ofNullable (JDK-8050819)