author | weijun |
Wed, 06 Dec 2017 21:17:19 +0800 | |
changeset 49774 | 5950773b665c |
parent 48328 | 7acf5700d542 |
child 49823 | af4b57a556be |
permissions | -rw-r--r-- |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
1 |
/* |
44779
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
2 |
* Copyright (c) 2012, 2017, 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. Oracle designates this |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
10 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
11 |
* 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
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
14 |
* 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
|
15 |
* accompanied this code). |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
16 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
17 |
* 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
|
18 |
* 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
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
20 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
22 |
* 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
|
23 |
* questions. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
24 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
25 |
package java.util; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
26 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
27 |
import java.util.function.Consumer; |
18819 | 28 |
import java.util.function.Function; |
29 |
import java.util.function.Predicate; |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
30 |
import java.util.function.Supplier; |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
31 |
import java.util.stream.Stream; |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
32 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
33 |
/** |
33238 | 34 |
* A container object which may or may not contain a non-{@code null} value. |
48328
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
35 |
* If a value is present, {@code isPresent()} returns {@code true}. If no |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
36 |
* value is present, the object is considered <i>empty</i> and |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
37 |
* {@code isPresent()} returns {@code false}. |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
38 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
39 |
* <p>Additional methods that depend on the presence or absence of a contained |
44779
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
40 |
* value are provided, such as {@link #orElse(Object) orElse()} |
33238 | 41 |
* (returns a default value if no value is present) and |
44779
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
42 |
* {@link #ifPresent(Consumer) ifPresent()} (performs an |
33238 | 43 |
* action if a value is present). |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
44 |
* |
21966
db858b7c6313
8028816: Add value-type notice to Optional* classes
briangoetz
parents:
20479
diff
changeset
|
45 |
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a> |
db858b7c6313
8028816: Add value-type notice to Optional* classes
briangoetz
parents:
20479
diff
changeset
|
46 |
* class; use of identity-sensitive operations (including reference equality |
db858b7c6313
8028816: Add value-type notice to Optional* classes
briangoetz
parents:
20479
diff
changeset
|
47 |
* ({@code ==}), identity hash code, or synchronization) on instances of |
db858b7c6313
8028816: Add value-type notice to Optional* classes
briangoetz
parents:
20479
diff
changeset
|
48 |
* {@code Optional} may have unpredictable results and should be avoided. |
db858b7c6313
8028816: Add value-type notice to Optional* classes
briangoetz
parents:
20479
diff
changeset
|
49 |
* |
44779
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
50 |
* @apiNote |
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
51 |
* {@code Optional} is primarily intended for use as a method return type where |
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
52 |
* there is a clear need to represent "no result," and where using {@code null} |
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
53 |
* is likely to cause errors. A variable whose type is {@code Optional} should |
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
54 |
* never itself be {@code null}; it should always point to an {@code Optional} |
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
55 |
* instance. |
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
56 |
* |
33238 | 57 |
* @param <T> the type of value |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
58 |
* @since 1.8 |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
59 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
60 |
public final class Optional<T> { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
61 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
62 |
* Common instance for {@code empty()}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
63 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
64 |
private static final Optional<?> EMPTY = new Optional<>(); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
65 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
66 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
67 |
* If non-null, the value; if null, indicates no value is present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
68 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
69 |
private final T value; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
70 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
71 |
/** |
18819 | 72 |
* Constructs an empty instance. |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
73 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
74 |
* @implNote Generally only one empty instance, {@link Optional#EMPTY}, |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
75 |
* should exist per VM. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
76 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
77 |
private Optional() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
78 |
this.value = null; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
79 |
} |
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 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
82 |
* Returns an empty {@code Optional} instance. No value is present for this |
33238 | 83 |
* {@code Optional}. |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
84 |
* |
33238 | 85 |
* @apiNote |
86 |
* Though it may be tempting to do so, avoid testing if an object is empty |
|
87 |
* by comparing with {@code ==} against instances returned by |
|
88 |
* {@code Optional.empty()}. There is no guarantee that it is a singleton. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
89 |
* Instead, use {@link #isPresent()}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
90 |
* |
33238 | 91 |
* @param <T> The type of the non-existent value |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
92 |
* @return an empty {@code Optional} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
93 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
94 |
public static<T> Optional<T> empty() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
95 |
@SuppressWarnings("unchecked") |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
96 |
Optional<T> t = (Optional<T>) EMPTY; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
97 |
return t; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
98 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
99 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
100 |
/** |
33238 | 101 |
* Constructs an instance with the described value. |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
102 |
* |
33238 | 103 |
* @param value the non-{@code null} value to describe |
104 |
* @throws NullPointerException if value is {@code null} |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
105 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
106 |
private Optional(T value) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
107 |
this.value = Objects.requireNonNull(value); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
108 |
} |
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 |
/** |
33238 | 111 |
* Returns an {@code Optional} describing the given non-{@code null} |
112 |
* value. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
113 |
* |
33238 | 114 |
* @param value the value to describe, which must be non-{@code null} |
115 |
* @param <T> the type of the value |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
116 |
* @return an {@code Optional} with the value present |
33238 | 117 |
* @throws NullPointerException if value is {@code null} |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
118 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
119 |
public static <T> Optional<T> of(T value) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
120 |
return new Optional<>(value); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
121 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
122 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
123 |
/** |
33238 | 124 |
* Returns an {@code Optional} describing the given value, if |
125 |
* non-{@code null}, otherwise returns an empty {@code Optional}. |
|
18819 | 126 |
* |
33238 | 127 |
* @param value the possibly-{@code null} value to describe |
128 |
* @param <T> the type of the value |
|
18819 | 129 |
* @return an {@code Optional} with a present value if the specified value |
33238 | 130 |
* is non-{@code null}, otherwise an empty {@code Optional} |
18819 | 131 |
*/ |
132 |
public static <T> Optional<T> ofNullable(T value) { |
|
133 |
return value == null ? empty() : of(value); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
33238 | 137 |
* If a value is present, returns the value, otherwise throws |
138 |
* {@code NoSuchElementException}. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
139 |
* |
44779
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
140 |
* @apiNote |
48328
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
141 |
* The preferred alternative to this method is {@link #orElseThrow()}. |
44779
2a39971d67ab
8167981: Optional: add notes explaining intended use
smarks
parents:
41963
diff
changeset
|
142 |
* |
33238 | 143 |
* @return the non-{@code null} value described by this {@code Optional} |
144 |
* @throws NoSuchElementException if no value is present |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
145 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
146 |
public T get() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
147 |
if (value == null) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
148 |
throw new NoSuchElementException("No value present"); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
149 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
150 |
return value; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
151 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
152 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
153 |
/** |
33238 | 154 |
* If a value is present, returns {@code true}, otherwise {@code false}. |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
155 |
* |
33238 | 156 |
* @return {@code true} if a value is present, otherwise {@code false} |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
157 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
158 |
public boolean isPresent() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
159 |
return value != null; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
160 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
161 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
162 |
/** |
33238 | 163 |
* If a value is present, performs the given action with the value, |
164 |
* otherwise does nothing. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
165 |
* |
33238 | 166 |
* @param action the action to be performed, if a value is present |
167 |
* @throws NullPointerException if value is present and the given action is |
|
168 |
* {@code null} |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
169 |
*/ |
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
170 |
public void ifPresent(Consumer<? super T> action) { |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
171 |
if (value != null) { |
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
172 |
action.accept(value); |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
173 |
} |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
174 |
} |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
175 |
|
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
176 |
/** |
33238 | 177 |
* If a value is present, performs the given action with the value, |
178 |
* otherwise performs the given empty-based action. |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
179 |
* |
33238 | 180 |
* @param action the action to be performed, if a value is present |
181 |
* @param emptyAction the empty-based action to be performed, if no value is |
|
182 |
* present |
|
183 |
* @throws NullPointerException if a value is present and the given action |
|
184 |
* is {@code null}, or no value is present and the given empty-based |
|
185 |
* action is {@code null}. |
|
186 |
* @since 9 |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
187 |
*/ |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
188 |
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) { |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
189 |
if (value != null) { |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
190 |
action.accept(value); |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
191 |
} else { |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
192 |
emptyAction.run(); |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
193 |
} |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
194 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
195 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
196 |
/** |
18819 | 197 |
* If a value is present, and the value matches the given predicate, |
33238 | 198 |
* returns an {@code Optional} describing the value, otherwise returns an |
18819 | 199 |
* empty {@code Optional}. |
200 |
* |
|
33238 | 201 |
* @param predicate the predicate to apply to a value, if present |
202 |
* @return an {@code Optional} describing the value of this |
|
203 |
* {@code Optional}, if a value is present and the value matches the |
|
204 |
* given predicate, otherwise an empty {@code Optional} |
|
205 |
* @throws NullPointerException if the predicate is {@code null} |
|
18819 | 206 |
*/ |
207 |
public Optional<T> filter(Predicate<? super T> predicate) { |
|
208 |
Objects.requireNonNull(predicate); |
|
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
209 |
if (!isPresent()) { |
18819 | 210 |
return this; |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
211 |
} else { |
18819 | 212 |
return predicate.test(value) ? this : empty(); |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
213 |
} |
18819 | 214 |
} |
215 |
||
216 |
/** |
|
33238 | 217 |
* If a value is present, returns an {@code Optional} describing (as if by |
218 |
* {@link #ofNullable}) the result of applying the given mapping function to |
|
219 |
* the value, otherwise returns an empty {@code Optional}. |
|
18819 | 220 |
* |
33238 | 221 |
* <p>If the mapping function returns a {@code null} result then this method |
222 |
* returns an empty {@code Optional}. |
|
223 |
* |
|
224 |
* @apiNote |
|
225 |
* This method supports post-processing on {@code Optional} values, without |
|
18819 | 226 |
* the need to explicitly check for a return status. For example, the |
41963 | 227 |
* following code traverses a stream of URIs, selects one that has not |
228 |
* yet been processed, and creates a path from that URI, returning |
|
229 |
* an {@code Optional<Path>}: |
|
18819 | 230 |
* |
231 |
* <pre>{@code |
|
41963 | 232 |
* Optional<Path> p = |
233 |
* uris.stream().filter(uri -> !isProcessedYet(uri)) |
|
18819 | 234 |
* .findFirst() |
41963 | 235 |
* .map(Paths::get); |
18819 | 236 |
* }</pre> |
237 |
* |
|
41963 | 238 |
* Here, {@code findFirst} returns an {@code Optional<URI>}, and then |
239 |
* {@code map} returns an {@code Optional<Path>} for the desired |
|
240 |
* URI if one exists. |
|
18819 | 241 |
* |
33238 | 242 |
* @param mapper the mapping function to apply to a value, if present |
243 |
* @param <U> The type of the value returned from the mapping function |
|
18819 | 244 |
* @return an {@code Optional} describing the result of applying a mapping |
33238 | 245 |
* function to the value of this {@code Optional}, if a value is |
246 |
* present, otherwise an empty {@code Optional} |
|
247 |
* @throws NullPointerException if the mapping function is {@code null} |
|
18819 | 248 |
*/ |
41598
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
249 |
public <U> Optional<U> map(Function<? super T, ? extends U> mapper) { |
18819 | 250 |
Objects.requireNonNull(mapper); |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
251 |
if (!isPresent()) { |
18819 | 252 |
return empty(); |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
253 |
} else { |
18819 | 254 |
return Optional.ofNullable(mapper.apply(value)); |
255 |
} |
|
256 |
} |
|
257 |
||
258 |
/** |
|
33238 | 259 |
* If a value is present, returns the result of applying the given |
260 |
* {@code Optional}-bearing mapping function to the value, otherwise returns |
|
261 |
* an empty {@code Optional}. |
|
262 |
* |
|
263 |
* <p>This method is similar to {@link #map(Function)}, but the mapping |
|
264 |
* function is one whose result is already an {@code Optional}, and if |
|
265 |
* invoked, {@code flatMap} does not wrap it within an additional |
|
18819 | 266 |
* {@code Optional}. |
267 |
* |
|
33238 | 268 |
* @param <U> The type of value of the {@code Optional} returned by the |
269 |
* mapping function |
|
270 |
* @param mapper the mapping function to apply to a value, if present |
|
18819 | 271 |
* @return the result of applying an {@code Optional}-bearing mapping |
33238 | 272 |
* function to the value of this {@code Optional}, if a value is |
273 |
* present, otherwise an empty {@code Optional} |
|
274 |
* @throws NullPointerException if the mapping function is {@code null} or |
|
275 |
* returns a {@code null} result |
|
18819 | 276 |
*/ |
41598
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
277 |
public <U> Optional<U> flatMap(Function<? super T, ? extends Optional<? extends U>> mapper) { |
18819 | 278 |
Objects.requireNonNull(mapper); |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
279 |
if (!isPresent()) { |
18819 | 280 |
return empty(); |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
281 |
} else { |
41598
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
282 |
@SuppressWarnings("unchecked") |
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
283 |
Optional<U> r = (Optional<U>) mapper.apply(value); |
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
284 |
return Objects.requireNonNull(r); |
18819 | 285 |
} |
286 |
} |
|
287 |
||
288 |
/** |
|
33238 | 289 |
* If a value is present, returns an {@code Optional} describing the value, |
290 |
* otherwise returns an {@code Optional} produced by the supplying function. |
|
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
291 |
* |
33238 | 292 |
* @param supplier the supplying function that produces an {@code Optional} |
293 |
* to be returned |
|
294 |
* @return returns an {@code Optional} describing the value of this |
|
295 |
* {@code Optional}, if a value is present, otherwise an |
|
296 |
* {@code Optional} produced by the supplying function. |
|
297 |
* @throws NullPointerException if the supplying function is {@code null} or |
|
298 |
* produces a {@code null} result |
|
299 |
* @since 9 |
|
300 |
*/ |
|
41598
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
301 |
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier) { |
33238 | 302 |
Objects.requireNonNull(supplier); |
303 |
if (isPresent()) { |
|
304 |
return this; |
|
305 |
} else { |
|
41598
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
306 |
@SuppressWarnings("unchecked") |
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
307 |
Optional<T> r = (Optional<T>) supplier.get(); |
755ef825ca6a
8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents:
33238
diff
changeset
|
308 |
return Objects.requireNonNull(r); |
33238 | 309 |
} |
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* If a value is present, returns a sequential {@link Stream} containing |
|
314 |
* only that value, otherwise returns an empty {@code Stream}. |
|
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
315 |
* |
33238 | 316 |
* @apiNote |
317 |
* This method can be used to transform a {@code Stream} of optional |
|
318 |
* elements to a {@code Stream} of present value elements: |
|
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
319 |
* <pre>{@code |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
320 |
* Stream<Optional<T>> os = .. |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
321 |
* Stream<T> s = os.flatMap(Optional::stream) |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
322 |
* }</pre> |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
323 |
* |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
324 |
* @return the optional value as a {@code Stream} |
33238 | 325 |
* @since 9 |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
326 |
*/ |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
327 |
public Stream<T> stream() { |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
328 |
if (!isPresent()) { |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
329 |
return Stream.empty(); |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
330 |
} else { |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
331 |
return Stream.of(value); |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
332 |
} |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
333 |
} |
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
334 |
|
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
25859
diff
changeset
|
335 |
/** |
33238 | 336 |
* If a value is present, returns the value, otherwise returns |
337 |
* {@code other}. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
338 |
* |
33238 | 339 |
* @param other the value to be returned, if no value is present. |
340 |
* May be {@code null}. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
341 |
* @return the value, if present, otherwise {@code other} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
342 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
343 |
public T orElse(T other) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
344 |
return value != null ? value : other; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
345 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
346 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
347 |
/** |
33238 | 348 |
* If a value is present, returns the value, otherwise returns the result |
349 |
* produced by the supplying function. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
350 |
* |
33238 | 351 |
* @param supplier the supplying function that produces a value to be returned |
352 |
* @return the value, if present, otherwise the result produced by the |
|
353 |
* supplying function |
|
354 |
* @throws NullPointerException if no value is present and the supplying |
|
355 |
* function is {@code null} |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
356 |
*/ |
33238 | 357 |
public T orElseGet(Supplier<? extends T> supplier) { |
358 |
return value != null ? value : supplier.get(); |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
359 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
360 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
361 |
/** |
48328
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
362 |
* If a value is present, returns the value, otherwise throws |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
363 |
* {@code NoSuchElementException}. |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
364 |
* |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
365 |
* @return the non-{@code null} value described by this {@code Optional} |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
366 |
* @throws NoSuchElementException if no value is present |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
367 |
* @since 10 |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
368 |
*/ |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
369 |
public T orElseThrow() { |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
370 |
if (value == null) { |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
371 |
throw new NoSuchElementException("No value present"); |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
372 |
} |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
373 |
return value; |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
374 |
} |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
375 |
|
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
376 |
/** |
33238 | 377 |
* If a value is present, returns the value, otherwise throws an exception |
378 |
* produced by the exception supplying function. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
379 |
* |
33238 | 380 |
* @apiNote |
381 |
* A method reference to the exception constructor with an empty argument |
|
382 |
* list can be used as the supplier. For example, |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
383 |
* {@code IllegalStateException::new} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
384 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
385 |
* @param <X> Type of the exception to be thrown |
33238 | 386 |
* @param exceptionSupplier the supplying function that produces an |
387 |
* exception to be thrown |
|
388 |
* @return the value, if present |
|
389 |
* @throws X if no value is present |
|
390 |
* @throws NullPointerException if no value is present and the exception |
|
391 |
* supplying function is {@code null} |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
392 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
393 |
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
394 |
if (value != null) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
395 |
return value; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
396 |
} else { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
397 |
throw exceptionSupplier.get(); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
398 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
399 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
400 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
401 |
/** |
33238 | 402 |
* Indicates whether some other object is "equal to" this {@code Optional}. |
403 |
* The other object is considered equal if: |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
404 |
* <ul> |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
405 |
* <li>it is also an {@code Optional} and; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
406 |
* <li>both instances have no value present or; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
407 |
* <li>the present values are "equal to" each other via {@code equals()}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
408 |
* </ul> |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
409 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
410 |
* @param obj an object to be tested for equality |
29599
e2c48b046f91
8075560: Typo in Javadoc for java.util.Optional.equals()
psandoz
parents:
28963
diff
changeset
|
411 |
* @return {@code true} if the other object is "equal to" this object |
33238 | 412 |
* otherwise {@code false} |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
413 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
414 |
@Override |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
415 |
public boolean equals(Object obj) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
416 |
if (this == obj) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
417 |
return true; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
418 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
419 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
420 |
if (!(obj instanceof Optional)) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
421 |
return false; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
422 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
423 |
|
19220
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
19074
diff
changeset
|
424 |
Optional<?> other = (Optional<?>) obj; |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
425 |
return Objects.equals(value, other.value); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
426 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
427 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
428 |
/** |
33238 | 429 |
* Returns the hash code of the value, if present, otherwise {@code 0} |
430 |
* (zero) if no value is present. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
431 |
* |
33238 | 432 |
* @return hash code value of the present value or {@code 0} if no value is |
433 |
* present |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
434 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
435 |
@Override |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
436 |
public int hashCode() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
437 |
return Objects.hashCode(value); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
438 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
439 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
440 |
/** |
33238 | 441 |
* Returns a non-empty string representation of this {@code Optional} |
442 |
* suitable for debugging. The exact presentation format is unspecified and |
|
443 |
* may vary between implementations and versions. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
444 |
* |
33238 | 445 |
* @implSpec |
446 |
* If a value is present the result must include its string representation |
|
447 |
* in the result. Empty and present {@code Optional}s must be unambiguously |
|
448 |
* differentiable. |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
449 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
450 |
* @return the string representation of this instance |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
451 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
452 |
@Override |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
453 |
public String toString() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
454 |
return value != null |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
455 |
? String.format("Optional[%s]", value) |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
456 |
: "Optional.empty"; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
457 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
458 |
} |