本文整理了Java中com.annimon.stream.Optional.isPresent()
方法的一些代码示例,展示了Optional.isPresent()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional.isPresent()
方法的具体详情如下:
包路径:com.annimon.stream.Optional
类名称:Optional
方法名:isPresent
[英]Checks value present.
[中]检查当前值。
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Override
public boolean matches(Optional<T> argument) {
return argument != null && argument.isPresent();
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Override
protected boolean matchesSafely(Optional<?> optional, Description mismatchDescription) {
mismatchDescription.appendText("Optional was empty");
return optional.isPresent();
}
代码示例来源: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
/**
* Wraps a value into {@code Stream} if present, otherwise returns an empty {@code Stream}.
*
* @return the optional value as a {@code Stream}
*/
@SuppressWarnings("unchecked")
public Stream<T> stream() {
if (!isPresent()) return Stream.empty();
return Stream.of(value);
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
/**
* Keeps inner value only if is present and instance of given class.
*
* @param <R> a type of instance to select.
* @param clazz a class which instance should be selected
* @return an {@code Optional} with value of type class if present, otherwise an empty {@code Optional}
*/
@SuppressWarnings("unchecked")
public <R> Optional<R> select(Class<R> clazz) {
Objects.requireNonNull(clazz);
if (!isPresent()) return empty();
return (Optional<R>) Optional.ofNullable(clazz.isInstance(value) ? value : null);
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
/**
* Invokes mapping function with {@code Optional} result if value is 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}
*/
public <U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
if (!isPresent()) return empty();
return Objects.requireNonNull(mapper.apply(value));
}
代码示例来源: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
/**
* Invokes mapping function on inner value if present.
*
* @param mapper mapping function
* @return an {@code OptionalBoolean} with transformed value if present,
* otherwise an empty {@code OptionalBoolean}
* @throws NullPointerException if value is present and
* {@code mapper} is {@code null}
*/
public OptionalBoolean mapToBoolean(ToBooleanFunction<? super T> mapper) {
if (!isPresent()) return OptionalBoolean.empty();
return OptionalBoolean.of(mapper.applyAsBoolean(value));
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
/**
* Invokes mapping function on inner value if present.
*
* @param mapper mapping function
* @return an {@code OptionalLong} with transformed value if present,
* otherwise an empty {@code OptionalLong}
* @throws NullPointerException if value is present and
* {@code mapper} is {@code null}
* @since 1.1.4
*/
public OptionalLong mapToLong(ToLongFunction<? super T> mapper) {
if (!isPresent()) return OptionalLong.empty();
return OptionalLong.of(mapper.applyAsLong(value));
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
/**
* Invokes the given mapping function on inner value if present.
*
* @param mapper mapping function
* @return an {@code OptionalInt} with transformed value if present,
* otherwise an empty {@code OptionalInt}
* @throws NullPointerException if value is present and
* {@code mapper} is {@code null}
* @since 1.1.3
*/
public OptionalInt mapToInt(ToIntFunction<? super T> mapper) {
if (!isPresent()) return OptionalInt.empty();
return OptionalInt.of(mapper.applyAsInt(value));
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
/**
* Invokes mapping function on inner value if present.
*
* @param mapper mapping function
* @return an {@code OptionalDouble} with transformed value if present,
* otherwise an empty {@code OptionalDouble}
* @throws NullPointerException if value is present and
* {@code mapper} is {@code null}
* @since 1.1.4
*/
public OptionalDouble mapToDouble(ToDoubleFunction<? super T> mapper) {
if (!isPresent()) return OptionalDouble.empty();
return OptionalDouble.of(mapper.applyAsDouble(value));
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
/**
* Returns current {@code Optional} if value is present, otherwise
* returns an {@code Optional} produced by supplier function.
*
* @param supplier supplier function that produces an {@code Optional} to be returned
* @return this {@code Optional} if value is present, otherwise
* an {@code Optional} produced by supplier function
* @throws NullPointerException if value is not present and
* {@code supplier} or value produced by it is {@code null}
*/
public Optional<T> or(Supplier<Optional<T>> supplier) {
if (isPresent()) return this;
Objects.requireNonNull(supplier);
return Objects.requireNonNull(supplier.get());
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testGetOptional() {
Optional<Integer> value = Exceptional
.of(ioExceptionSupplier)
.getOptional();
assertFalse(value.isPresent());
}
代码示例来源:origin: aNNiMON/Lightweight-Stream-API
@Test
public void testMapOnEmptyOptional() {
assertFalse(
Optional.<Integer>empty()
.map(UnaryOperator.Util.<Integer>identity())
.isPresent());
}
代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate
public static <T> Observable<Optional<T>> permute(Optional<Observable<T>> source) {
if (source.isPresent()) {
return source.get().map(Optional::ofNullable);
} else {
return just(Optional.empty());
}
}
代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate
public static <T> Observable<T> flatten(Optional<Observable<T>> source) {
if (source.isPresent()) {
return source.get();
} else {
return empty();
}
}
代码示例来源:origin: felHR85/UsbSerial
public boolean disconnectDevice(UsbDevice usbDevice){
Optional<UsbSerialDevice> optionalDevice = Stream.of(serialDevices)
.filter(p -> usbDevice.getDeviceId() == p.getDeviceId())
.findSingle();
if(optionalDevice.isPresent()){
UsbSerialDevice disconnectedDevice = optionalDevice.get();
disconnectedDevice.syncClose();
serialDevices = Utils.removeIf(serialDevices, p -> usbDevice.getDeviceId() == p.getDeviceId());
return true;
}
return false;
}
内容来源于网络,如有侵权,请联系作者删除!