src/share/classes/java/util/OptionalDouble.java

Print this page
rev 7596 : 8015317: Optional.filter, map, and flatMap
Reviewed-by:
Contributed-by: brian.goetz@oracle.com, henry.jen@oracle.com


 169      * argument list can be used as the supplier. For example,
 170      * {@code IllegalStateException::new}
 171      *
 172      * @param <X> Type of the exception to be thrown
 173      * @param exceptionSupplier The supplier which will return the exception to
 174      * be thrown
 175      * @return the present value
 176      * @throws X if there is no value present
 177      * @throws NullPointerException if no value is present and
 178      * {@code exceptionSupplier} is null
 179      */
 180     public<X extends Throwable> double orElseThrow(Supplier<X> exceptionSupplier) throws X {
 181         if (isPresent) {
 182             return value;
 183         } else {
 184             throw exceptionSupplier.get();
 185         }
 186     }
 187 
 188     /**
 189      * Indicates whether some other object is "equal to" this Optional. The
 190      * other object is considered equal if:
 191      * <ul>
 192      * <li>it is also an {@code OptionalInt} and;
 193      * <li>both instances have no value present or;
 194      * <li>the present values are "equal to" each other via {@code Double.compare() == 0}.
 195      * </ul>
 196      *
 197      * @param obj an object to be tested for equality
 198      * @return {code true} if the other object is "equal to" this object
 199      * otherwise {@code false}
 200      */
 201     @Override
 202     public boolean equals(Object obj) {
 203         if (this == obj) {
 204             return true;
 205         }
 206 
 207         if (!(obj instanceof OptionalDouble)) {
 208             return false;
 209         }
 210 
 211         OptionalDouble other = (OptionalDouble) obj;
 212         return (isPresent && other.isPresent)
 213                ? Double.compare(value, other.value) == 0
 214                : isPresent == other.isPresent;
 215     }
 216 
 217     /**
 218      * Returns the hash code value of the present value, if any, or 0 (zero) if
 219      * no value is present.
 220      *
 221      * @return hash code value of the present value or 0 if no value is present
 222      */
 223     @Override
 224     public int hashCode() {
 225         return isPresent ? Double.hashCode(value) : 0;
 226     }
 227 
 228     /**
 229      * Returns a non-empty string representation of this OptionalDouble suitable for


 230      * debugging. The exact presentation format is unspecified and may vary
 231      * between implementations and versions.
 232      *
 233      * @implSpec If a value is present the result must include its string
 234      * representation in the result. Empty and present OptionalDoubless must be
 235      * unambiguously differentiable.
 236      *
 237      * @return the string representation of this instance
 238      */
 239     @Override
 240     public String toString() {
 241         return isPresent
 242                 ? String.format("OptionalDouble[%s]", value)
 243                 : "OptionalDouble.empty";
 244     }
 245 }


 169      * argument list can be used as the supplier. For example,
 170      * {@code IllegalStateException::new}
 171      *
 172      * @param <X> Type of the exception to be thrown
 173      * @param exceptionSupplier The supplier which will return the exception to
 174      * be thrown
 175      * @return the present value
 176      * @throws X if there is no value present
 177      * @throws NullPointerException if no value is present and
 178      * {@code exceptionSupplier} is null
 179      */
 180     public<X extends Throwable> double orElseThrow(Supplier<X> exceptionSupplier) throws X {
 181         if (isPresent) {
 182             return value;
 183         } else {
 184             throw exceptionSupplier.get();
 185         }
 186     }
 187 
 188     /**
 189      * Indicates whether some other object is "equal to" this OptionalDouble. The
 190      * other object is considered equal if:
 191      * <ul>
 192      * <li>it is also an {@code OptionalDouble} and;
 193      * <li>both instances have no value present or;
 194      * <li>the present values are "equal to" each other via {@code Double.compare() == 0}.
 195      * </ul>
 196      *
 197      * @param obj an object to be tested for equality
 198      * @return {code true} if the other object is "equal to" this object
 199      * otherwise {@code false}
 200      */
 201     @Override
 202     public boolean equals(Object obj) {
 203         if (this == obj) {
 204             return true;
 205         }
 206 
 207         if (!(obj instanceof OptionalDouble)) {
 208             return false;
 209         }
 210 
 211         OptionalDouble other = (OptionalDouble) obj;
 212         return (isPresent && other.isPresent)
 213                ? Double.compare(value, other.value) == 0
 214                : isPresent == other.isPresent;
 215     }
 216 
 217     /**
 218      * Returns the hash code value of the present value, if any, or 0 (zero) if
 219      * no value is present.
 220      *
 221      * @return hash code value of the present value or 0 if no value is present
 222      */
 223     @Override
 224     public int hashCode() {
 225         return isPresent ? Double.hashCode(value) : 0;
 226     }
 227 
 228     /**
 229      * {@inheritDoc}
 230      *
 231      * Returns a non-empty string representation of this object suitable for
 232      * debugging. The exact presentation format is unspecified and may vary
 233      * between implementations and versions.
 234      *
 235      * @implSpec If a value is present the result must include its string
 236      * representation in the result. Empty and present instances must be
 237      * unambiguously differentiable.
 238      *
 239      * @return the string representation of this instance
 240      */
 241     @Override
 242     public String toString() {
 243         return isPresent
 244                 ? String.format("OptionalDouble[%s]", value)
 245                 : "OptionalDouble.empty";
 246     }
 247 }