Java Code Examples for java.util.OptionalLong#ifPresent()

The following examples show how to use java.util.OptionalLong#ifPresent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicLong.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 2
Source File: BasicLong.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 3
Source File: BasicLong.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 4
Source File: BasicLong.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 5
Source File: BasicLong.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 6
Source File: BasicLong.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 7
Source File: BasicLong.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 8
Source File: BasicLong.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 9
Source File: BasicLong.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(groups = "unit")
    public void testPresent() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1L);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalLong.of(0L)));
    assertTrue(present.equals(OptionalLong.of(1L)));
    assertFalse(present.equals(empty));
    assertTrue(Long.hashCode(1) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
    assertEquals(1L, present.getAsLong());
    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));
}
 
Example 10
Source File: BasicLong.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalLong.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));
}
 
Example 11
Source File: BasicLong.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(groups = "unit")
    public void testPresent() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1L);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalLong.of(0L)));
    assertTrue(present.equals(OptionalLong.of(1L)));
    assertFalse(present.equals(empty));
    assertTrue(Long.hashCode(1) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
    assertEquals(1L, present.getAsLong());
    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));
}
 
Example 12
Source File: BasicLong.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(groups = "unit")
    public void testPresent() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1L);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalLong.of(0L)));
    assertTrue(present.equals(OptionalLong.of(1L)));
    assertFalse(present.equals(empty));
    assertTrue(Long.hashCode(1) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
    assertEquals(1L, present.getAsLong());
    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));
}
 
Example 13
Source File: BasicLong.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test(groups = "unit")
    public void testPresent() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1L);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalLong.of(0L)));
    assertTrue(present.equals(OptionalLong.of(1L)));
    assertFalse(present.equals(empty));
    assertTrue(Long.hashCode(1) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
    assertEquals(1L, present.getAsLong());
    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));
}
 
Example 14
Source File: AbstractAccess.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
private <T extends StorageIndex> T createIndex(Supplier<T> indexSupplier) {
  // Create an index
  T newIndex = indexSupplier.get();
  // Find the id of the index (possibly, newly created)
  OptionalLong indexId = findIndexId(newIndex.getAddress());
  // Register the open index in the pool, if it exists.
  // It does not "exist" until it is created with a Fork-based Access,
  // i.e., an empty index created with the Snapshot will not have an id.
  // We can, of course, cache them by address then, but that is not
  // required for correctness and may be done later as an optimization.
  indexId.ifPresent(id -> registerIndex(id, newIndex));
  return newIndex;
}
 
Example 15
Source File: BasicLong.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(groups = "unit")
    public void testPresent() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1L);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalLong.of(0L)));
    assertTrue(present.equals(OptionalLong.of(1L)));
    assertFalse(present.equals(empty));
    assertTrue(Long.hashCode(1) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
    assertEquals(1L, present.getAsLong());
    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));
}
 
Example 16
Source File: BasicLong.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(groups = "unit")
    public void testPresent() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1L);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalLong.of(0L)));
    assertTrue(present.equals(OptionalLong.of(1L)));
    assertFalse(present.equals(empty));
    assertTrue(Long.hashCode(1) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
    assertEquals(1L, present.getAsLong());
    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));
}
 
Example 17
Source File: BasicLong.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(groups = "unit")
    public void testPresent() {
    OptionalLong empty = OptionalLong.empty();
    OptionalLong present = OptionalLong.of(1L);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalLong.of(0L)));
    assertTrue(present.equals(OptionalLong.of(1L)));
    assertFalse(present.equals(empty));
    assertTrue(Long.hashCode(1) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
    assertEquals(1L, present.getAsLong());
    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));
}
 
Example 18
Source File: RecordServiceImpl.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public boolean forwardEntry(final Record source, final Record.Builder builder, final String sourceColumn,
        final Schema.Entry entry) {
    switch (entry.getType()) {
    case INT:
        final OptionalInt optionalInt = source.getOptionalInt(sourceColumn);
        optionalInt.ifPresent(v -> builder.withInt(entry, v));
        return optionalInt.isPresent();
    case LONG:
        final OptionalLong optionalLong = source.getOptionalLong(sourceColumn);
        optionalLong.ifPresent(v -> builder.withLong(entry, v));
        return optionalLong.isPresent();
    case FLOAT:
        final OptionalDouble optionalFloat = source.getOptionalFloat(sourceColumn);
        optionalFloat.ifPresent(v -> builder.withFloat(entry, (float) v));
        return optionalFloat.isPresent();
    case DOUBLE:
        final OptionalDouble optionalDouble = source.getOptionalDouble(sourceColumn);
        optionalDouble.ifPresent(v -> builder.withDouble(entry, v));
        return optionalDouble.isPresent();
    case BOOLEAN:
        final Optional<Boolean> optionalBoolean = source.getOptionalBoolean(sourceColumn);
        optionalBoolean.ifPresent(v -> builder.withBoolean(entry, v));
        return optionalBoolean.isPresent();
    case STRING:
        final Optional<String> optionalString = source.getOptionalString(sourceColumn);
        optionalString.ifPresent(v -> builder.withString(entry, v));
        return optionalString.isPresent();
    case DATETIME:
        final Optional<ZonedDateTime> optionalDateTime = source.getOptionalDateTime(sourceColumn);
        optionalDateTime.ifPresent(v -> builder.withDateTime(entry, v));
        return optionalDateTime.isPresent();
    case BYTES:
        final Optional<byte[]> optionalBytes = source.getOptionalBytes(sourceColumn);
        optionalBytes.ifPresent(v -> builder.withBytes(entry, v));
        return optionalBytes.isPresent();
    case RECORD:
        final Optional<Record> optionalRecord = source.getOptionalRecord(sourceColumn);
        optionalRecord.ifPresent(v -> builder.withRecord(entry, v));
        return optionalRecord.isPresent();
    case ARRAY:
        final Optional<Collection<Object>> optionalArray = source.getOptionalArray(Object.class, sourceColumn);
        optionalArray.ifPresent(v -> builder.withArray(entry, v));
        return optionalArray.isPresent();
    default:
        throw new IllegalStateException("Unsupported entry type: " + entry);
    }
}
 
Example 19
Source File: RecordJsonMapper.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public void onLong(final Schema.Entry entry, final OptionalLong optionalLong) {
    optionalLong.ifPresent(v -> builder.add(entry.getName(), v));
}
 
Example 20
Source File: OptionalLongExample.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void optional_long_ifPresent() {
	
	OptionalLong optionalLong = OptionalLong.of(56);

	optionalLong.ifPresent(p -> System.out.println(p));
	
	// or
	
	optionalLong.ifPresent(System.out::println);
}