1 /* 2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.util; 26 27 import java.util.function.DoubleConsumer; 28 import java.util.function.DoubleSupplier; 29 import java.util.function.Supplier; 30 import java.util.stream.DoubleStream; 31 32 /** 33 * A container object which may or may not contain a {@code double} value. If a 34 * value is present, {@code isPresent()} returns {@code true} and 35 * {@code getAsDouble()} returns the value. 36 * 37 * <p>Additional methods that depend on the presence or absence of a contained 38 * value are provided, such as {@link #orElse(double) orElse()} 39 * (returns a default value if no value is present) and 40 * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (performs 41 * an action if a value is present). 42 * 43 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a> 44 * class; use of identity-sensitive operations (including reference equality 45 * ({@code ==}), identity hash code, or synchronization) on instances of 46 * {@code OptionalDouble} may have unpredictable results and should be avoided. 47 * 48 * @apiNote 49 * {@code OptionalDouble} is primarily intended for use as a method return type where 50 * there is a clear need to represent "no result," and where using {@code null} 51 * is likely to cause errors. A variable whose type is {@code OptionalDouble} should 52 * never itself be {@code null}; it should always point to an {@code OptionalDouble} 53 * instance. 54 * 55 * @since 1.8 56 */ 57 public final class OptionalDouble { 58 /** 59 * Common instance for {@code empty()}. 60 */ 61 private static final OptionalDouble EMPTY = new OptionalDouble(); 62 63 /** 64 * If true then the value is present, otherwise indicates no value is present 65 */ 66 private final boolean isPresent; 67 private final double value; 68 69 /** 70 * Construct an empty instance. 71 * 72 * @implNote generally only one empty instance, {@link OptionalDouble#EMPTY}, 73 * should exist per VM. 74 */ 75 private OptionalDouble() { 76 this.isPresent = false; 77 this.value = Double.NaN; 78 } 79 80 /** 81 * Returns an empty {@code OptionalDouble} instance. No value is present 82 * for this {@code OptionalDouble}. 83 * 84 * @apiNote 85 * Though it may be tempting to do so, avoid testing if an object is empty 86 * by comparing with {@code ==} against instances returned by 87 * {@code OptionalDouble.empty()}. There is no guarantee that it is a singleton. 88 * Instead, use {@link #isPresent()}. 89 * 90 * @return an empty {@code OptionalDouble}. 91 */ 92 public static OptionalDouble empty() { 93 return EMPTY; 94 } 95 96 /** 97 * Construct an instance with the described value. 98 * 99 * @param value the double value to describe. 100 */ 101 private OptionalDouble(double value) { 102 this.isPresent = true; 103 this.value = value; 104 } 105 106 /** 107 * Returns an {@code OptionalDouble} describing the given value. 108 * 109 * @param value the value to describe 110 * @return an {@code OptionalDouble} with the value present 111 */ 112 public static OptionalDouble of(double value) { 113 return new OptionalDouble(value); 114 } 115 116 /** 117 * If a value is present, returns the value, otherwise throws 118 * {@code NoSuchElementException}. 119 * 120 * @apiNote 121 * The methods {@link #orElse(double) orElse()} and 122 * {@link #orElseGet(java.util.function.DoubleSupplier) orElseGet()} 123 * are generally preferable to this method, as they return a substitute 124 * value if the value is absent, instead of throwing an exception. 125 * 126 * @return the value described by this {@code OptionalDouble} 127 * @throws NoSuchElementException if no value is present 128 * @see OptionalDouble#isPresent() 129 */ 130 public double getAsDouble() { 131 if (!isPresent) { 132 throw new NoSuchElementException("No value present"); 133 } 134 return value; 135 } 136 137 /** 138 * If a value is present, returns {@code true}, otherwise {@code false}. 139 * 140 * @return {@code true} if a value is present, otherwise {@code false} 141 */ 142 public boolean isPresent() { 143 return isPresent; 144 } 145 146 /** 147 * If a value is present, performs the given action with the value, 148 * otherwise does nothing. 149 * 150 * @param action the action to be performed, if a value is present 151 * @throws NullPointerException if value is present and the given action is 152 * {@code null} 153 */ 154 public void ifPresent(DoubleConsumer action) { 155 if (isPresent) { 156 action.accept(value); 157 } 158 } 159 160 /** 161 * If a value is present, performs the given action with the value, 162 * otherwise performs the given empty-based action. 163 * 164 * @param action the action to be performed, if a value is present 165 * @param emptyAction the empty-based action to be performed, if no value is 166 * present 167 * @throws NullPointerException if a value is present and the given action 168 * is {@code null}, or no value is present and the given empty-based 169 * action is {@code null}. 170 * @since 9 171 */ 172 public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) { 173 if (isPresent) { 174 action.accept(value); 175 } else { 176 emptyAction.run(); 177 } 178 } 179 180 /** 181 * If a value is present, returns a sequential {@link DoubleStream} 182 * containing only that value, otherwise returns an empty 183 * {@code DoubleStream}. 184 * 185 * @apiNote 186 * This method can be used to transform a {@code Stream} of optional doubles 187 * to a {@code DoubleStream} of present doubles: 188 * <pre>{@code 189 * Stream<OptionalDouble> os = .. 190 * DoubleStream s = os.flatMapToDouble(OptionalDouble::stream) 191 * }</pre> 192 * 193 * @return the optional value as a {@code DoubleStream} 194 * @since 9 195 */ 196 public DoubleStream stream() { 197 if (isPresent) { 198 return DoubleStream.of(value); 199 } else { 200 return DoubleStream.empty(); 201 } 202 } 203 204 /** 205 * If a value is present, returns the value, otherwise returns 206 * {@code other}. 207 * 208 * @param other the value to be returned, if no value is present 209 * @return the value, if present, otherwise {@code other} 210 */ 211 public double orElse(double other) { 212 return isPresent ? value : other; 213 } 214 215 /** 216 * If a value is present, returns the value, otherwise returns the result 217 * produced by the supplying function. 218 * 219 * @param supplier the supplying function that produces a value to be returned 220 * @return the value, if present, otherwise the result produced by the 221 * supplying function 222 * @throws NullPointerException if no value is present and the supplying 223 * function is {@code null} 224 */ 225 public double orElseGet(DoubleSupplier supplier) { 226 return isPresent ? value : supplier.getAsDouble(); 227 } 228 229 /** 230 * If a value is present, returns the value, otherwise throws an exception 231 * produced by the exception supplying function. 232 * 233 * @apiNote 234 * A method reference to the exception constructor with an empty argument 235 * list can be used as the supplier. For example, 236 * {@code IllegalStateException::new} 237 * 238 * @param <X> Type of the exception to be thrown 239 * @param exceptionSupplier the supplying function that produces an 240 * exception to be thrown 241 * @return the value, if present 242 * @throws X if no value is present 243 * @throws NullPointerException if no value is present and the exception 244 * supplying function is {@code null} 245 */ 246 public<X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { 247 if (isPresent) { 248 return value; 249 } else { 250 throw exceptionSupplier.get(); 251 } 252 } 253 254 /** 255 * Indicates whether some other object is "equal to" this 256 * {@code OptionalDouble}. The other object is considered equal if: 257 * <ul> 258 * <li>it is also an {@code OptionalDouble} and; 259 * <li>both instances have no value present or; 260 * <li>the present values are "equal to" each other via 261 * {@code Double.compare() == 0}. 262 * </ul> 263 * 264 * @param obj an object to be tested for equality 265 * @return {@code true} if the other object is "equal to" this object 266 * otherwise {@code false} 267 */ 268 @Override 269 public boolean equals(Object obj) { 270 if (this == obj) { 271 return true; 272 } 273 274 if (!(obj instanceof OptionalDouble)) { 275 return false; 276 } 277 278 OptionalDouble other = (OptionalDouble) obj; 279 return (isPresent && other.isPresent) 280 ? Double.compare(value, other.value) == 0 281 : isPresent == other.isPresent; 282 } 283 284 /** 285 * Returns the hash code of the value, if present, otherwise {@code 0} 286 * (zero) if no value is present. 287 * 288 * @return hash code value of the present value or {@code 0} if no value is 289 * present 290 */ 291 @Override 292 public int hashCode() { 293 return isPresent ? Double.hashCode(value) : 0; 294 } 295 296 /** 297 * Returns a non-empty string representation of this {@code OptionalDouble} 298 * suitable for debugging. The exact presentation format is unspecified and 299 * may vary between implementations and versions. 300 * 301 * @implSpec 302 * If a value is present the result must include its string representation 303 * in the result. Empty and present {@code OptionalDouble}s must be 304 * unambiguously differentiable. 305 * 306 * @return the string representation of this instance 307 */ 308 @Override 309 public String toString() { 310 return isPresent 311 ? String.format("OptionalDouble[%s]", value) 312 : "OptionalDouble.empty"; 313 } 314 }