com.annimon.stream.Optional.ofNullable()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(6.3k)|赞(0)|评价(0)|浏览(107)

本文整理了Java中com.annimon.stream.Optional.ofNullable()方法的一些代码示例,展示了Optional.ofNullable()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional.ofNullable()方法的具体详情如下:
包路径:com.annimon.stream.Optional
类名称:Optional
方法名:ofNullable

Optional.ofNullable介绍

[英]Returns an Optional with the specified value, or empty Optional if value is null.
[中]返回具有指定值的可选值,如果值为null,则返回空可选值。

代码示例

代码示例来源:origin: aNNiMON/Lightweight-Stream-API

/**
 * Wraps inner value with {@code Optional} container
 *
 * @return an {@code Optional}
 */
public Optional<T> getOptional() {
  return Optional.ofNullable(value);
}

代码示例来源:origin: aNNiMON/Lightweight-Stream-API

@Override
  public Optional<String> apply(Integer value) {
    return Optional.ofNullable(String.valueOf((char) value.intValue()));
  }
});

代码示例来源:origin: aNNiMON/Lightweight-Stream-API

@Override
  public Optional<String> apply(Integer value) {
    return Optional.ofNullable(String.valueOf((char) value.intValue()));
  }
});

代码示例来源: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 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}
 * @since 1.1.3
 */
public <U> Optional<U> mapToObj(IntFunction<U> mapper) {
  if (!isPresent()) return Optional.empty();
  return Optional.ofNullable(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 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> mapToObj(LongFunction<U> mapper) {
  if (!isPresent()) {
    return Optional.empty();
  }
  Objects.requireNonNull(mapper);
  return Optional.ofNullable(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> mapToObj(BooleanFunction<U> mapper) {
  if (!isPresent()) {
    return Optional.empty();
  }
  Objects.requireNonNull(mapper);
  return Optional.ofNullable(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> mapToObj(DoubleFunction<U> mapper) {
  if (!isPresent()) {
    return Optional.empty();
  }
  Objects.requireNonNull(mapper);
  return Optional.ofNullable(mapper.apply(value));
}

代码示例来源: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 testEqualsWithDifferentNullableState() {
  final Optional<Integer> optInt = Optional.of(10);
  final Optional<Integer> optIntNullable = Optional.ofNullable(10);
  assertEquals(optInt, optIntNullable);
}

代码示例来源:origin: aNNiMON/Lightweight-Stream-API

@Test
public void testIsPresentOnEmptyOptional() {
  assertThat(Optional.ofNullable(null), isEmpty());
}

代码示例来源:origin: aNNiMON/Lightweight-Stream-API

@Test
public void testFlatMapOnEmptyOptional() {
  Optional<String> result = Optional.<Integer>ofNullable(null)
      .flatMap(new Function<Integer, Optional<String>>() {
        @Override
        public Optional<String> apply(Integer value) {
          return Optional.ofNullable(String.valueOf((char) value.intValue()));
        }
      });
  assertThat(result, isEmpty());
}

代码示例来源: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 testHashCodeWithDifferentNullableState() {
  final Optional<Integer> optInt = Optional.of(10);
  final Optional<Integer> optIntNullable = Optional.ofNullable(10);
  assertEquals(optInt.hashCode(), optIntNullable.hashCode());
}

代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate

@Nullable
public Optional<CustomToolbar> getToolbar() {
  return Optional.ofNullable(toolbar);
}

代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate

public Optional<String> getToken() {
  return Optional.ofNullable(preferences.getString(PREF_KEY_TOKEN, null));
}

代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate

public String getMessage() {
    return ofNullable(message)
      .orElse(detail);
  }
}

代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate

public Optional<T> getMvpView() {
  if (mMvpView == null) {
    new MvpViewNotAttachedException().printStackTrace();
  }
  return Optional.ofNullable(mMvpView);
}

代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate

public void setToolbarTitle(String title) {
  Optional.ofNullable(toolbar).ifPresent(bar -> toolbar.setToolbarTitle(title));
}

相关文章