This commit is contained in:
Bob Vandette 2015-10-19 15:48:09 -04:00
commit 3ab71df2e0
19 changed files with 600 additions and 346 deletions

View File

@ -328,3 +328,4 @@ c8753d0be1778944dc512ec86a459941ea1ad2c3 jdk9-b78
f61a63b7d1e52e307abc0bfc751203155d362ec4 jdk9-b83
51b2db2fa04c16d767b66113dbf08c5349ce382a jdk9-b84
8392405ab038b22e69a3728e17dbdd9e3d3a22ed jdk9-b85
7db0663a5e968059fa7c772172187ebd60b6492d jdk9-b86

View File

@ -328,3 +328,4 @@ b8afcf91331d78626a583ec1b63164468d6f4181 jdk9-b81
ce5c14d97d95084504c32b9320cb33cce4235588 jdk9-b83
1c8134475511ffe6726677e1418a89a7a45e92d6 jdk9-b84
1f345217c9bab05f192d00cf1665b3286c49ccdb jdk9-b85
2aa1daf98d3e2ee37f20f6858c53cc37020f6937 jdk9-b86

View File

@ -328,3 +328,4 @@ c20d8ebddaa6fb09cc81d3edf3d1d05f4232700a jdk9-b82
ca8a1719588424f6e04e943790c7fcb7cb0b8c8f jdk9-b83
df70bb200356fec686681f0295c50cc3ed43c3b3 jdk9-b84
3ec06af1368924469f7ce60a00324bac55eaeecc jdk9-b85
0a3f0d25c201b40575a7c3920fce4d6f4d3ae310 jdk9-b86

View File

@ -488,3 +488,4 @@ e9e63d93bbfe2c6c23447e2c1f5cc71c98671cba jdk9-b79
3ed0df2c553a80e0e26b91a6ce08806ea17a066a jdk9-b83
184c4328444974edd6b3b490b9d0177ace7e331c jdk9-b84
03845376ea9dbf9690b6a9cfb4ed63f8cc0541c0 jdk9-b85
1ae4191359d811a51512f17dca80ffe79837a5ff jdk9-b86

View File

@ -328,3 +328,4 @@ f464f9b2fb1178f6a957e5730b4b5252c6149ed9 jdk9-b80
497bc2654e11684b11de46744227883d7e760f35 jdk9-b83
91795d86744f3074d1e59b1e75d9c851c098688f jdk9-b84
1d9850c1b35c74e8b5c17970ed5d46dc0fc33f06 jdk9-b85
88d9b1f6b73e0c46fcb5ccabe1231a30ce758a22 jdk9-b86

View File

@ -331,3 +331,4 @@ e9940bf1c8ddaa6f1f5f1813846b080f0ccaf50b jdk9-b80
d7ee8157f4feced67924e421225c6f844079a03d jdk9-b83
51729143f8fe038f52cf55720c4c1f89267f5948 jdk9-b84
67b626ec730d2601d95ef036d06be34b37fa9ce6 jdk9-b85
5289646179079394890a34a898a8ab70fcde0331 jdk9-b86

View File

@ -328,3 +328,4 @@ b10b64263b563e21f055c881444f625ec618b826 jdk9-b82
d11f25ce3c545823f53bb978d454a4d2901abac3 jdk9-b83
757ef7f6d0042934edea3e0bf616fad2c1a22789 jdk9-b84
fe40b31c0e526d357cf5b62044fea006e43b53a5 jdk9-b85
e8a66c0b05d786a282a7ff1d7eb4989afa30c891 jdk9-b86

View File

@ -0,0 +1,31 @@
#
# Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation. Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
include LauncherCommon.gmk
$(eval $(call SetupLauncher,jshell, \
-DEXPAND_CLASSPATH_WILDCARDS \
-DNEVER_ACT_AS_SERVER_CLASS_MACHINE \
-DJAVA_ARGS='{ "-J-ms8m"$(COMMA) "jdk.internal.jshell.tool.JShellTool"$(COMMA) }'))

View File

