本文整理了Java中com.annimon.stream.Optional.ifPresentOrElse()
方法的一些代码示例,展示了Optional.ifPresentOrElse()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional.ifPresentOrElse()
方法的具体详情如下:
包路径:com.annimon.stream.Optional
类名称: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();
});
}
}
内容来源于网络,如有侵权,请联系作者删除!