本文整理了Java中com.annimon.stream.Optional.empty()
方法的一些代码示例,展示了Optional.empty()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional.empty()
方法的具体详情如下:
包路径:com.annimon.stream.Optional
类名称:Optional
方法名:empty
[英]Returns an empty Optional.
[中]返回一个空的可选值。
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Override
public Optional<Integer> get() {
return Optional.empty();
}
});
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
/**
* Performs filtering on inner value if it is present.
*
* @param predicate a predicate function
* @return this {@code Optional} if the value is present and matches predicate,
* otherwise an empty {@code Optional}
*/
public Optional<T> filter(Predicate<? super T> predicate) {
if (!isPresent()) return this;
return predicate.test(value) ? this : Optional.<T>empty();
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
/**
* Invokes the given mapping function on inner value if present.
*
* @param <U> the type of result value
* @param mapper mapping function
* @return an {@code Optional} with transformed value if present,
* otherwise an empty {@code Optional}
* @throws NullPointerException if value is present and
* {@code mapper} is {@code null}
*/
public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
if (!isPresent()) return empty();
return Optional.ofNullable(mapper.apply(value));
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test(expected = RuntimeException.class)
public void testIfPresentOrElseWhenValueAbsentAndConsumerNull() {
Optional.<Integer>empty().ifPresentOrElse(null, new Runnable() {
@Override
public void run() {
throw new RuntimeException();
}
});
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test(expected = ArithmeticException.class)
public void testOrElseThrow() {
Optional.empty().orElseThrow(new Supplier<RuntimeException>() {
@Override
public RuntimeException get() {
return new ArithmeticException();
}
});
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testExecuteIfPresentOnAbsentValue() {
Optional.<Integer>empty()
.executeIfPresent(new Consumer<Integer>() {
@Override
public void accept(Integer value) {
fail();
}
});
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test(expected = RuntimeException.class)
public void testExecuteIfAbsent() {
Optional.empty()
.executeIfAbsent(new Runnable() {
@Override
public void run() {
throw new RuntimeException();
}
});
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testCustomTerminal() {
Integer result = Optional.<Integer>empty()
.custom(new Function<Optional<Integer>, Integer>() {
@Override
public Integer apply(Optional<Integer> optional) {
return optional.orElse(0);
}
});
assertThat(result, is(0));
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testOrElseGet() {
int value = Optional.<Integer>empty().orElseGet(new Supplier<Integer>() {
@Override
public Integer get() {
return 42;
}
});
assertEquals(42, value);
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testToStringOnEmptyOptional() {
assertEquals("Optional.empty", Optional.empty().toString());
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testStreamOnEmptyOptional() {
long count = Optional.empty().stream().count();
assertThat(count, is(0L));
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testEqualsWithTwoEmptyOptional() {
final Optional<Integer> empty1 = Optional.ofNullable(null);
final Optional<Integer> empty2 = Optional.empty();
assertEquals(empty1, empty2);
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testOrOnEmptyOptionalAndEmptySupplierOptional() {
final Optional<Integer> optional = Optional.<Integer>empty().or(new Supplier<Optional<Integer>>() {
@Override
public Optional<Integer> get() {
return Optional.empty();
}
});
assertThat(optional, isEmpty());
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testSelectOnEmptyOptional() {
Optional<Integer> result = Optional.empty()
.select(Integer.class);
assertThat(result, isEmpty());
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testOrOnEmptyOptional() {
int value = Optional.<Integer>empty().or(new Supplier<Optional<Integer>>() {
@Override
public Optional<Integer> get() {
return Optional.of(19);
}
}).get();
assertEquals(19, value);
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testIsEmpty() {
Optional<Integer> optional = Optional.empty();
assertThat(optional, isEmpty());
assertThat(optional, not(isPresent()));
assertThat(isEmpty(), description(is("Optional value should be empty")));
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testHashCodeWithTwoEmptyOptional() {
final Optional<Integer> empty1 = Optional.ofNullable(null);
final Optional<Integer> empty2 = Optional.empty();
assertEquals(empty1.hashCode(), empty2.hashCode());
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testMapOnEmptyOptional() {
assertFalse(
Optional.<Integer>empty()
.map(UnaryOperator.Util.<Integer>identity())
.isPresent());
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@SuppressWarnings("EqualsBetweenInconvertibleTypes")
@Test
public void testEquals() {
assertEquals(OptionalLong.empty(), OptionalLong.empty());
assertNotEquals(OptionalLong.empty(), Optional.empty());
assertEquals(OptionalLong.of(42), OptionalLong.of(42));
assertNotEquals(OptionalLong.of(41), OptionalLong.of(42));
assertNotEquals(OptionalLong.of(0), OptionalLong.empty());
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@SuppressWarnings("EqualsBetweenInconvertibleTypes")
@Test
public void testEquals() {
assertEquals(OptionalInt.empty(), OptionalInt.empty());
assertNotEquals(OptionalInt.empty(), Optional.empty());
assertEquals(OptionalInt.of(42), OptionalInt.of(42));
assertNotEquals(OptionalInt.of(41), OptionalInt.of(42));
assertNotEquals(OptionalInt.of(0), OptionalInt.empty());
}
内容来源于网络,如有侵权,请联系作者删除!