8071670: java.util.Optional: please add a way to specify if-else behavior
Reviewed-by: dfuchs, lowasser
This commit is contained in:
parent
3cb54779b0
commit
4b2af9acf9
@ -38,8 +38,8 @@ import java.util.stream.Stream;
|
|||||||
* <p>Additional methods that depend on the presence or absence of a contained
|
* <p>Additional methods that depend on the presence or absence of a contained
|
||||||
* value are provided, such as {@link #orElse(java.lang.Object) orElse()}
|
* value are provided, such as {@link #orElse(java.lang.Object) orElse()}
|
||||||
* (return a default value if value not present) and
|
* (return a default value if value not present) and
|
||||||
* {@link #ifPresent(java.util.function.Consumer) ifPresent()} (execute a block
|
* {@link #ifPresent(java.util.function.Consumer) ifPresent()} (perform an
|
||||||
* of code if the value is present).
|
* action if the value is present).
|
||||||
*
|
*
|
||||||
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
|
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
|
||||||
* class; use of identity-sensitive operations (including reference equality
|
* class; use of identity-sensitive operations (including reference equality
|
||||||
@ -148,16 +148,35 @@ public final class Optional<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If a value is present, invoke the specified consumer with the value,
|
* If a value is present, perform the given action with the value,
|
||||||
* otherwise do nothing.
|
* otherwise do nothing.
|
||||||
*
|
*
|
||||||
* @param consumer block to be executed if a value is present
|
* @param action the action to be performed if a value is present
|
||||||
* @throws NullPointerException if value is present and {@code consumer} is
|
* @throws NullPointerException if a value is present and {@code action} is
|
||||||
* null
|
* null
|
||||||
*/
|
*/
|
||||||
public void ifPresent(Consumer<? super T> consumer) {
|
public void ifPresent(Consumer<? super T> action) {
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
consumer.accept(value);
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a value is present, perform the given action with the value,
|
||||||
|
* otherwise perform 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
|
||||||
|
*/
|
||||||
|
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
|
||||||
|
if (value != null) {
|
||||||
|
action.accept(value);
|
||||||
|
} else {
|
||||||
|
emptyAction.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,8 +37,8 @@ import java.util.stream.DoubleStream;
|
|||||||
* <p>Additional methods that depend on the presence or absence of a contained
|
* <p>Additional methods that depend on the presence or absence of a contained
|
||||||
* value are provided, such as {@link #orElse(double) orElse()}
|
* value are provided, such as {@link #orElse(double) orElse()}
|
||||||
* (return a default value if value not present) and
|
* (return a default value if value not present) and
|
||||||
* {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (execute a block
|
* {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (perform an
|
||||||
* of code if the value is present).
|
* action if the value is present).
|
||||||
*
|
*
|
||||||
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
|
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
|
||||||
* class; use of identity-sensitive operations (including reference equality
|
* class; use of identity-sensitive operations (including reference equality
|
||||||
@ -131,16 +131,35 @@ public final class OptionalDouble {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Have the specified consumer accept the value if a value is present,
|
* If a value is present, perform the given action with the value,
|
||||||
* otherwise do nothing.
|
* otherwise do nothing.
|
||||||
*
|
*
|
||||||
* @param consumer block to be executed if a value is present
|
* @param action the action to be performed if a value is present
|
||||||
* @throws NullPointerException if value is present and {@code consumer} is
|
* @throws NullPointerException if a value is present and {@code action} is
|
||||||
* null
|
* null
|
||||||
*/
|
*/
|
||||||
public void ifPresent(DoubleConsumer consumer) {
|
public void ifPresent(DoubleConsumer action) {
|
||||||
if (isPresent) {
|
if (isPresent) {
|
||||||
consumer.accept(value);
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a value is present, perform the given action with the value,
|
||||||
|
* otherwise perform 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
|
||||||
|
*/
|
||||||
|
public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
|
||||||
|
if (isPresent) {
|
||||||
|
action.accept(value);
|
||||||
|
} else {
|
||||||
|
emptyAction.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,8 +37,8 @@ import java.util.stream.IntStream;
|
|||||||
* <p>Additional methods that depend on the presence or absence of a contained
|
* <p>Additional methods that depend on the presence or absence of a contained
|
||||||
* value are provided, such as {@link #orElse(int) orElse()}
|
* value are provided, such as {@link #orElse(int) orElse()}
|
||||||
* (return a default value if value not present) and
|
* (return a default value if value not present) and
|
||||||
* {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (execute a block
|
* {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (perform an
|
||||||
* of code if the value is present).
|
* action if the value is present).
|
||||||
*
|
*
|
||||||
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
|
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
|
||||||
* class; use of identity-sensitive operations (including reference equality
|
* class; use of identity-sensitive operations (including reference equality
|
||||||
@ -131,16 +131,35 @@ public final class OptionalInt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Have the specified consumer accept the value if a value is present,
|
* If a value is present, perform the given action with the value,
|
||||||
* otherwise do nothing.
|
* otherwise do nothing.
|
||||||
*
|
*
|
||||||
* @param consumer block to be executed if a value is present
|
* @param action the action to be performed if a value is present
|
||||||
* @throws NullPointerException if value is present and {@code consumer} is
|
* @throws NullPointerException if value is present and {@code action} is
|
||||||
* null
|
* null
|
||||||
*/
|
*/
|
||||||
public void ifPresent(IntConsumer consumer) {
|
public void ifPresent(IntConsumer action) {
|
||||||
if (isPresent) {
|
if (isPresent) {
|
||||||
consumer.accept(value);
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a value is present, perform the given action with the value,
|
||||||
|
* otherwise perform 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
|
||||||
|
*/
|
||||||
|
public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) {
|
||||||
|
if (isPresent) {
|
||||||
|
action.accept(value);
|
||||||
|
} else {
|
||||||
|
emptyAction.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,8 +37,8 @@ import java.util.stream.LongStream;
|
|||||||
* <p>Additional methods that depend on the presence or absence of a contained
|
* <p>Additional methods that depend on the presence or absence of a contained
|
||||||
* value are provided, such as {@link #orElse(long) orElse()}
|
* value are provided, such as {@link #orElse(long) orElse()}
|
||||||
* (return a default value if value not present) and
|
* (return a default value if value not present) and
|
||||||
* {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (execute a block
|
* {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (perform an
|
||||||
* of code if the value is present).
|
* action if the value is present).
|
||||||
*
|
*
|
||||||
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
|
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
|
||||||
* class; use of identity-sensitive operations (including reference equality
|
* class; use of identity-sensitive operations (including reference equality
|
||||||
@ -131,16 +131,35 @@ public final class OptionalLong {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Have the specified consumer accept the value if a value is present,
|
* If a value is present, perform the given action with the value,
|
||||||
* otherwise do nothing.
|
* otherwise do nothing.
|
||||||
*
|
*
|
||||||
* @param consumer block to be executed if a value is present
|
* @param action the action to be performed if a value is present
|
||||||
* @throws NullPointerException if value is present and {@code consumer} is
|
* @throws NullPointerException if a value is present and {@code action} is
|
||||||
* null
|
* null
|
||||||
*/
|
*/
|
||||||
public void ifPresent(LongConsumer consumer) {
|
public void ifPresent(LongConsumer action) {
|
||||||
if (isPresent) {
|
if (isPresent) {
|
||||||
consumer.accept(value);
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a value is present, perform the given action with the value,
|
||||||
|
* otherwise perform 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
|
||||||
|
*/
|
||||||
|
public void ifPresentOrElse(LongConsumer action, Runnable emptyAction) {
|
||||||
|
if (isPresent) {
|
||||||
|
action.accept(value);
|
||||||
|
} else {
|
||||||
|
emptyAction.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,8 +27,12 @@
|
|||||||
* @run testng Basic
|
* @run testng Basic
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.lang.AssertionError;
|
||||||
|
import java.lang.NullPointerException;
|
||||||
|
import java.lang.Throwable;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.testng.Assert.*;
|
import static org.testng.Assert.*;
|
||||||
@ -51,7 +55,23 @@ public class Basic {
|
|||||||
assertTrue(!empty.toString().isEmpty());
|
assertTrue(!empty.toString().isEmpty());
|
||||||
assertTrue(!empty.toString().equals(presentEmptyString.toString()));
|
assertTrue(!empty.toString().equals(presentEmptyString.toString()));
|
||||||
assertTrue(!empty.isPresent());
|
assertTrue(!empty.isPresent());
|
||||||
empty.ifPresent(v -> { fail(); });
|
|
||||||
|
empty.ifPresent(v -> fail());
|
||||||
|
|
||||||
|
AtomicBoolean emptyCheck = new AtomicBoolean();
|
||||||
|
empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
|
||||||
|
assertTrue(emptyCheck.get());
|
||||||
|
|
||||||
|
try {
|
||||||
|
empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
|
||||||
|
fail();
|
||||||
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
assertSame(null, empty.orElse(null));
|
assertSame(null, empty.orElse(null));
|
||||||
RuntimeException orElse = new RuntimeException() { };
|
RuntimeException orElse = new RuntimeException() { };
|
||||||
assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));
|
assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));
|
||||||
@ -59,6 +79,31 @@ public class Basic {
|
|||||||
assertSame(Boolean.FALSE, empty.orElseGet(() -> Boolean.FALSE));
|
assertSame(Boolean.FALSE, empty.orElseGet(() -> Boolean.FALSE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(groups = "unit")
|
||||||
|
public void testIfPresentAndOrElseAndNull() {
|
||||||
|
Optional<Boolean> empty = Optional.empty();
|
||||||
|
Optional<Boolean> present = Optional.of(Boolean.TRUE);
|
||||||
|
|
||||||
|
// No NPE
|
||||||
|
present.ifPresentOrElse(v -> {}, null);
|
||||||
|
empty.ifPresent(null);
|
||||||
|
empty.ifPresentOrElse(null, () -> {});
|
||||||
|
|
||||||
|
// NPE
|
||||||
|
try {
|
||||||
|
present.ifPresent(null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
try {
|
||||||
|
present.ifPresentOrElse(null, () -> {});
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
try {
|
||||||
|
empty.ifPresentOrElse(v -> {}, null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expectedExceptions=NoSuchElementException.class)
|
@Test(expectedExceptions=NoSuchElementException.class)
|
||||||
public void testEmptyGet() {
|
public void testEmptyGet() {
|
||||||
Optional<Boolean> empty = Optional.empty();
|
Optional<Boolean> empty = Optional.empty();
|
||||||
@ -102,12 +147,33 @@ public class Basic {
|
|||||||
assertTrue(!present.toString().equals(presentEmptyString.toString()));
|
assertTrue(!present.toString().equals(presentEmptyString.toString()));
|
||||||
assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
|
assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
|
||||||
assertSame(Boolean.TRUE, present.get());
|
assertSame(Boolean.TRUE, present.get());
|
||||||
|
|
||||||
|
AtomicBoolean presentCheck = new AtomicBoolean();
|
||||||
|
present.ifPresent(v -> presentCheck.set(true));
|
||||||
|
assertTrue(presentCheck.get());
|
||||||
|
presentCheck.set(false);
|
||||||
|
present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
|
||||||
|
assertTrue(presentCheck.get());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
present.ifPresent(v -> { throw new ObscureException(); });
|
present.ifPresent(v -> { throw new ObscureException(); });
|
||||||
fail();
|
fail();
|
||||||
} catch (ObscureException expected) {
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
|
||||||
|
fail();
|
||||||
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
assertSame(Boolean.TRUE, present.orElse(null));
|
assertSame(Boolean.TRUE, present.orElse(null));
|
||||||
assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
|
assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
|
||||||
assertSame(Boolean.TRUE, present.orElseGet(null));
|
assertSame(Boolean.TRUE, present.orElseGet(null));
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.OptionalDouble;
|
import java.util.OptionalDouble;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.stream.DoubleStream;
|
import java.util.stream.DoubleStream;
|
||||||
|
|
||||||
import static org.testng.Assert.*;
|
import static org.testng.Assert.*;
|
||||||
@ -49,11 +50,52 @@ public class BasicDouble {
|
|||||||
assertTrue(0 == empty.hashCode());
|
assertTrue(0 == empty.hashCode());
|
||||||
assertTrue(!empty.toString().isEmpty());
|
assertTrue(!empty.toString().isEmpty());
|
||||||
assertTrue(!empty.isPresent());
|
assertTrue(!empty.isPresent());
|
||||||
|
|
||||||
empty.ifPresent(v -> { fail(); });
|
empty.ifPresent(v -> { fail(); });
|
||||||
|
|
||||||
|
AtomicBoolean emptyCheck = new AtomicBoolean();
|
||||||
|
empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
|
||||||
|
assertTrue(emptyCheck.get());
|
||||||
|
|
||||||
|
try {
|
||||||
|
empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
|
||||||
|
fail();
|
||||||
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(2.0, empty.orElse(2.0));
|
assertEquals(2.0, empty.orElse(2.0));
|
||||||
assertEquals(2.0, empty.orElseGet(()-> 2.0));
|
assertEquals(2.0, empty.orElseGet(()-> 2.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(groups = "unit")
|
||||||
|
public void testIfPresentAndOrElseAndNull() {
|
||||||
|
OptionalDouble empty = OptionalDouble.empty();
|
||||||
|
OptionalDouble present = OptionalDouble.of(1.0);
|
||||||
|
|
||||||
|
// No NPE
|
||||||
|
present.ifPresentOrElse(v -> {}, null);
|
||||||
|
empty.ifPresent(null);
|
||||||
|
empty.ifPresentOrElse(null, () -> {});
|
||||||
|
|
||||||
|
// NPE
|
||||||
|
try {
|
||||||
|
present.ifPresent(null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
try {
|
||||||
|
present.ifPresentOrElse(null, () -> {});
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
try {
|
||||||
|
empty.ifPresentOrElse(v -> {}, null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expectedExceptions=NoSuchElementException.class)
|
@Test(expectedExceptions=NoSuchElementException.class)
|
||||||
public void testEmptyGet() {
|
public void testEmptyGet() {
|
||||||
OptionalDouble empty = OptionalDouble.empty();
|
OptionalDouble empty = OptionalDouble.empty();
|
||||||
@ -96,12 +138,33 @@ public class BasicDouble {
|
|||||||
assertFalse(present.toString().isEmpty());
|
assertFalse(present.toString().isEmpty());
|
||||||
assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
|
assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
|
||||||
assertEquals(1.0, present.getAsDouble());
|
assertEquals(1.0, present.getAsDouble());
|
||||||
|
|
||||||
|
AtomicBoolean presentCheck = new AtomicBoolean();
|
||||||
|
present.ifPresent(v -> presentCheck.set(true));
|
||||||
|
assertTrue(presentCheck.get());
|
||||||
|
presentCheck.set(false);
|
||||||
|
present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
|
||||||
|
assertTrue(presentCheck.get());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
present.ifPresent(v -> { throw new ObscureException(); });
|
present.ifPresent(v -> { throw new ObscureException(); });
|
||||||
fail();
|
fail();
|
||||||
} catch (ObscureException expected) {
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
|
||||||
|
fail();
|
||||||
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(1.0, present.orElse(2.0));
|
assertEquals(1.0, present.orElse(2.0));
|
||||||
assertEquals(1.0, present.orElseGet(null));
|
assertEquals(1.0, present.orElseGet(null));
|
||||||
assertEquals(1.0, present.orElseGet(()-> 2.0));
|
assertEquals(1.0, present.orElseGet(()-> 2.0));
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.OptionalInt;
|
import java.util.OptionalInt;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import static org.testng.Assert.*;
|
import static org.testng.Assert.*;
|
||||||
@ -49,11 +50,52 @@ public class BasicInt {
|
|||||||
assertTrue(0 == empty.hashCode());
|
assertTrue(0 == empty.hashCode());
|
||||||
assertTrue(!empty.toString().isEmpty());
|
assertTrue(!empty.toString().isEmpty());
|
||||||
assertTrue(!empty.isPresent());
|
assertTrue(!empty.isPresent());
|
||||||
|
|
||||||
empty.ifPresent(v -> { fail(); });
|
empty.ifPresent(v -> { fail(); });
|
||||||
|
|
||||||
|
AtomicBoolean emptyCheck = new AtomicBoolean();
|
||||||
|
empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
|
||||||
|
assertTrue(emptyCheck.get());
|
||||||
|
|
||||||
|
try {
|
||||||
|
empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
|
||||||
|
fail();
|
||||||
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(2, empty.orElse(2));
|
assertEquals(2, empty.orElse(2));
|
||||||
assertEquals(2, empty.orElseGet(()-> 2));
|
assertEquals(2, empty.orElseGet(()-> 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(groups = "unit")
|
||||||
|
public void testIfPresentAndOrElseAndNull() {
|
||||||
|
OptionalInt empty = OptionalInt.empty();
|
||||||
|
OptionalInt present = OptionalInt.of(1);
|
||||||
|
|
||||||
|
// No NPE
|
||||||
|
present.ifPresentOrElse(v -> {}, null);
|
||||||
|
empty.ifPresent(null);
|
||||||
|
empty.ifPresentOrElse(null, () -> {});
|
||||||
|
|
||||||
|
// NPE
|
||||||
|
try {
|
||||||
|
present.ifPresent(null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
try {
|
||||||
|
present.ifPresentOrElse(null, () -> {});
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
try {
|
||||||
|
empty.ifPresentOrElse(v -> {}, null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expectedExceptions=NoSuchElementException.class)
|
@Test(expectedExceptions=NoSuchElementException.class)
|
||||||
public void testEmptyGet() {
|
public void testEmptyGet() {
|
||||||
OptionalInt empty = OptionalInt.empty();
|
OptionalInt empty = OptionalInt.empty();
|
||||||
@ -96,12 +138,33 @@ public class BasicInt {
|
|||||||
assertFalse(present.toString().isEmpty());
|
assertFalse(present.toString().isEmpty());
|
||||||
assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
|
assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
|
||||||
assertEquals(1, present.getAsInt());
|
assertEquals(1, present.getAsInt());
|
||||||
|
|
||||||
|
AtomicBoolean presentCheck = new AtomicBoolean();
|
||||||
|
present.ifPresent(v -> presentCheck.set(true));
|
||||||
|
assertTrue(presentCheck.get());
|
||||||
|
presentCheck.set(false);
|
||||||
|
present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
|
||||||
|
assertTrue(presentCheck.get());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
present.ifPresent(v -> { throw new ObscureException(); });
|
present.ifPresent(v -> { throw new ObscureException(); });
|
||||||
fail();
|
fail();
|
||||||
} catch (ObscureException expected) {
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
|
||||||
|
fail();
|
||||||
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(1, present.orElse(2));
|
assertEquals(1, present.orElse(2));
|
||||||
assertEquals(1, present.orElseGet(null));
|
assertEquals(1, present.orElseGet(null));
|
||||||
assertEquals(1, present.orElseGet(()-> 2));
|
assertEquals(1, present.orElseGet(()-> 2));
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.OptionalLong;
|
import java.util.OptionalLong;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.stream.LongStream;
|
import java.util.stream.LongStream;
|
||||||
|
|
||||||
import static org.testng.Assert.*;
|
import static org.testng.Assert.*;
|
||||||
@ -49,11 +50,52 @@ public class BasicLong {
|
|||||||
assertTrue(0 == empty.hashCode());
|
assertTrue(0 == empty.hashCode());
|
||||||
assertTrue(!empty.toString().isEmpty());
|
assertTrue(!empty.toString().isEmpty());
|
||||||
assertTrue(!empty.isPresent());
|
assertTrue(!empty.isPresent());
|
||||||
|
|
||||||
empty.ifPresent(v -> { fail(); });
|
empty.ifPresent(v -> { fail(); });
|
||||||
|
|
||||||
|
AtomicBoolean emptyCheck = new AtomicBoolean();
|
||||||
|
empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
|
||||||
|
assertTrue(emptyCheck.get());
|
||||||
|
|
||||||
|
try {
|
||||||
|
empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
|
||||||
|
fail();
|
||||||
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(2, empty.orElse(2));
|
assertEquals(2, empty.orElse(2));
|
||||||
assertEquals(2, empty.orElseGet(()-> 2));
|
assertEquals(2, empty.orElseGet(()-> 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(groups = "unit")
|
||||||
|
public void testIfPresentAndOrElseAndNull() {
|
||||||
|
OptionalLong empty = OptionalLong.empty();
|
||||||
|
OptionalLong present = OptionalLong.of(1);
|
||||||
|
|
||||||
|
// No NPE
|
||||||
|
present.ifPresentOrElse(v -> {}, null);
|
||||||
|
empty.ifPresent(null);
|
||||||
|
empty.ifPresentOrElse(null, () -> {});
|
||||||
|
|
||||||
|
// NPE
|
||||||
|
try {
|
||||||
|
present.ifPresent(null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
try {
|
||||||
|
present.ifPresentOrElse(null, () -> {});
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
try {
|
||||||
|
empty.ifPresentOrElse(v -> {}, null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException ex) {}
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expectedExceptions=NoSuchElementException.class)
|
@Test(expectedExceptions=NoSuchElementException.class)
|
||||||
public void testEmptyGet() {
|
public void testEmptyGet() {
|
||||||
OptionalLong empty = OptionalLong.empty();
|
OptionalLong empty = OptionalLong.empty();
|
||||||
@ -96,12 +138,35 @@ public class BasicLong {
|
|||||||
assertFalse(present.toString().isEmpty());
|
assertFalse(present.toString().isEmpty());
|
||||||
assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
|
assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
|
||||||
assertEquals(1L, present.getAsLong());
|
assertEquals(1L, present.getAsLong());
|
||||||
|
|
||||||
|
AtomicBoolean presentCheck = new AtomicBoolean();
|
||||||
|
present.ifPresent(v -> presentCheck.set(true));
|
||||||
|
assertTrue(presentCheck.get());
|
||||||
|
presentCheck.set(false);
|
||||||
|
present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
|
||||||
|
assertTrue(presentCheck.get());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
present.ifPresent(v -> { throw new ObscureException(); });
|
present.ifPresent(v -> { throw new ObscureException(); });
|
||||||
fail();
|
fail();
|
||||||
} catch (ObscureException expected) {
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
present.ifPresentOrElse(v -> {
|
||||||
|
throw new ObscureException();
|
||||||
|
}, () -> fail());
|
||||||
|
fail();
|
||||||
|
} catch (ObscureException expected) {
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(1, present.orElse(2));
|
assertEquals(1, present.orElse(2));
|
||||||
assertEquals(1, present.orElseGet(null));
|
assertEquals(1, present.orElseGet(null));
|
||||||
assertEquals(1, present.orElseGet(()-> 2));
|
assertEquals(1, present.orElseGet(()-> 2));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user