com.annimon.stream.Optional类的使用及代码示例

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

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

Optional介绍

[英]A container object which may or may not contain a non-null value.
[中]

代码示例

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

@Test
public void testGetWithPresentValue() {
  int value = Optional.of(10).get();
  assertEquals(10, value);
}

代码示例来源: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

/**
 * Returns an {@code Optional} with the specified value, or empty {@code Optional} if value is null.
 *
 * @param <T> the type of value
 * @param value  the value which can be null
 * @return an {@code Optional}
 * @see #of(java.lang.Object)
 */
public static <T> Optional<T> ofNullable(T value) {
  return value == null ? Optional.<T>empty() : of(value);
}

代码示例来源: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

@Test
public void testFindIndexed() {
  IntPair<Integer> result = Stream.rangeClosed(1, 10)
      .findIndexed(sumEquals(7))
      .get();
  assertThat(result.getFirst(), is(3));
  assertThat(result.getSecond(), is(4));
}

代码示例来源: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: aNNiMON/Lightweight-Stream-API

@Test
public void testExecuteIfPresent() {
  int value = Optional.of(10)
      .executeIfPresent(new Consumer<Integer>() {
        @Override
        public void accept(Integer value) {
          assertEquals(10, (int) value);
        }
      })
      .get();
  assertEquals(10, value);
}

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

@Test
public void testIfPresentOrElseWhenValuePresentAndEmptyActionNull() {
  Optional.of(10).ifPresentOrElse(new Consumer<Integer>() {
    @Override
    public void accept(Integer value) {
      assertEquals(10, (int) value);
    }
  }, null);
}

代码示例来源: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: aNNiMON/Lightweight-Stream-API

@Override
public boolean matches(Optional<T> argument) {
  return argument != null && argument.isPresent();
}

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

/**
 * Invokes consumer function with the value if present.
 * This method same as {@code ifPresent}, but does not breaks chaining
 *
 * @param consumer  consumer function
 * @return this {@code Optional}
 * @see #ifPresent(com.annimon.stream.function.Consumer)
 * @since 1.1.2
 */
public Optional<T> executeIfPresent(Consumer<? super T> consumer) {
  ifPresent(consumer);
  return this;
}

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

@Test
public void testOr() {
  int value = Optional.of(42).or(new Supplier<Optional<Integer>>() {
    @Override
    public Optional<Integer> get() {
      return Optional.of(19);
    }
  }).get();
  assertEquals(42, value);
}

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

@Override
  public Optional<Integer> get() {
    return Optional.of(19);
  }
}).get();

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

@Test
public void testMapOnEmptyOptional() {
  assertFalse(
      Optional.<Integer>empty()
          .map(UnaryOperator.Util.<Integer>identity())
          .isPresent());
}

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

@Test
public void testIfPresent() {
  Optional.of(10).ifPresent(new Consumer<Integer>() {
    @Override
    public void accept(Integer value) {
      assertEquals(10, (int) 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
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 testHashCodeWithDifferentNullableState() {
  final Optional<Integer> optInt = Optional.of(10);
  final Optional<Integer> optIntNullable = Optional.ofNullable(10);
  assertEquals(optInt.hashCode(), optIntNullable.hashCode());
}

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

@Test(expected = NoSuchElementException.class)
public void testGetOnEmptyOptional() {
  Optional.empty().get();
}

相关文章