本文整理匯總了Java中java.util.OptionalInt.ifPresent方法的典型用法代碼示例。如果您正苦於以下問題:Java OptionalInt.ifPresent方法的具體用法?Java OptionalInt.ifPresent怎麽用?Java OptionalInt.ifPresent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.OptionalInt
的用法示例。
在下文中一共展示了OptionalInt.ifPresent方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testEmpty
import java.util.OptionalInt; //導入方法依賴的package包/類
@Test(groups = "unit")
public void testEmpty() {
OptionalInt empty = OptionalInt.empty();
OptionalInt present = OptionalInt.of(1);
// empty
assertTrue(empty.equals(empty));
assertTrue(empty.equals(OptionalInt.empty()));
assertTrue(!empty.equals(present));
assertTrue(0 == empty.hashCode());
assertTrue(!empty.toString().isEmpty());
assertTrue(!empty.isPresent());
empty.ifPresent(v -> { fail(); });
assertEquals(2, empty.orElse(2));
assertEquals(2, empty.orElseGet(()-> 2));
}
示例2: testPresent
import java.util.OptionalInt; //導入方法依賴的package包/類
@Test(groups = "unit")
public void testPresent() {
OptionalInt empty = OptionalInt.empty();
OptionalInt present = OptionalInt.of(1);
// present
assertTrue(present.equals(present));
assertFalse(present.equals(OptionalInt.of(0)));
assertTrue(present.equals(OptionalInt.of(1)));
assertFalse(present.equals(empty));
assertTrue(Integer.hashCode(1) == present.hashCode());
assertFalse(present.toString().isEmpty());
assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
assertEquals(1, present.getAsInt());
try {
present.ifPresent(v -> { throw new ObscureException(); });
fail();
} catch(ObscureException expected) {
}
assertEquals(1, present.orElse(2));
assertEquals(1, present.orElseGet(null));
assertEquals(1, present.orElseGet(()-> 2));
assertEquals(1, present.orElseGet(()-> 3));
assertEquals(1, present.<RuntimeException>orElseThrow(null));
assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
}
示例3: testEmpty
import java.util.OptionalInt; //導入方法依賴的package包/類
@Test(groups = "unit")
public void testEmpty() {
OptionalInt empty = OptionalInt.empty();
OptionalInt present = OptionalInt.of(1);
// empty
assertTrue(empty.equals(empty));
assertTrue(empty.equals(OptionalInt.empty()));
assertTrue(!empty.equals(present));
assertTrue(0 == empty.hashCode());
assertTrue(!empty.toString().isEmpty());
assertTrue(!empty.isPresent());
empty.ifPresent(v -> { fail(); });
AtomicBoolean emptyCheck = new AtomicBoolean();
empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
assertTrue(emptyCheck.get());
try {
empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
fail();
} catch (ObscureException expected) {
} catch (AssertionError e) {
throw e;
} catch (Throwable t) {
fail();
}
assertEquals(2, empty.orElse(2));
assertEquals(2, empty.orElseGet(()-> 2));
}