jdk/test/java/util/Optional/Basic.java
author psandoz
Fri, 13 Feb 2015 11:13:27 +0100
changeset 28963 8498cdb7c54b
parent 28754 8fe59917548d
child 33238 80720cbea36f
permissions -rw-r--r--
8071670: java.util.Optional: please add a way to specify if-else behavior Reviewed-by: dfuchs, lowasser
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     1
/*
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     4
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     8
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    13
 * accompanied this code).
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    14
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    18
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    21
 * questions.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    22
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    23
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    24
/* @test
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    25
 * @summary Basic functional test of Optional
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    26
 * @author Mike Duigou
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    27
 * @run testng Basic
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    28
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    29
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    30
import java.lang.AssertionError;
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    31
import java.lang.NullPointerException;
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    32
import java.lang.Throwable;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    33
import java.util.NoSuchElementException;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    34
import java.util.Optional;
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    35
import java.util.concurrent.atomic.AtomicBoolean;
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
    36
import java.util.stream.Stream;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    37
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    38
import static org.testng.Assert.*;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    39
import org.testng.annotations.Test;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    40
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    41
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    42
public class Basic {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    43
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    44
    @Test(groups = "unit")
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    45
    public void testEmpty() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    46
        Optional<Boolean> empty = Optional.empty();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    47
        Optional<String> presentEmptyString = Optional.of("");
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    48
        Optional<Boolean> present = Optional.of(Boolean.TRUE);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    49
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    50
        // empty
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    51
        assertTrue(empty.equals(empty));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    52
        assertTrue(empty.equals(Optional.empty()));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    53
        assertTrue(!empty.equals(present));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    54
        assertTrue(0 == empty.hashCode());
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    55
        assertTrue(!empty.toString().isEmpty());
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    56
        assertTrue(!empty.toString().equals(presentEmptyString.toString()));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    57
        assertTrue(!empty.isPresent());
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    58
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    59
        empty.ifPresent(v -> fail());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    60
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    61
        AtomicBoolean emptyCheck = new AtomicBoolean();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    62
        empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    63
        assertTrue(emptyCheck.get());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    64
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    65
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    66
            empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    67
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    68
        } catch (ObscureException expected) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    69
        } catch (AssertionError e) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    70
            throw e;
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    71
        } catch (Throwable t) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    72
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    73
        }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    74
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    75
        assertSame(null, empty.orElse(null));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    76
        RuntimeException orElse = new RuntimeException() { };
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    77
        assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
    78
        assertSame(null, empty.orElseGet(() -> null));
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
    79
        assertSame(Boolean.FALSE, empty.orElseGet(() -> Boolean.FALSE));
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    80
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    81
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    82
    @Test(groups = "unit")
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    83
    public void testIfPresentAndOrElseAndNull() {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    84
        Optional<Boolean> empty = Optional.empty();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    85
        Optional<Boolean> present = Optional.of(Boolean.TRUE);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    86
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    87
        // No NPE
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    88
        present.ifPresentOrElse(v -> {}, null);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    89
        empty.ifPresent(null);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    90
        empty.ifPresentOrElse(null, () -> {});
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    91
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    92
        // NPE
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    93
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    94
            present.ifPresent(null);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    95
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    96
        } catch (NullPointerException ex) {}
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    97
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    98
            present.ifPresentOrElse(null, () -> {});
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    99
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   100
        } catch (NullPointerException ex) {}
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   101
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   102
            empty.ifPresentOrElse(v -> {}, null);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   103
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   104
        } catch (NullPointerException ex) {}
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   105
    }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   106
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   107
    @Test(expectedExceptions=NoSuchElementException.class)
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   108
    public void testEmptyGet() {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   109
        Optional<Boolean> empty = Optional.empty();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   110
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   111
        Boolean got = empty.get();
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   112
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   113
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   114
    @Test(expectedExceptions=NullPointerException.class)
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   115
    public void testEmptyOrElseGetNull() {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   116
        Optional<Boolean> empty = Optional.empty();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   117
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   118
        Boolean got = empty.orElseGet(null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   119
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   120
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   121
    @Test(expectedExceptions=NullPointerException.class)
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   122
    public void testEmptyOrElseThrowNull() throws Throwable {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   123
        Optional<Boolean> empty = Optional.empty();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   124
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   125
        Boolean got = empty.orElseThrow(null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   126
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   127
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   128
    @Test(expectedExceptions=ObscureException.class)
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   129
    public void testEmptyOrElseThrow() throws Exception {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   130
        Optional<Boolean> empty = Optional.empty();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   131
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   132
        Boolean got = empty.orElseThrow(ObscureException::new);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   133
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   134
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   135
    @Test(groups = "unit")
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   136
    public void testPresent() {
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   137
        Optional<Boolean> empty = Optional.empty();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   138
        Optional<String> presentEmptyString = Optional.of("");
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   139
        Optional<Boolean> present = Optional.of(Boolean.TRUE);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   140
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   141
        // present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   142
        assertTrue(present.equals(present));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   143
        assertTrue(present.equals(Optional.of(Boolean.TRUE)));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   144
        assertTrue(!present.equals(empty));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   145
        assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   146
        assertTrue(!present.toString().isEmpty());
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   147
        assertTrue(!present.toString().equals(presentEmptyString.toString()));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   148
        assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   149
        assertSame(Boolean.TRUE, present.get());
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   150
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   151
        AtomicBoolean presentCheck = new AtomicBoolean();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   152
        present.ifPresent(v -> presentCheck.set(true));
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   153
        assertTrue(presentCheck.get());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   154
        presentCheck.set(false);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   155
        present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   156
        assertTrue(presentCheck.get());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   157
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   158
        try {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   159
            present.ifPresent(v -> { throw new ObscureException(); });
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   160
            fail();
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   161
        } catch (ObscureException expected) {
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   162
        } catch (AssertionError e) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   163
            throw e;
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   164
        } catch (Throwable t) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   165
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   166
        }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   167
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   168
            present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   169
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   170
        } catch (ObscureException expected) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   171
        } catch (AssertionError e) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   172
            throw e;
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   173
        } catch (Throwable t) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   174
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   175
        }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   176
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   177
        assertSame(Boolean.TRUE, present.orElse(null));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   178
        assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   179
        assertSame(Boolean.TRUE, present.orElseGet(null));
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   180
        assertSame(Boolean.TRUE, present.orElseGet(() -> null));
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   181
        assertSame(Boolean.TRUE, present.orElseGet(() -> Boolean.FALSE));
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   182
        assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(null));
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   183
        assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   184
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   185
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   186
    @Test(groups = "unit")
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   187
    public void testOfNullable() {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   188
        Optional<String> instance = Optional.ofNullable(null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   189
        assertFalse(instance.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   190
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   191
        instance = Optional.ofNullable("Duke");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   192
        assertTrue(instance.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   193
        assertEquals(instance.get(), "Duke");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   194
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   195
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   196
    @Test(groups = "unit")
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   197
    public void testFilter() {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   198
        // Null mapper function
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   199
        Optional<String> empty = Optional.empty();
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   200
        Optional<String> duke = Optional.of("Duke");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   201
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   202
        try {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   203
            Optional<String> result = empty.filter(null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   204
            fail("Should throw NPE on null mapping function");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   205
        } catch (NullPointerException npe) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   206
            // expected
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   207
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   208
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   209
        Optional<String> result = empty.filter(String::isEmpty);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   210
        assertFalse(result.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   211
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   212
        result = duke.filter(String::isEmpty);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   213
        assertFalse(result.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   214
        result = duke.filter(s -> s.startsWith("D"));
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   215
        assertTrue(result.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   216
        assertEquals(result.get(), "Duke");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   217
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   218
        Optional<String> emptyString = Optional.of("");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   219
        result = emptyString.filter(String::isEmpty);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   220
        assertTrue(result.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   221
        assertEquals(result.get(), "");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   222
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   223
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   224
    @Test(groups = "unit")
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   225
    public void testMap() {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   226
        Optional<String> empty = Optional.empty();
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   227
        Optional<String> duke = Optional.of("Duke");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   228
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   229
        // Null mapper function
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   230
        try {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   231
            Optional<Boolean> b = empty.map(null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   232
            fail("Should throw NPE on null mapping function");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   233
        } catch (NullPointerException npe) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   234
            // expected
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   235
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   236
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   237
        // Map an empty value
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   238
        Optional<Boolean> b = empty.map(String::isEmpty);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   239
        assertFalse(b.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   240
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   241
        // Map into null
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   242
        b = empty.map(n -> null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   243
        assertFalse(b.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   244
        b = duke.map(s -> null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   245
        assertFalse(b.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   246
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   247
        // Map to value
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   248
        Optional<Integer> l = duke.map(String::length);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   249
        assertEquals(l.get().intValue(), 4);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   250
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   251
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   252
    @Test(groups = "unit")
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   253
    public void testFlatMap() {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   254
        Optional<String> empty = Optional.empty();
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   255
        Optional<String> duke = Optional.of("Duke");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   256
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   257
        // Null mapper function
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   258
        try {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   259
            Optional<Boolean> b = empty.flatMap(null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   260
            fail("Should throw NPE on null mapping function");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   261
        } catch (NullPointerException npe) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   262
            // expected
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   263
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   264
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   265
        // Map into null
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   266
        try {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   267
            Optional<Boolean> b = duke.flatMap(s -> null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   268
            fail("Should throw NPE when mapper return null");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   269
        } catch (NullPointerException npe) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   270
            // expected
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   271
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   272
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   273
        // Empty won't invoke mapper function
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   274
        try {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   275
            Optional<Boolean> b = empty.flatMap(s -> null);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   276
            assertFalse(b.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   277
        } catch (NullPointerException npe) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   278
            fail("Mapper function should not be invoked");
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   279
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   280
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   281
        // Map an empty value
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   282
        Optional<Integer> l = empty.flatMap(s -> Optional.of(s.length()));
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   283
        assertFalse(l.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   284
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   285
        // Map to value
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   286
        Optional<Integer> fixture = Optional.of(Integer.MAX_VALUE);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   287
        l = duke.flatMap(s -> Optional.of(s.length()));
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   288
        assertTrue(l.isPresent());
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   289
        assertEquals(l.get().intValue(), 4);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   290
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   291
        // Verify same instance
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   292
        l = duke.flatMap(s -> fixture);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   293
        assertSame(l, fixture);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   294
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   295
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   296
    @Test(groups = "unit")
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   297
    public void testStream() {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   298
        {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   299
            Stream<String> s = Optional.<String>empty().stream();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   300
            assertFalse(s.isParallel());
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   301
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   302
            Object[] es = s.toArray();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   303
            assertEquals(es.length, 0);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   304
        }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   305
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   306
        {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   307
            Stream<String> s = Optional.of("Duke").stream();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   308
            assertFalse(s.isParallel());
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   309
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   310
            String[] es = s.toArray(String[]::new);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   311
            assertEquals(es.length, 1);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   312
            assertEquals(es[0], "Duke");
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   313
        }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   314
    }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 18819
diff changeset
   315
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   316
    private static class ObscureException extends RuntimeException {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   317
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   318
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   319
}