author | smarks |
Thu, 29 Mar 2018 16:12:41 -0700 | |
branch | JDK-8195649-branch |
changeset 56376 | 718b9df3e302 |
parent 56375 | e0530ee8af48 |
child 56383 | 4872fc3de1b0 |
permissions | -rw-r--r-- |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
1 |
/* |
56376 | 2 |
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. |
16489
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 |
56376 | 25 |
* @bug 8195649 |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
26 |
* @summary Basic functional test of OptionalLong |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
27 |
* @author Mike Duigou |
56375
e0530ee8af48
Move ObscureException to its own file. Rewrite BasicDouble.
smarks
parents:
48328
diff
changeset
|
28 |
* @build ObscureException |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
29 |
* @run testng BasicLong |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
30 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
31 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
32 |
import java.util.NoSuchElementException; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
33 |
import java.util.OptionalLong; |
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
34 |
import java.util.concurrent.atomic.AtomicBoolean; |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
16489
diff
changeset
|
35 |
import java.util.stream.LongStream; |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
36 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
37 |
import static org.testng.Assert.*; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
38 |
import org.testng.annotations.Test; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
39 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
40 |
public class BasicLong { |
56376 | 41 |
static final long LONGVAL = 2_305_843_008_139_952_128L; |
42 |
static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL; |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
43 |
|
56376 | 44 |
/** |
45 |
* Checks a block of assertions over an empty OptionalLong. |
|
46 |
*/ |
|
47 |
void checkEmpty(OptionalLong empty) { |
|
48 |
assertTrue(empty.equals(OptionalLong.empty())); |
|
49 |
assertTrue(OptionalLong.empty().equals(empty)); |
|
50 |
assertFalse(empty.equals(OptionalLong.of(UNEXPECTED))); |
|
51 |
assertFalse(OptionalLong.of(UNEXPECTED).equals(empty)); |
|
52 |
assertFalse(empty.equals("unexpected")); |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
53 |
|
56376 | 54 |
assertFalse(empty.isPresent()); |
55 |
assertEquals(empty.hashCode(), 0); |
|
56 |
assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); |
|
57 |
assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); |
|
58 |
||
59 |
assertThrows(NoSuchElementException.class, () -> empty.getAsLong()); |
|
60 |
assertThrows(NoSuchElementException.class, () -> empty.orElseThrow()); |
|
61 |
assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new)); |
|
62 |
||
63 |
var b = new AtomicBoolean(); |
|
64 |
empty.ifPresent(s -> b.set(true)); |
|
65 |
assertFalse(b.get()); |
|
66 |
||
67 |
var b1 = new AtomicBoolean(false); |
|
68 |
var b2 = new AtomicBoolean(false); |
|
69 |
empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); |
|
70 |
assertFalse(b1.get()); |
|
71 |
assertTrue(b2.get()); |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
72 |
|
56376 | 73 |
assertEquals(empty.toString(), "OptionalLong.empty"); |
74 |
} |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
75 |
|
56376 | 76 |
/** |
77 |
* Checks a block of assertions over an OptionalLong that is expected to |
|
78 |
* have a particular value present. |
|
79 |
*/ |
|
80 |
void checkPresent(OptionalLong opt, long expected) { |
|
81 |
assertFalse(opt.equals(OptionalLong.empty())); |
|
82 |
assertFalse(OptionalLong.empty().equals(opt)); |
|
83 |
assertTrue(opt.equals(OptionalLong.of(expected))); |
|
84 |
assertTrue(OptionalLong.of(expected).equals(opt)); |
|
85 |
assertFalse(opt.equals(OptionalLong.of(UNEXPECTED))); |
|
86 |
assertFalse(OptionalLong.of(UNEXPECTED).equals(opt)); |
|
87 |
assertFalse(opt.equals("unexpected")); |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
88 |
|
56376 | 89 |
assertTrue(opt.isPresent()); |
90 |
assertEquals(opt.hashCode(), Long.hashCode(expected)); |
|
91 |
assertEquals(opt.orElse(UNEXPECTED), expected); |
|
92 |
assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); |
|
93 |
||
94 |
assertEquals(opt.getAsLong(), expected); |
|
95 |
assertEquals(opt.orElseThrow(), expected); |
|
96 |
assertEquals(opt.orElseThrow(ObscureException::new), expected); |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
97 |
|
56376 | 98 |
var b = new AtomicBoolean(false); |
99 |
opt.ifPresent(s -> b.set(true)); |
|
100 |
assertTrue(b.get()); |
|
101 |
||
102 |
var b1 = new AtomicBoolean(false); |
|
103 |
var b2 = new AtomicBoolean(false); |
|
104 |
opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); |
|
105 |
assertTrue(b1.get()); |
|
106 |
assertFalse(b2.get()); |
|
107 |
||
108 |
assertEquals(opt.toString(), "OptionalLong[" + expected + "]"); |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
109 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
110 |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
111 |
@Test(groups = "unit") |
56376 | 112 |
public void testEmpty() { |
113 |
checkEmpty(OptionalLong.empty()); |
|
48328
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
114 |
} |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
115 |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
116 |
@Test(groups = "unit") |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
117 |
public void testPresent() { |
56376 | 118 |
checkPresent(OptionalLong.of(LONGVAL), LONGVAL); |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
119 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
120 |
|
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
16489
diff
changeset
|
121 |
@Test(groups = "unit") |
56376 | 122 |
public void testStreamEmpty() { |
123 |
assertEquals(OptionalLong.empty().stream().toArray(), new long[] { }); |
|
124 |
} |
|
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
16489
diff
changeset
|
125 |
|
56376 | 126 |
@Test(groups = "unit") |
127 |
public void testStreamPresent() { |
|
128 |
assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL }); |
|
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
16489
diff
changeset
|
129 |
} |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
130 |
} |