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

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

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

Optional.orElse介绍

[英]Returns inner value if present, otherwise returns other.
[中]如果存在,则返回内部值,否则返回其他值。

代码示例

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

@Override
  public Integer apply(Optional<Integer> optional) {
    return optional.orElse(0);
  }
});

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

@Test
public void testOrElseWithPresentValue() {
  int value = Optional.<Integer>empty().orElse(42);
  assertEquals(42, value);
}

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

@Test
public void testOrElseOnEmptyOptional() {
  assertEquals("Lena", Optional.<Student>empty().orElse(student).getName());
}

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

@Test
  public void testReduceOptionalOnEmptyStream() {
    Optional<Integer> result = Stream.<Integer>empty()
        .reduce(Functions.addition());

    assertThat(result, isEmpty());
    assertEquals(119, (int) result.orElse(119));
  }
}

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

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

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

@Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    return chain.proceed(
        preferencesManager.getToken().map(token -> request.newBuilder()
            .header(AUTHORIZATION_KEY, token)
            .method(request.method(), request.body())
            .build()).orElse(request)
    );
  }
}

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

public static <T> Observable<T> ofOptional(Optional<T> optional) {
  return optional.map(Observable::just).orElse(Observable.empty());
}

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

public static void setupTypefaceToTextView(TextView tv, AttributeSet attrs) {
  if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    applyFixForPre21ver(tv);
  }
  if (!tv.isInEditMode() && attrs != null) {
    TypedArray typedArray = tv.getContext().obtainStyledAttributes(
      attrs,
      R.styleable.CustomFontTextView);
    final int fontIndex = typedArray.getInt(R.styleable.CustomFontTextView_customFontName, -1);
    if (fontIndex < 0) {
      typedArray.recycle();
      throw new IllegalArgumentException("You must provide attribute \"customFontName\" for your CustomFontTextView");
    } else {
      final Typeface customTypeface = CustomFont.findByIndex(fontIndex).orElse(CustomFont.CLANT_OT_NARR_BOOK)
        .asTypeface(tv.getContext());
      tv.setTypeface(customTypeface);
      typedArray.recycle();
    }
  }
}

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

public static <T> Maybe<T> ofOptionalMaybe(Optional<T> optional) {
  return optional.map(Maybe::just).orElse(Maybe.empty());
}

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

@SafeVarargs
public static <T> T firstOrNull(T... args) {
  return Stream.of(args).filter(value -> value != null).findFirst().orElse(null);
}

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

@NonNull
public Response proceed(@NonNull Request request, @NonNull String path) {
  Set<String> keys = mResponsesMap.keySet();
  String token = TasBoilerplateApplication.get(context).getComponent().preferencesHelper().getToken().orElse(null);
  if (token != null && (request.header(RetrofitBuilder.AUTHORIZATION_KEY) == null ||
      !request.header(RetrofitBuilder.AUTHORIZATION_KEY).contains(token))) {
    return OkHttpResponse.error(request, 401, "Not authorized");
  }
  for (String interceptUrl : keys) {
    if (path.contains(interceptUrl)) {
      String mockResponsePath = mResponsesMap.get(interceptUrl);
      return createResponseFromAssets(request, mockResponsePath);
    }
  }
  return OkHttpResponse.error(request, 500, "Incorrectly intercepted request");
}

相关文章