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

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

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

Optional.ifPresentOrElse介绍

[英]If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
[中]如果存在值,则使用该值执行给定操作,否则执行给定的基于空的操作。

代码示例

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

@Test(expected = NullPointerException.class)
public void testIfPresentOrElseWhenValuePresentAndConsumerNull() {
  Optional.of(10).ifPresentOrElse(null, new Runnable() {
    @Override
    public void run() {
      fail("Should not have been executed.");
    }
  });
}

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

@Test(expected = NullPointerException.class)
public void testIfPresentOrElseWhenValueAbsentAndEmptyActionNull() {
  Optional.<Integer>empty().ifPresentOrElse(new Consumer<Integer>() {
    @Override
    public void accept(Integer value) {
      fail("Should not have been executed.");
    }
  }, null);
}

代码示例来源: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 = RuntimeException.class)
public void testIfPresentOrElseWhenValueAbsent() {
  Optional.<Integer>empty().ifPresentOrElse(new Consumer<Integer>() {
    @Override
    public void accept(Integer value) {
      fail();
    }
  }, new Runnable() {
    @Override
    public void run() {
      throw new RuntimeException();
    }
  });
}

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

@Test
public void testIfPresentOrElseWhenValuePresent() {
  Optional.of(10).ifPresentOrElse(new Consumer<Integer>() {
    @Override
    public void accept(Integer value) {
      assertEquals(10, (int) value);
    }
  }, new Runnable() {
    @Override
    public void run() {
      fail("Should not execute empty action when value is present.");
    }
  });
}

代码示例来源:origin: Applandeo/Material-Calendar-View

private void onClick(Calendar day) {
  if (mCalendarProperties.getEventDays() == null) {
    createEmptyEventDay(day);
    return;
  }
  Stream.of(mCalendarProperties.getEventDays())
      .filter(eventDate -> eventDate.getCalendar().equals(day))
      .findFirst()
      .ifPresentOrElse(this::callOnClickListener, () -> createEmptyEventDay(day));
}

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

public void init() {
    if (!preferencesManager.getAutologinEnabled()) {
      preferencesManager.clear();
      dataManager.clearData();
    }
    getMvpView().ifPresent(splashView -> {
      preferencesManager.getToken().ifPresentOrElse(token -> splashView.goToProjectList(), splashView::goToLogin);
      splashView.close();
    });
  }
}

相关文章