author | darcy |
Wed, 10 Jul 2013 11:05:39 -0700 | |
changeset 18796 | 486b43748d9b |
parent 16489 | 53619efef3fb |
child 18819 | c4335fc31aeb |
permissions | -rw-r--r-- |
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) 2012, 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. 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.DoubleConsumer; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
28 |
import java.util.function.DoubleSupplier; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
29 |
import java.util.function.Supplier; |
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 |
* A container object which may or may not contain a {@code double} value. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
33 |
* If a value is present, {@code isPresent()} will return {@code true} and |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
34 |
* {@code get()} will return the value. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
35 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
36 |
* <p>Additional methods that depend on the presence or absence of a contained |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
37 |
* value are provided, such as {@link #orElse(double) orElse()} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
38 |
* (return a default value if value not present) and |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
39 |
* {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (execute a block |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
40 |
* of code if the value is present). |
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 |
* @since 1.8 |
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 |
public final class OptionalDouble { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
45 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
46 |
* Common instance for {@code empty()}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
47 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
48 |
private static final OptionalDouble EMPTY = new OptionalDouble(); |
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 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
51 |
* If true then the value is present, otherwise indicates no value is present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
52 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
53 |
private final boolean isPresent; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
54 |
private final double value; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
55 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
56 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
57 |
* Construct an empty instance. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
58 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
59 |
* @implNote generally only one empty instance, {@link OptionalDouble#EMPTY}, |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
60 |
* should exist per VM. |
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 |
private OptionalDouble() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
63 |
this.isPresent = false; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
64 |
this.value = Double.NaN; |
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 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
68 |
* Returns an empty {@code OptionalDouble} instance. No value is present for this |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
69 |
* OptionalDouble. |
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 |
* @apiNote Though it may be tempting to do so, avoid testing if an object |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
72 |
* is empty by comparing with {@code ==} against instances returned by |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
73 |
* {@code Option.empty()}. There is no guarantee that it is a singleton. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
74 |
* Instead, use {@link #isPresent()}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
75 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
76 |
* @return an empty {@code OptionalDouble}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
77 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
78 |
public static OptionalDouble empty() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
79 |
return EMPTY; |
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 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
83 |
* Construct an instance with the value present. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
84 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
85 |
* @param value the double value to be present. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
86 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
87 |
private OptionalDouble(double value) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
88 |
this.isPresent = true; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
89 |
this.value = value; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
90 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
91 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
92 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
93 |
* Return an {@code OptionalDouble} with the specified value present. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
94 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
95 |
* @param value the value to be present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
96 |
* @return an {@code OptionalDouble} with the value present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
97 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
98 |
public static OptionalDouble of(double value) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
99 |
return new OptionalDouble(value); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
100 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
101 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
102 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
103 |
* If a value is present in this {@code OptionalDouble}, returns the value, |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
104 |
* otherwise throws {@code NoSuchElementException}. |
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 |
* @return the value held by this {@code OptionalDouble} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
107 |
* @throws NoSuchElementException if there is no value present |
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 |
* @see OptionalDouble#isPresent() |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
110 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
111 |
public double getAsDouble() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
112 |
if (!isPresent) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
113 |
throw new NoSuchElementException("No value present"); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
114 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
115 |
return value; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
116 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
117 |
|
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 |
* Return {@code true} if there is a value present, otherwise {@code false}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
120 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
121 |
* @return {@code true} if there is a value present, otherwise {@code false} |
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 |
public boolean isPresent() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
124 |
return isPresent; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
125 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
126 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
127 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
128 |
* Have the specified consumer accept the value if a value is present, |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
129 |
* otherwise do nothing. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
130 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
131 |
* @param consumer block to be executed if a value is present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
132 |
* @throws NullPointerException if value is present and {@code consumer} is |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
133 |
* null |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
134 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
135 |
public void ifPresent(DoubleConsumer consumer) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
136 |
if (isPresent) |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
137 |
consumer.accept(value); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
138 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
139 |
|
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 |
* Return the value if present, otherwise return {@code other}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
142 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
143 |
* @param other the value to be returned if there is no value present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
144 |
* @return the value, if present, otherwise {@code other} |
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 double orElse(double other) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
147 |
return isPresent ? value : other; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
148 |
} |
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 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
151 |
* Return the value if present, otherwise invoke {@code other} and return |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
152 |
* the result of that invocation. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
153 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
154 |
* @param other a {@code DoubleSupplier} whose result is returned if no value |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
155 |
* is present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
156 |
* @return the value if present otherwise the result of {@code other.getAsDouble()} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
157 |
* @throws NullPointerException if value is not present and {@code other} is |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
158 |
* null |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
159 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
160 |
public double orElseGet(DoubleSupplier other) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
161 |
return isPresent ? value : other.getAsDouble(); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
162 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
163 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
164 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
165 |
* Return the contained value, if present, otherwise throw an exception |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
166 |
* to be created by the provided supplier. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
167 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
168 |
* @apiNote A method reference to the exception constructor with an empty |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
169 |
* argument list can be used as the supplier. For example, |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
170 |
* {@code IllegalStateException::new} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
171 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
172 |
* @param <X> Type of the exception to be thrown |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
173 |
* @param exceptionSupplier The supplier which will return the exception to |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
174 |
* be thrown |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
175 |
* @return the present value |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
176 |
* @throws X if there is no value present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
177 |
* @throws NullPointerException if no value is present and |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
178 |
* {@code exceptionSupplier} is null |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
179 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
180 |
public<X extends Throwable> double orElseThrow(Supplier<X> exceptionSupplier) throws X { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
181 |
if (isPresent) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
182 |
return value; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
183 |
} else { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
184 |
throw exceptionSupplier.get(); |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
185 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
186 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
187 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
188 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
189 |
* Indicates whether some other object is "equal to" this Optional. The |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
190 |
* other object is considered equal if: |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
191 |
* <ul> |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
192 |
* <li>it is also an {@code OptionalInt} and; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
193 |
* <li>both instances have no value present or; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
194 |
* <li>the present values are "equal to" each other via {@code Double.compare() == 0}. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
195 |
* </ul> |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
196 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
197 |
* @param obj an object to be tested for equality |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
198 |
* @return {code true} if the other object is "equal to" this object |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
199 |
* otherwise {@code false} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
200 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
201 |
@Override |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
202 |
public boolean equals(Object obj) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
203 |
if (this == obj) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
204 |
return true; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
205 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
206 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
207 |
if (!(obj instanceof OptionalDouble)) { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
208 |
return false; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
209 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
210 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
211 |
OptionalDouble other = (OptionalDouble) obj; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
212 |
return (isPresent && other.isPresent) |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
213 |
? Double.compare(value, other.value) == 0 |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
214 |
: isPresent == other.isPresent; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
215 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
216 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
217 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
218 |
* Returns the hash code value of the present value, if any, or 0 (zero) if |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
219 |
* no value is present. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
220 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
221 |
* @return hash code value of the present value or 0 if no value is present |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
222 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
223 |
@Override |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
224 |
public int hashCode() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
225 |
return isPresent ? Double.hashCode(value) : 0; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
226 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
227 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
228 |
/** |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
229 |
* Returns a non-empty string representation of this OptionalDouble suitable for |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
230 |
* debugging. The exact presentation format is unspecified and may vary |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
231 |
* between implementations and versions. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
232 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
233 |
* @implSpec If a value is present the result must include its string |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
234 |
* representation in the result. Empty and present OptionalDoubless must be |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
235 |
* unambiguously differentiable. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
236 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
237 |
* @return the string representation of this instance |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
238 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
239 |
@Override |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
240 |
public String toString() { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
241 |
return isPresent |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
242 |
? String.format("OptionalDouble[%s]", value) |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
243 |
: "OptionalDouble.empty"; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
244 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
245 |
} |