@ -31,21 +31,22 @@ import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* A container object which may or may not contain a non-null value.
* If a value is present, {@code isPresent()} will return {@code true} and
* {@code get()} will return the value.
* A container object which may or may not contain a non-{@code null} value.
* If a value is present, {@code isPresent()} returns {@code true} and
* {@code get()} returns the value.
*
* <p>Additional methods that depend on the presence or absence of a contained
* value are provided, such as {@link #orElse(java.lang.Object) orElse()}
* (return a default value if value not present) and
* {@link #ifPresent(java.util.function.Consumer) ifPresent()} (perform an
* action if the value is present).
* (returns a default value if no value is present) and
* {@link #ifPresent(java.util.function.Consumer) ifPresent()} (performs an
* action if a value is present).
*
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
* class; use of identity-sensitive operations (including reference equality
* ({@code ==}), identity hash code, or synchronization) on instances of
* {@code Optional} may have unpredictable results and should be avoided.
*
* @param <T> the type of value
* @since 1.8
*/
public final class Optional<T> {
@ -71,14 +72,15 @@ public final class Optional<T> {
/**
* Returns an empty {@code Optional} instance. No value is present for this
* Optional.
* {@code Optional}.
*
* @apiNote Though it may be tempting to do so, avoid testing if an object
* is empty by comparing with {@code ==} against instances returned by
* {@code Option.empty()}. There is no guarantee that it is a singleton.
* @apiNote
* Though it may be tempting to do so, avoid testing if an object is empty
* by comparing with {@code ==} against instances returned by
* {@code Optional.empty()}. There is no guarantee that it is a singleton.
* Instead, use {@link #isPresent()}.
*
* @param <T> Type of the non-existent value
* @param <T> The type of the non-existent value
* @return an empty {@code Optional}
*/
public static<T> Optional<T> empty() {
@ -88,47 +90,47 @@ public final class Optional<T> {
}
/**
* Constructs an instance with the value present.
* Constructs an instance with the described value.
*
* @param value the non-null value to be present
* @throws NullPointerException if value is null
* @param value the non-{@code null} value to describe
* @throws NullPointerException if value is {@code null}
*/
private Optional(T value) {
this.value = Objects.requireNonNull(value);
}
/**
* Returns an {@code Optional} with the specified present non-null value.
* Returns an {@code Optional} describing the given non-{@code null}
* value.
*
* @param <T> the class of the value
* @param value the value to be present, which must be non-null
* @param value the value to describe, which must be non-{@code null}
* @param <T> the type of the value
* @return an {@code Optional} with the value present
* @throws NullPointerException if value is null
* @throws NullPointerException if value is {@code null}
*/
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
/**
* Returns an {@code Optional} describing the specified value, if non-null,
* otherwise returns an empty {@code Optional}.
* Returns an {@code Optional} describing the given value, if
* non-{@code null}, otherwise returns an empty {@code Optional}.
*
* @param <T> the class of the value
* @param value the possibly-null value to describe
* @param value the possibly-{@code null} value to describe
* @param <T> the type of the value
* @return an {@code Optional} with a present value if the specified value
* is non-null, otherwise an empty {@code Optional}
* is non-{@code null}, otherwise an empty {@code Optional}
*/
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
/**
* If a value is present in this {@code Optional}, returns the value,
* otherwise throws {@code NoSuchElementException}.
*
* @return the non-null value held by this {@code Optional}
* @throws NoSuchElementException if there is no value present
* If a value is present, returns the value, otherwise throws
* {@code NoSuchElementException}.
*
* @return the non-{@code null} value described by this {@code Optional}
* @throws NoSuchElementException if no value is present
* @see Optional#isPresent()
*/
public T get() {
@ -139,21 +141,21 @@ public final class Optional<T> {
}
/**
* Return {@code true} if there is a value present, otherwise {@code false}.
* If a value is present, returns {@code true}, otherwise {@code false}.
*
* @return {@code true} if there is a value present, otherwise {@code false}
* @return {@code true} if a value is present, otherwise {@code false}
*/
public boolean isPresent() {
return value != null;
}
/**
* If a value is present, perform the given action with the value,
* otherwise do nothing.
* If a value is present, performs the given action with the value,
* otherwise does nothing.
*
* @param action the action to be performed if a value is present
* @throws NullPointerException if a value is present and {@code action} is
* null
* @param action the action to be performed, if a value is present
* @throws NullPointerException if value is present and the given action is
* {@code null}
*/
public void ifPresent(Consumer<? super T> action) {
if (value != null) {
@ -162,15 +164,16 @@ public final class Optional<T> {
}
/**
* If a value is present, perform the given action with the value,
* otherwise perform the given empty-based action.
* If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* @param action the action to be performed if a value is present
* @param emptyAction the empty-based action to be performed if a value is
* not present
* @throws NullPointerException if a value is present and {@code action} is
* null, or a value is not present and {@code emptyAction} is null.
* @since 1.9
* @param action the action to be performed, if a value is present
* @param emptyAction the empty-based action to be performed, if no value is
* present
* @throws NullPointerException if a value is present and the given action
* is {@code null}, or no value is present and the given empty-based
* action is {@code null}.
* @since 9
*/
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
if (value != null) {
@ -182,14 +185,14 @@ public final class Optional<T> {
/**
* If a value is present, and the value matches the given predicate,
* return an {@code Optional} describing the value, otherwise return an
* returns an {@code Optional} describing the value, otherwise returns an
* empty {@code Optional}.
*
* @param predicate a predicate to apply to the value, if present
* @return an {@code Optional} describing the value of this {@code Optional}
* if a value is present and the value matches the given predicate,
* otherwise an empty {@code Optional}
* @throws NullPointerException if the predicate is null
* @param predicate the predicate to apply to a value, if present
* @return an {@code Optional} describing the value of this
* {@code Optional}, if a value is present and the value matches the
* given predicate, otherwise an empty {@code Optional}
* @throws NullPointerException if the predicate is {@code null}
*/
public Optional<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
@ -201,14 +204,18 @@ public final class Optional<T> {
}
/**
* If a value is present, apply the provided mapping function to it,
* and if the result is non-null, return an {@code Optional} describing the
* result. Otherwise return an empty {@code Optional}.
* If a value is present, returns an {@code Optional} describing (as if by
* {@link #ofNullable}) the result of applying the given mapping function to
* the value, otherwise returns an empty {@code Optional}.
*
* @apiNote This method supports post-processing on optional values, without
* <p>If the mapping function returns a {@code null} result then this method
* returns an empty {@code Optional}.
*
* @apiNote
* This method supports post-processing on {@code Optional} values, without
* the need to explicitly check for a return status. For example, the
* following code traverses a stream of file names, selects one that has
* not yet been processed, and then opens that file, returning an
* following code traverses a stream of file names, selects one that has not
* yet been processed, and then opens that file, returning an
* {@code Optional<FileInputStream>}:
*
* <pre>{@code
@ -222,12 +229,12 @@ public final class Optional<T> {
* {@code map} returns an {@code Optional<FileInputStream>} for the desired
* file if one exists.
*
* @param <U> The type of the result of the mapping function
* @param mapper a mapping function to apply to the value, if present
* @param mapper the mapping function to apply to a value, if present
* @param <U> The type of the value returned from the mapping function
* @return an {@code Optional} describing the result of applying a mapping
* function to the value of this {@code Optional}, if a value is present,
* otherwise an empty {@code Optional}
* @throws NullPointerException if the mapping function is null
* function to the value of this {@code Optional}, if a value is
* present, otherwise an empty {@code Optional}
* @throws NullPointerException if the mapping function is {@code null}
*/
public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
Objects.requireNonNull(mapper);
@ -239,21 +246,23 @@ public final class Optional<T> {
}
/**
* If a value is present, apply the provided {@code Optional}-bearing
* mapping function to it, return that result, otherwise return an empty
* {@code Optional}. This method is similar to {@link #map(Function)},
* but the provided mapper is one whose result is already an {@code Optional},
* and if invoked, {@code flatMap} does not wrap it with an additional
* If a value is present, returns the result of applying the given
* {@code Optional}-bearing mapping function to the value, otherwise returns
* an empty {@code Optional}.
*
* <p>This method is similar to {@link #map(Function)}, but the mapping
* function is one whose result is already an {@code Optional}, and if
* invoked, {@code flatMap} does not wrap it within an additional
* {@code Optional}.
*
* @param <U> The type parameter to the {@code Optional} returned by
* @param mapper a mapping function to apply to the value, if present
* the mapping function
* @param <U> The type of value of the {@code Optional} returned by the
* mapping function
* @param mapper the mapping function to apply to a value, if present
* @return the result of applying an {@code Optional}-bearing mapping
* function to the value of this {@code Optional}, if a value is present,
* otherwise an empty {@code Optional}
* @throws NullPointerException if the mapping function is null or returns
* a null result
* function to the value of this {@code Optional}, if a value is
* present, otherwise an empty {@code Optional}
* @throws NullPointerException if the mapping function is {@code null} or
* returns a {@code null} result
*/
public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
Objects.requireNonNull(mapper);
@ -265,19 +274,41 @@ public final class Optional<T> {
}
/**
* If a value is present return a sequential {@link Stream} containing only
* that value, otherwise return an empty {@code Stream}.
* If a value is present, returns an {@code Optional} describing the value,
* otherwise returns an {@code Optional} produced by the supplying function.
*
* @apiNote This method can be used to transform a {@code Stream} of
* optional elements to a {@code Stream} of present value elements:
* @param supplier the supplying function that produces an {@code Optional}
* to be returned
* @return returns an {@code Optional} describing the value of this
* {@code Optional}, if a value is present, otherwise an
* {@code Optional} produced by the supplying function.
* @throws NullPointerException if the supplying function is {@code null} or
* produces a {@code null} result
* @since 9
*/
public Optional<T> or(Supplier<Optional<T>> supplier) {
Objects.requireNonNull(supplier);
if (isPresent()) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
/**
* If a value is present, returns a sequential {@link Stream} containing
* only that value, otherwise returns an empty {@code Stream}.
*
* @apiNote
* This method can be used to transform a {@code Stream} of optional
* elements to a {@code Stream} of present value elements:
* <pre>{@code
* Stream<Optional<T>> os = ..
* Stream<T> s = os.flatMap(Optional::stream)
* }</pre>
*
* @return the optional value as a {@code Stream}
* @since 1.9
* @since 9
*/
public Stream<T> stream() {
if (!isPresent()) {
@ -288,10 +319,11 @@ public final class Optional<T> {
}
/**
* Return the value if present, otherwise return {@code other}.
* If a value is present, returns the value, otherwise returns
* {@code other}.
*
* @param other the value to be returned if there is no value present, may
* be null
* @param other the value to be returned, if no value is present.
* May be {@code null}.
* @return the value, if present, otherwise {@code other}
*/
public T orElse(T other) {
@ -299,34 +331,35 @@ public final class Optional<T> {
}
/**
* Return the value if present, otherwise invoke {@code other} and return
* the result of that invocation.
* If a value is present, returns the value, otherwise returns the result
* produced by the supplying function.
*
* @param other a {@code Supplier} whose result is returned if no value
* is present
* @return the value if present otherwise the result of {@code other.get()}
* @throws NullPointerException if value is not present and {@code other} is
* null
* @param supplier the supplying function that produces a value to be returned
* @return the value, if present, otherwise the result produced by the
* supplying function
* @throws NullPointerException if no value is present and the supplying
* function is {@code null}
*/
public T orElseGet(Supplier<? extends T> other) {
return value != null ? value : other.get();
public T orElseGet(Supplier<? extends T> supplier) {
return value != null ? value : supplier.get();
}
/**
* Return the contained value, if present, otherwise throw an exception
* to be created by the provided supplier.
* If a value is present, returns the value, otherwise throws an exception
* produced by the exception supplying function.
*
* @apiNote A method reference to the exception constructor with an empty
* argument list can be used as the supplier. For example,
* @apiNote
* A method reference to the exception constructor with an empty argument
* list can be used as the supplier. For example,
* {@code IllegalStateException::new}
*
* @param <X> Type of the exception to be thrown
* @param exceptionSupplier The supplier which will return the exception to
* be thrown
* @return the present value
* @throws X if there is no value present
* @throws NullPointerException if no value is present and
* {@code exceptionSupplier} is null
* @param exceptionSupplier the supplying function that produces an
* exception to be thrown
* @return the value, if present
* @throws X if no value is present
* @throws NullPointerException if no value is present and the exception
* supplying function is {@code null}
*/
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (value != null) {
@ -337,8 +370,8 @@ public final class Optional<T> {
}
/**
* Indicates whether some other object is "equal to" this Optional. The
* other object is considered equal if:
* Indicates whether some other object is "equal to" this {@code Optional}.
* The other object is considered equal if:
* <ul>
* <li>it is also an {@code Optional} and;
* <li>both instances have no value present or;
@ -347,7 +380,7 @@ public final class Optional<T> {
*
* @param obj an object to be tested for equality
* @return {@code true} if the other object is "equal to" this object
* otherwise {@code false}
* otherwise {@code false}
*/
@Override
public boolean equals(Object obj) {
@ -364,10 +397,11 @@ public final class Optional<T> {
}
/**
* Returns the hash code value of the present value, if any, or 0 (zero) if
* no value is present.
* Returns the hash code of the value, if present, otherwise {@code 0}
* (zero) if no value is present.
*
* @return hash code value of the present value or 0 if no value is present
* @return hash code value of the present value or {@code 0} if no value is
* present
*/
@Override
public int hashCode() {
@ -375,13 +409,14 @@ public final class Optional<T> {
}
/**
* Returns a non-empty string representation of this Optional suitable for
* debugging. The exact presentation format is unspecified and may vary
* between implementations and versions.
* Returns a non-empty string representation of this {@code Optional}
* suitable for debugging. The exact presentation format is unspecified and
* may vary between implementations and versions.
*
* @implSpec If a value is present the result must include its string
* representation in the result. Empty and present Optionals must be
* unambiguously differentiable.
* @implSpec
* If a value is present the result must include its string representation
* in the result. Empty and present {@code Optional}s must be unambiguously
* differentiable.
*
* @return the string representation of this instance
*/

View File

@ -30,15 +30,15 @@ import java.util.function.Supplier;
import java.util.stream.DoubleStream;
/**
* A container object which may or may not contain a {@code double} value.
* If a value is present, {@code isPresent()} will return {@code true} and
* {@code getAsDouble()} will return the value.
* A container object which may or may not contain a {@code double} value. If a
* value is present, {@code isPresent()} returns {@code true} and
* {@code getAsDouble()} returns the value.
*
* <p>Additional methods that depend on the presence or absence of a contained
* value are provided, such as {@link #orElse(double) orElse()}
* (return a default value if value not present) and
* {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (perform an
* action if the value is present).
* (returns a default value if no value is present) and
* {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (performs
* an action if a value is present).
*
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
* class; use of identity-sensitive operations (including reference equality
@ -71,12 +71,13 @@ public final class OptionalDouble {
}
/**
* Returns an empty {@code OptionalDouble} instance. No value is present for this
* OptionalDouble.
* Returns an empty {@code OptionalDouble} instance. No value is present
* for this {@code OptionalDouble}.
*
* @apiNote Though it may be tempting to do so, avoid testing if an object
* is empty by comparing with {@code ==} against instances returned by
* {@code Option.empty()}. There is no guarantee that it is a singleton.
* @apiNote
* Though it may be tempting to do so, avoid testing if an object is empty
* by comparing with {@code ==} against instances returned by
* {@code OptionalDouble.empty()}. There is no guarantee that it is a singleton.
* Instead, use {@link #isPresent()}.
*
* @return an empty {@code OptionalDouble}.
@ -86,9 +87,9 @@ public final class OptionalDouble {
}
/**
* Construct an instance with the value present.
* Construct an instance with the described value.
*
* @param value the double value to be present.
* @param value the double value to describe.
*/
private OptionalDouble(double value) {
this.isPresent = true;
@ -96,9 +97,9 @@ public final class OptionalDouble {
}
/**
* Return an {@code OptionalDouble} with the specified value present.
* Returns an {@code OptionalDouble} describing the given value.
*
* @param value the value to be present
* @param value the value to describe
* @return an {@code OptionalDouble} with the value present
*/
public static OptionalDouble of(double value) {
@ -106,12 +107,11 @@ public final class OptionalDouble {
}
/**
* If a value is present in this {@code OptionalDouble}, returns the value,
* otherwise throws {@code NoSuchElementException}.
*
* @return the value held by this {@code OptionalDouble}
* @throws NoSuchElementException if there is no value present
* If a value is present, returns the value, otherwise throws
* {@code NoSuchElementException}.
*
* @return the value described by this {@code OptionalDouble}
* @throws NoSuchElementException if no value is present
* @see OptionalDouble#isPresent()
*/
public double getAsDouble() {
@ -122,21 +122,21 @@ public final class OptionalDouble {
}
/**
* Return {@code true} if there is a value present, otherwise {@code false}.
* If a value is present, returns {@code true}, otherwise {@code false}.
*
* @return {@code true} if there is a value present, otherwise {@code false}
* @return {@code true} if a value is present, otherwise {@code false}
*/
public boolean isPresent() {
return isPresent;
}
/**
* If a value is present, perform the given action with the value,
* otherwise do nothing.
* If a value is present, performs the given action with the value,
* otherwise does nothing.
*
* @param action the action to be performed if a value is present
* @throws NullPointerException if a value is present and {@code action} is
* null
* @param action the action to be performed, if a value is present
* @throws NullPointerException if value is present and the given action is
* {@code null}
*/
public void ifPresent(DoubleConsumer action) {
if (isPresent) {
@ -145,15 +145,16 @@ public final class OptionalDouble {
}
/**
* If a value is present, perform the given action with the value,
* otherwise perform the given empty-based action.
* If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* @param action the action to be performed if a value is present
* @param emptyAction the empty-based action to be performed if a value is
* not present
* @throws NullPointerException if a value is present and {@code action} is
* null, or a value is not present and {@code emptyAction} is null.
* @since 1.9
* @param action the action to be performed, if a value is present
* @param emptyAction the empty-based action to be performed, if no value is
* present
* @throws NullPointerException if a value is present and the given action
* is {@code null}, or no value is present and the given empty-based
* action is {@code null}.
* @since 9
*/
public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
if (isPresent) {
@ -164,19 +165,20 @@ public final class OptionalDouble {
}
/**
* If a value is present return a sequential {@link DoubleStream} containing
* only that value, otherwise return an empty {@code DoubleStream}.
*
* @apiNote This method can be used to transform a {@code Stream} of
* optional doubles to a {@code DoubleStream} of present doubles:
* If a value is present, returns a sequential {@link DoubleStream}
* containing only that value, otherwise returns an empty
* {@code DoubleStream}.
*
* @apiNote
* This method can be used to transform a {@code Stream} of optional doubles
* to a {@code DoubleStream} of present doubles:
* <pre>{@code
* Stream<OptionalDouble> os = ..
* DoubleStream s = os.flatMapToDouble(OptionalDouble::stream)
* }</pre>
*
* @return the optional value as a {@code DoubleStream}
* @since 1.9
* @since 9
*/
public DoubleStream stream() {
if (isPresent) {
@ -187,9 +189,10 @@ public final class OptionalDouble {
}
/**
* Return the value if present, otherwise return {@code other}.
* If a value is present, returns the value, otherwise returns
* {@code other}.
*
* @param other the value to be returned if there is no value present
* @param other the value to be returned, if no value is present
* @return the value, if present, otherwise {@code other}
*/
public double orElse(double other) {
@ -197,34 +200,35 @@ public final class OptionalDouble {
}
/**
* Return the value if present, otherwise invoke {@code other} and return
* the result of that invocation.
* If a value is present, returns the value, otherwise returns the result
* produced by the supplying function.
*
* @param other a {@code DoubleSupplier} whose result is returned if no value
* is present
* @return the value if present otherwise the result of {@code other.getAsDouble()}
* @throws NullPointerException if value is not present and {@code other} is
* null
* @param supplier the supplying function that produces a value to be returned
* @return the value, if present, otherwise the result produced by the
* supplying function
* @throws NullPointerException if no value is present and the supplying
* function is {@code null}
*/
public double orElseGet(DoubleSupplier other) {
return isPresent ? value : other.getAsDouble();
public double orElseGet(DoubleSupplier supplier) {
return isPresent ? value : supplier.getAsDouble();
}
/**
* Return the contained value, if present, otherwise throw an exception
* to be created by the provided supplier.
* If a value is present, returns the value, otherwise throws an exception
* produced by the exception supplying function.
*
* @apiNote A method reference to the exception constructor with an empty
* argument list can be used as the supplier. For example,
* @apiNote
* A method reference to the exception constructor with an empty argument
* list can be used as the supplier. For example,
* {@code IllegalStateException::new}
*
* @param <X> Type of the exception to be thrown
* @param exceptionSupplier The supplier which will return the exception to
* be thrown
* @return the present value
* @throws X if there is no value present
* @throws NullPointerException if no value is present and
* {@code exceptionSupplier} is null
* @param exceptionSupplier the supplying function that produces an
* exception to be thrown
* @return the value, if present
* @throws X if no value is present
* @throws NullPointerException if no value is present and the exception
* supplying function is {@code null}
*/
public<X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (isPresent) {
@ -235,17 +239,18 @@ public final class OptionalDouble {
}
/**
* Indicates whether some other object is "equal to" this OptionalDouble. The
* other object is considered equal if:
* Indicates whether some other object is "equal to" this
* {@code OptionalDouble}. The other object is considered equal if:
* <ul>
* <li>it is also an {@code OptionalDouble} and;
* <li>both instances have no value present or;
* <li>the present values are "equal to" each other via {@code Double.compare() == 0}.
* <li>the present values are "equal to" each other via
* {@code Double.compare() == 0}.
* </ul>
*
* @param obj an object to be tested for equality
* @return {@code true} if the other object is "equal to" this object
* otherwise {@code false}
* otherwise {@code false}
*/
@Override
public boolean equals(Object obj) {
@ -264,10 +269,11 @@ public final class OptionalDouble {
}
/**
* Returns the hash code value of the present value, if any, or 0 (zero) if
* no value is present.
* Returns the hash code of the value, if present, otherwise {@code 0}
* (zero) if no value is present.
*
* @return hash code value of the present value or 0 if no value is present
* @return hash code value of the present value or {@code 0} if no value is
* present
*/
@Override
public int hashCode() {
@ -275,14 +281,13 @@ public final class OptionalDouble {
}
/**
* {@inheritDoc}
* Returns a non-empty string representation of this {@code OptionalDouble}
* suitable for debugging. The exact presentation format is unspecified and
* may vary between implementations and versions.
*
* Returns a non-empty string representation of this object suitable for
* debugging. The exact presentation format is unspecified and may vary
* between implementations and versions.
*
* @implSpec If a value is present the result must include its string
* representation in the result. Empty and present instances must be
* @implSpec
* If a value is present the result must include its string representation
* in the result. Empty and present {@code OptionalDouble}s must be
* unambiguously differentiable.
*
* @return the string representation of this instance

View File

@ -30,15 +30,15 @@ import java.util.function.Supplier;
import java.util.stream.IntStream;
/**
* A container object which may or may not contain a {@code int} value.
* If a value is present, {@code isPresent()} will return {@code true} and
* {@code getAsInt()} will return the value.
* A container object which may or may not contain an {@code int} value. If a
* value is present, {@code isPresent()} returns {@code true} and
* {@code getAsInt()} returns the value.
*
* <p>Additional methods that depend on the presence or absence of a contained
* value are provided, such as {@link #orElse(int) orElse()}
* (return a default value if value not present) and
* {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (perform an
* action if the value is present).
* (returns a default value if no value is present) and
* {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (performs an
* action if a value is present).
*
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
* class; use of identity-sensitive operations (including reference equality
@ -71,24 +71,25 @@ public final class OptionalInt {
}
/**
* Returns an empty {@code OptionalInt} instance. No value is present for this
* OptionalInt.
* Returns an empty {@code OptionalInt} instance. No value is present for
* this {@code OptionalInt}.
*
* @apiNote Though it may be tempting to do so, avoid testing if an object
* is empty by comparing with {@code ==} against instances returned by
* {@code Option.empty()}. There is no guarantee that it is a singleton.
* @apiNote
* Though it may be tempting to do so, avoid testing if an object is empty
* by comparing with {@code ==} against instances returned by
* {@code OptionalInt.empty()}. There is no guarantee that it is a singleton.
* Instead, use {@link #isPresent()}.
*
* @return an empty {@code OptionalInt}
* @return an empty {@code OptionalInt}
*/
public static OptionalInt empty() {
return EMPTY;
}
/**
* Construct an instance with the value present.
* Construct an instance with the described value.
*
* @param value the int value to be present
* @param value the int value to describe
*/
private OptionalInt(int value) {
this.isPresent = true;
@ -96,9 +97,9 @@ public final class OptionalInt {
}
/**
* Return an {@code OptionalInt} with the specified value present.
* Returns an {@code OptionalInt} describing the given value.
*
* @param value the value to be present
* @param value the value to describe
* @return an {@code OptionalInt} with the value present
*/
public static OptionalInt of(int value) {
@ -106,12 +107,11 @@ public final class OptionalInt {
}
/**
* If a value is present in this {@code OptionalInt}, returns the value,
* otherwise throws {@code NoSuchElementException}.
*
* @return the value held by this {@code OptionalInt}
* @throws NoSuchElementException if there is no value present
* If a value is present, returns the value, otherwise throws
* {@code NoSuchElementException}.
*
* @return the value described by this {@code OptionalInt}
* @throws NoSuchElementException if no value is present
* @see OptionalInt#isPresent()
*/
public int getAsInt() {
@ -122,21 +122,21 @@ public final class OptionalInt {
}
/**
* Return {@code true} if there is a value present, otherwise {@code false}.
* If a value is present, returns {@code true}, otherwise {@code false}.
*
* @return {@code true} if there is a value present, otherwise {@code false}
* @return {@code true} if a value is present, otherwise {@code false}
*/
public boolean isPresent() {
return isPresent;
}
/**
* If a value is present, perform the given action with the value,
* otherwise do nothing.
* If a value is present, performs the given action with the value,
* otherwise does nothing.
*
* @param action the action to be performed if a value is present
* @throws NullPointerException if value is present and {@code action} is
* null
* @param action the action to be performed, if a value is present
* @throws NullPointerException if value is present and the given action is
* {@code null}
*/
public void ifPresent(IntConsumer action) {
if (isPresent) {
@ -145,15 +145,16 @@ public final class OptionalInt {
}
/**
* If a value is present, perform the given action with the value,
* otherwise perform the given empty-based action.
* If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* @param action the action to be performed if a value is present
* @param emptyAction the empty-based action to be performed if a value is
* not present
* @throws NullPointerException if a value is present and {@code action} is
* null, or a value is not present and {@code emptyAction} is null.
* @since 1.9
* @param action the action to be performed, if a value is present
* @param emptyAction the empty-based action to be performed, if no value is
* present
* @throws NullPointerException if a value is present and the given action
* is {@code null}, or no value is present and the given empty-based
* action is {@code null}.
* @since 9
*/
public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) {
if (isPresent) {
@ -164,19 +165,19 @@ public final class OptionalInt {
}
/**
* If a value is present return a sequential {@link IntStream} containing
* only that value, otherwise return an empty {@code IntStream}.
*
* @apiNote This method can be used to transform a {@code Stream} of
* optional integers to an {@code IntStream} of present integers:
* If a value is present, returns a sequential {@link IntStream} containing
* only that value, otherwise returns an empty {@code IntStream}.
*
* @apiNote
* This method can be used to transform a {@code Stream} of optional
* integers to an {@code IntStream} of present integers:
* <pre>{@code
* Stream<OptionalInt> os = ..
* IntStream s = os.flatMapToInt(OptionalInt::stream)
* }</pre>
*
* @return the optional value as an {@code IntStream}
* @since 1.9
* @since 9
*/
public IntStream stream() {
if (isPresent) {
@ -187,9 +188,10 @@ public final class OptionalInt {
}
/**
* Return the value if present, otherwise return {@code other}.
* If a value is present, returns the value, otherwise returns
* {@code other}.
*
* @param other the value to be returned if there is no value present
* @param other the value to be returned, if no value is present
* @return the value, if present, otherwise {@code other}
*/
public int orElse(int other) {
@ -197,34 +199,35 @@ public final class OptionalInt {
}
/**
* Return the value if present, otherwise invoke {@code other} and return
* the result of that invocation.
* If a value is present, returns the value, otherwise returns the result
* produced by the supplying function.
*
* @param other a {@code IntSupplier} whose result is returned if no value
* is present
* @return the value if present otherwise the result of {@code other.getAsInt()}
* @throws NullPointerException if value is not present and {@code other} is
* null
* @param supplier the supplying function that produces a value to be returned
* @return the value, if present, otherwise the result produced by the
* supplying function
* @throws NullPointerException if no value is present and the supplying
* function is {@code null}
*/
public int orElseGet(IntSupplier other) {
return isPresent ? value : other.getAsInt();
public int orElseGet(IntSupplier supplier) {
return isPresent ? value : supplier.getAsInt();
}
/**
* Return the contained value, if present, otherwise throw an exception
* to be created by the provided supplier.
* If a value is present, returns the value, otherwise throws an exception
* produced by the exception supplying function.
*
* @apiNote A method reference to the exception constructor with an empty
* argument list can be used as the supplier. For example,
* @apiNote
* A method reference to the exception constructor with an empty argument
* list can be used as the supplier. For example,
* {@code IllegalStateException::new}
*
* @param <X> Type of the exception to be thrown
* @param exceptionSupplier The supplier which will return the exception to
* be thrown
* @return the present value
* @throws X if there is no value present
* @throws NullPointerException if no value is present and
* {@code exceptionSupplier} is null
* @param exceptionSupplier the supplying function that produces an
* exception to be thrown
* @return the value, if present
* @throws X if no value is present
* @throws NullPointerException if no value is present and the exception
* supplying function is {@code null}
*/
public<X extends Throwable> int orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (isPresent) {
@ -235,8 +238,8 @@ public final class OptionalInt {
}
/**
* Indicates whether some other object is "equal to" this OptionalInt. The
* other object is considered equal if:
* Indicates whether some other object is "equal to" this
* {@code OptionalInt}. The other object is considered equal if:
* <ul>
* <li>it is also an {@code OptionalInt} and;
* <li>both instances have no value present or;
@ -245,7 +248,7 @@ public final class OptionalInt {
*
* @param obj an object to be tested for equality
* @return {@code true} if the other object is "equal to" this object
* otherwise {@code false}
* otherwise {@code false}
*/
@Override
public boolean equals(Object obj) {
@ -264,10 +267,11 @@ public final class OptionalInt {
}
/**
* Returns the hash code value of the present value, if any, or 0 (zero) if
* no value is present.
* Returns the hash code of the value, if present, otherwise {@code 0}
* (zero) if no value is present.
*
* @return hash code value of the present value or 0 if no value is present
* @return hash code value of the present value or {@code 0} if no value is
* present
*/
@Override
public int hashCode() {
@ -275,14 +279,13 @@ public final class OptionalInt {
}
/**
* {@inheritDoc}
* Returns a non-empty string representation of this {@code OptionalInt}
* suitable for debugging. The exact presentation format is unspecified and
* may vary between implementations and versions.
*
* Returns a non-empty string representation of this object suitable for
* debugging. The exact presentation format is unspecified and may vary
* between implementations and versions.
*
* @implSpec If a value is present the result must include its string
* representation in the result. Empty and present instances must be
* @implSpec
* If a value is present the result must include its string representation
* in the result. Empty and present {@code OptionalInt}s must be
* unambiguously differentiable.
*
* @return the string representation of this instance

View File

@ -30,15 +30,15 @@ import java.util.function.Supplier;
import java.util.stream.LongStream;
/**
* A container object which may or may not contain a {@code long} value.
* If a value is present, {@code isPresent()} will return {@code true} and
* {@code getAsLong()} will return the value.
* A container object which may or may not contain a {@code long} value. If a
* value is present, {@code isPresent()} returns {@code true} and
* {@code getAsLong()} returns the value.
*
* <p>Additional methods that depend on the presence or absence of a contained
* value are provided, such as {@link #orElse(long) orElse()}
* (return a default value if value not present) and
* {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (perform an
* action if the value is present).
* (returns a default value if no value is present) and
* {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (performs an
* action if a value is present).
*
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
* class; use of identity-sensitive operations (including reference equality
@ -71,24 +71,25 @@ public final class OptionalLong {
}
/**
* Returns an empty {@code OptionalLong} instance. No value is present for this
* OptionalLong.
* Returns an empty {@code OptionalLong} instance. No value is present for
* this {@code OptionalLong}.
*
* @apiNote Though it may be tempting to do so, avoid testing if an object
* is empty by comparing with {@code ==} against instances returned by
* {@code Option.empty()}. There is no guarantee that it is a singleton.
* @apiNote
* Though it may be tempting to do so, avoid testing if an object is empty
* by comparing with {@code ==} against instances returned by
* {@code OptionalLong.empty()}. There is no guarantee that it is a singleton.
* Instead, use {@link #isPresent()}.
*
* @return an empty {@code OptionalLong}.
* @return an empty {@code OptionalLong}.
*/
public static OptionalLong empty() {
return EMPTY;
}
/**
* Construct an instance with the value present.
* Construct an instance with the described value.
*
* @param value the long value to be present
* @param value the long value to describe
*/
private OptionalLong(long value) {
this.isPresent = true;
@ -96,9 +97,9 @@ public final class OptionalLong {
}
/**
* Return an {@code OptionalLong} with the specified value present.
* Returns an {@code OptionalLong} describing the given value.
*
* @param value the value to be present
* @param value the value to describe
* @return an {@code OptionalLong} with the value present
*/
public static OptionalLong of(long value) {
@ -106,12 +107,11 @@ public final class OptionalLong {
}
/**
* If a value is present in this {@code OptionalLong}, returns the value,
* otherwise throws {@code NoSuchElementException}.
*
* @return the value held by this {@code OptionalLong}
* @throws NoSuchElementException if there is no value present
* If a value is present, returns the value, otherwise throws
* {@code NoSuchElementException}.
*
* @return the value described by this {@code OptionalLong}
* @throws NoSuchElementException if no value is present
* @see OptionalLong#isPresent()
*/
public long getAsLong() {
@ -122,21 +122,21 @@ public final class OptionalLong {
}
/**
* Return {@code true} if there is a value present, otherwise {@code false}.
* If a value is present, returns {@code true}, otherwise {@code false}.
*
* @return {@code true} if there is a value present, otherwise {@code false}
* @return {@code true} if a value is present, otherwise {@code false}
*/
public boolean isPresent() {
return isPresent;
}
/**
* If a value is present, perform the given action with the value,
* otherwise do nothing.
* If a value is present, performs the given action with the value,
* otherwise does nothing.
*
* @param action the action to be performed if a value is present
* @throws NullPointerException if a value is present and {@code action} is
* null
* @param action the action to be performed, if a value is present
* @throws NullPointerException if value is present and the given action is
* {@code null}
*/
public void ifPresent(LongConsumer action) {
if (isPresent) {
@ -145,15 +145,16 @@ public final class OptionalLong {
}
/**
* If a value is present, perform the given action with the value,
* otherwise perform the given empty-based action.
* If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* @param action the action to be performed if a value is present
* @param emptyAction the empty-based action to be performed if a value is
* not present
* @throws NullPointerException if a value is present and {@code action} is
* null, or a value is not present and {@code emptyAction} is null.
* @since 1.9
* @param action the action to be performed, if a value is present
* @param emptyAction the empty-based action to be performed, if no value is
* present
* @throws NullPointerException if a value is present and the given action
* is {@code null}, or no value is present and the given empty-based
* action is {@code null}.
* @since 9
*/
public void ifPresentOrElse(LongConsumer action, Runnable emptyAction) {
if (isPresent) {
@ -164,19 +165,19 @@ public final class OptionalLong {
}
/**
* If a value is present return a sequential {@link LongStream} containing
* only that value, otherwise return an empty {@code LongStream}.
*
* @apiNote This method can be used to transform a {@code Stream} of
* optional longs to a {@code LongStream} of present longs:
* If a value is present, returns a sequential {@link LongStream} containing
* only that value, otherwise returns an empty {@code LongStream}.
*
* @apiNote
* This method can be used to transform a {@code Stream} of optional longs
* to an {@code LongStream} of present longs:
* <pre>{@code
* Stream<OptionalLong> os = ..
* LongStream s = os.flatMapToLong(OptionalLong::stream)
* }</pre>
*
* @return the optional value as a {@code LongStream}
* @since 1.9
* @return the optional value as an {@code LongStream}
* @since 9
*/
public LongStream stream() {
if (isPresent) {
@ -187,9 +188,10 @@ public final class OptionalLong {
}
/**
* Return the value if present, otherwise return {@code other}.
* If a value is present, returns the value, otherwise returns
* {@code other}.
*
* @param other the value to be returned if there is no value present
* @param other the value to be returned, if no value is present
* @return the value, if present, otherwise {@code other}
*/
public long orElse(long other) {
@ -197,34 +199,35 @@ public final class OptionalLong {
}
/**
* Return the value if present, otherwise invoke {@code other} and return
* the result of that invocation.
* If a value is present, returns the value, otherwise returns the result
* produced by the supplying function.
*
* @param other a {@code LongSupplier} whose result is returned if no value
* is present
* @return the value if present otherwise the result of {@code other.getAsLong()}
* @throws NullPointerException if value is not present and {@code other} is
* null
* @param supplier the supplying function that produces a value to be returned
* @return the value, if present, otherwise the result produced by the
* supplying function
* @throws NullPointerException if no value is present and the supplying
* function is {@code null}
*/
public long orElseGet(LongSupplier other) {
return isPresent ? value : other.getAsLong();
public long orElseGet(LongSupplier supplier) {
return isPresent ? value : supplier.getAsLong();
}
/**
* Return the contained value, if present, otherwise throw an exception
* to be created by the provided supplier.
* If a value is present, returns the value, otherwise throws an exception
* produced by the exception supplying function.
*
* @apiNote A method reference to the exception constructor with an empty
* argument list can be used as the supplier. For example,
* @apiNote
* A method reference to the exception constructor with an empty argument
* list can be used as the supplier. For example,
* {@code IllegalStateException::new}
*
* @param <X> Type of the exception to be thrown
* @param exceptionSupplier The supplier which will return the exception to
* be thrown
* @return the present value
* @throws X if there is no value present
* @throws NullPointerException if no value is present and
* {@code exceptionSupplier} is null
* @param exceptionSupplier the supplying function that produces an
* exception to be thrown
* @return the value, if present
* @throws X if no value is present
* @throws NullPointerException if no value is present and the exception
* supplying function is {@code null}
*/
public<X extends Throwable> long orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (isPresent) {
@ -235,8 +238,8 @@ public final class OptionalLong {
}
/**
* Indicates whether some other object is "equal to" this OptionalLong. The
* other object is considered equal if:
* Indicates whether some other object is "equal to" this
* {@code OptionalLong}. The other object is considered equal if:
* <ul>
* <li>it is also an {@code OptionalLong} and;
* <li>both instances have no value present or;
@ -245,7 +248,7 @@ public final class OptionalLong {
*
* @param obj an object to be tested for equality
* @return {@code true} if the other object is "equal to" this object
* otherwise {@code false}
* otherwise {@code false}
*/
@Override
public boolean equals(Object obj) {
@ -264,10 +267,11 @@ public final class OptionalLong {
}
/**
* Returns the hash code value of the present value, if any, or 0 (zero) if
* no value is present.
* Returns the hash code of the value, if present, otherwise {@code 0}
* (zero) if no value is present.
*
* @return hash code value of the present value or 0 if no value is present
* @return hash code value of the present value or {@code 0} if no value is
* present
*/
@Override
public int hashCode() {
@ -275,14 +279,13 @@ public final class OptionalLong {
}
/**
* {@inheritDoc}
* Returns a non-empty string representation of this {@code OptionalLong}
* suitable for debugging. The exact presentation format is unspecified and
* may vary between implementations and versions.
*
* Returns a non-empty string representation of this object suitable for
* debugging. The exact presentation format is unspecified and may vary
* between implementations and versions.
*
* @implSpec If a value is present the result must include its string
* representation in the result. Empty and present instances must be
* @implSpec
* If a value is present the result must include its string representation
* in the result. Empty and present {@code OptionalLong}s must be
* unambiguously differentiable.
*
* @return the string representation of this instance

View File

@ -438,10 +438,19 @@ class JarFile extends ZipFile {
private byte[] getBytes(ZipEntry ze) throws IOException {
try (InputStream is = super.getInputStream(ze)) {
int len = (int)ze.getSize();
byte[] b = is.readAllBytes();
if (len != -1 && b.length != len)
throw new EOFException("Expected:" + len + ", read:" + b.length);
int bytesRead;
byte[] b;
// trust specified entry sizes when reasonably small
if (len != -1 && len <= 65535) {
b = new byte[len];
bytesRead = is.readNBytes(b, 0, len);
} else {
b = is.readAllBytes();
bytesRead = b.length;
}
if (len != -1 && len != bytesRead) {
throw new EOFException("Expected:" + len + ", read:" + bytesRead);
}
return b;
}
}

View File

@ -350,8 +350,6 @@ final class CipherSuite implements Comparable<CipherSuite> {
private static void add(String name, int id, int priority,
KeyExchange keyExchange, BulkCipher cipher, MacAlg mac,
boolean allowed, int obsoleted) {
// If this is an obsoleted suite, then don't let the TLS 1.2
// protocol have a valid PRF value.
PRF prf = obsoleted < ProtocolVersion.TLS12.v ? P_NONE : P_SHA256;
add(name, id, priority, keyExchange, cipher, mac, allowed, obsoleted,

View File

@ -293,6 +293,46 @@ public class Basic {
assertSame(l, fixture);
}
@Test(groups = "unit")
public void testOr() {
Optional<String> empty = Optional.empty();
Optional<String> duke = Optional.of("Duke");
// Null supplier
try {
Optional<String> b = empty.or(null);
fail("Should throw NPE on null supplier");
} catch (NullPointerException npe) {
// expected
}
// Supply null
try {
Optional<String> b = empty.or(() -> null);
fail("Should throw NPE when supplier returns null");
} catch (NullPointerException npe) {
// expected
}
// Non-empty won't invoke supplier
try {
Optional<String> b = duke.or(() -> null);
assertTrue(b.isPresent());
} catch (NullPointerException npe) {
fail("Supplier should not be invoked");
}
// Supply for empty
Optional<String> suppliedDuke = empty.or(() -> duke);
assertTrue(suppliedDuke.isPresent());
assertSame(suppliedDuke, duke);
// Supply for non-empty
Optional<String> actualDuke = duke.or(() -> Optional.of("Other Duke"));
assertTrue(actualDuke.isPresent());
assertSame(actualDuke, duke);
}
@Test(groups = "unit")
public void testStream() {
{

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import javax.xml.bind.annotation.XmlType;
@XmlType
public class InputWithError {
public int a;
int compile-error;
}

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8073519
* @summary test that schemagen tool reports errors during
* xsd generation process
* @library /lib/testlibrary
* @run testng/othervm SchemagenErrorReporting
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.stream.Collectors;
import jdk.testlibrary.JDKToolLauncher;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SchemagenErrorReporting {
@Test
public void schemagenErrorReporting() throws Exception {
//schemagen tool output file name
final String SCHEMA_FILE = "schema1.xsd";
//Schemagen input java file with not compilable source
final String CLASS_FILE = "InputWithError.java";
//Test working, src directories and test output file
Path testWorkDir, testSrcDir, testOutput;
//Prepare test environment
//Create test directory inside scratch
testWorkDir = Paths.get(System.getProperty("user.dir", "."))
.resolve("SchemagenErrorReporting");
//Get test source directory
testSrcDir = Paths.get(System.getProperty("test.src", "."));
//Set test output file path
testOutput = testWorkDir.resolve("stdErrContent");
//Create test directory inside scratch directory
Files.createDirectory(testWorkDir);
//Copy java source from test.src to the test directory
Files.copy(testSrcDir.resolve(CLASS_FILE), testWorkDir.resolve(CLASS_FILE),
StandardCopyOption.REPLACE_EXISTING);
//Prepare process builder to run schemagen tool and save its output
JDKToolLauncher sgl = JDKToolLauncher.createUsingTestJDK("schemagen");
sgl.addToolArg(CLASS_FILE);
System.out.println("Executing: " + Arrays.asList(sgl.getCommand()));
ProcessBuilder pb = new ProcessBuilder(sgl.getCommand());
//Set schemagen work directory with the input java file
pb.directory(testWorkDir.toFile());
//Redirect schemagen output to file
pb.redirectError(testOutput.toFile());
Process p = pb.start();
int result = p.waitFor();
p.destroy();
//Read schemagen output from the file
String stdErrContent = Files.lines(testOutput)
.collect(Collectors.joining(System.lineSeparator(), System.lineSeparator(), ""));
System.out.println("Schemagen return value:" + result);
System.out.println("Error output:" + stdErrContent);
//Check test results:
//Schemagen finished with non-0 return value
Assert.assertNotEquals(result, 0);
//Schemagen output contains compile error message
Assert.assertTrue(stdErrContent.contains("InputWithError.java:28: error"));
}
}

View File

@ -328,3 +328,4 @@ d68904d5a00e7e6c714e11f7ec9d0689af959d79 jdk9-b82
6b3b94a2ebca4a99b2effefb2a1556cee954fd2f jdk9-b83
e3445ccab58f741801021dec9aa46e7f2c09efd9 jdk9-b84
7ef2c66892a3af15540c2800104c660c4f7f45e9 jdk9-b85
130a7c2a85900dde04e119bc36853b73146e3414 jdk9-b86

View File

@ -319,3 +319,4 @@ ab231613d7206431ba31917a02e7cedd70e88e70 jdk9-b76
21b86b980a5f0d27f1f758a3e4818d3331387172 jdk9-b83
214b97ba911f4d768c0214098739e32ab54c8503 jdk9-b84
285628dac94332d95979de365630c93ab8fa9962 jdk9-b85
e4283eeb182cbdf5004740728a404aac6afa1104 jdk9-b86