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

The following examples show how to use java.util.OptionalInt#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: BasicInt.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example 2
Source File: BasicInt.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example 3
Source File: BasicInt.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example 4
Source File: BasicInt.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example 5
Source File: BasicInt.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example 6
Source File: BasicInt.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example 7
Source File: BasicInt.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example 8
Source File: TestDictionaryCompressionOptimizer.java    From presto with Apache License 2.0 6 votes vote down vote up
private TestDictionaryColumn(int bytesPerEntry, double uniquePercentage, OptionalInt maxDictionaryEntries, double valuesPerRow, double nullRate)
{
    checkArgument(bytesPerEntry >= 0, "bytesPerEntry is negative");
    this.bytesPerEntry = bytesPerEntry;

    checkArgument(uniquePercentage >= 0 && uniquePercentage <= 1, "bytesPerEntry must be between 0 and 1");
    this.uniquePercentage = uniquePercentage;

    this.maxDictionaryEntries = requireNonNull(maxDictionaryEntries, "maxDictionaryEntries is null");
    maxDictionaryEntries.ifPresent(value -> checkArgument(value >= 0, "maxDictionaryEntries is negative"));

    checkArgument(valuesPerRow >= 0, "valuesPerRow is negative");
    this.valuesPerRow = valuesPerRow;

    checkArgument(nullRate >= 0 && nullRate <= 1, "nullRate must be between 0 and 1");
    this.nullRate = nullRate;
}
 
Example 9
Source File: BasicInt.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 10
Source File: BasicInt.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 11
Source File: BasicInt.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 12
Source File: Endpoint.java    From besu with Apache License 2.0 5 votes vote down vote up
public Endpoint(final String host, final int udpPort, final OptionalInt tcpPort) {
  checkArgument(
      host != null && InetAddresses.isInetAddress(host), "host requires a valid IP address");
  checkArgument(
      NetworkUtility.isValidPort(udpPort), "UDP port requires a value between 1 and 65535");
  tcpPort.ifPresent(
      p ->
          checkArgument(
              NetworkUtility.isValidPort(p), "TCP port requires a value between 1 and 65535"));

  this.host = host;
  this.udpPort = udpPort;
  this.tcpPort = tcpPort;
}
 
Example 13
Source File: BasicInt.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 14
Source File: DynamicLifespanScheduler.java    From presto with Apache License 2.0 5 votes vote down vote up
public DynamicLifespanScheduler(BucketNodeMap bucketNodeMap, List<InternalNode> allNodes, List<ConnectorPartitionHandle> partitionHandles, OptionalInt concurrentLifespansPerTask)
{
    this.bucketNodeMap = requireNonNull(bucketNodeMap, "bucketNodeMap is null");
    this.allNodes = requireNonNull(allNodes, "allNodes is null");
    this.partitionHandles = unmodifiableList(new ArrayList<>(
            requireNonNull(partitionHandles, "partitionHandles is null")));

    this.concurrentLifespansPerTask = requireNonNull(concurrentLifespansPerTask, "concurrentLifespansPerTask is null");
    concurrentLifespansPerTask.ifPresent(lifespansPerTask -> checkArgument(lifespansPerTask >= 1, "concurrentLifespansPerTask must be great or equal to 1 if present"));

    int bucketCount = partitionHandles.size();
    verify(bucketCount > 0);
    this.driverGroups = new IntArrayList(IntStream.range(0, bucketCount).toArray()).iterator();
}
 
Example 15
Source File: BasicInt.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 16
Source File: BasicInt.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 17
Source File: BasicInt.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
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: AmountInputInterface.java    From luna with MIT License 4 votes vote down vote up
@Override
public final void applyInput(Player player, OptionalInt amount, Optional<String> name) {
    amount.ifPresent(value -> onAmountInput(player, value));
}
 
Example 20
Source File: Equipment.java    From luna with MIT License 4 votes vote down vote up
/**
 * Equips an item from the inventory.
 *
 * @param inventoryIndex The inventory index of the item.
 * Returns {@code true} if successful.
 */
public boolean equip(int inventoryIndex) {

    // Validate index.
    Item inventoryItem = inventory.get(inventoryIndex);
    if (inventoryItem == null) {
        return false;
    }
    EquipmentDefinition equipDef = inventoryItem.getEquipDef();
    int equipIndex = equipDef.getIndex();

    // Check equipment requirements.
    boolean failedToMeet = ifPresent(equipDef.getFailedRequirement(player),
            req -> req.sendFailureMessage(player));
    if (failedToMeet) {
        return false;
    }

    // Unequip something if we have to.
    OptionalInt unequipIndex = OptionalInt.empty();
    if (equipIndex == WEAPON && equipDef.isTwoHanded()) {

        // Equipping 2h weapon, so unequip shield.
        unequipIndex = OptionalInt.of(SHIELD);
    } else if (equipIndex == Equipment.SHIELD &&
            occupied(WEAPON) &&
            get(WEAPON).getEquipDef().isTwoHanded()) {

        // Equipping shield, so unequip 2h weapon.
        unequipIndex = OptionalInt.of(WEAPON);
    }

    // Check if inventory has enough space.
    if (unequipIndex.isPresent()) {
        int remaining = inventory.computeRemainingSize();
        if (remaining == 0 && occupied(unequipIndex.getAsInt()) && occupied(equipIndex)) {
            inventory.fireCapacityExceededEvent();
            return false;
        }
    }

    // Equip item.
    inventory.set(inventoryIndex, null);
    unequipIndex.ifPresent(this::unequip);

    Item equipItem = get(equipIndex);
    if (equipItem == null || inventory.add(equipItem)) {
        set(equipIndex, inventoryItem);
    }
    return true;
}