Java Code Examples for java8.util.Optional#ifPresent()

The following examples show how to use java8.util.Optional#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: MultiSelectDialogFactory.java    From ground-android with Apache License 2.0 5 votes vote down vote up
public DialogState(MultipleChoice multipleChoice, Optional<Response> initialResponse) {
  ImmutableList<Option> options = multipleChoice.getOptions();
  checkedItems = new boolean[options.size()];
  // TODO: Check cast.
  initialResponse.ifPresent(
      r ->
          IntStreams.range(0, options.size())
              .forEach(
                  i ->
                      checkedItems[i] =
                          ((MultipleChoiceResponse) r).isSelected(options.get(i))));
}
 
Example 2
Source File: GuiInterface.java    From settlers-remake with MIT License 5 votes vote down vote up
private void handleBuildAction(BuildAction buildAction) {
	this.setSelection(new SelectionSet());
	EBuildingType buildingType = buildAction.getBuildingType();

	Optional<ShortPoint2D> position = grid.getConstructablePosition(buildAction.getPosition(), buildingType, playerId);
	position.ifPresent(pos -> scheduleTask(new ConstructBuildingTask(EGuiAction.BUILD, playerId, pos, buildingType)));
	System.out.println("build " + buildingType + " at " + position);
}
 
Example 3
Source File: ObservationListFragment.java    From ground-android with Apache License 2.0 4 votes vote down vote up
private void onFeatureSelected(Optional<Feature> feature) {
  observationListAdapter.clear();
  feature.ifPresent(viewModel::loadObservationList);
}
 
Example 4
Source File: Basic.java    From streamsupport with GNU General Public License v2.0 4 votes vote down vote up
@Test(groups = "unit")
public void testPresent() {
    Optional<Boolean> empty = Optional.empty();
    Optional<String> presentEmptyString = Optional.of("");
    Optional<Boolean> present = Optional.of(Boolean.TRUE);

    // present
    assertTrue(present.equals(present));
    assertTrue(present.equals(Optional.of(Boolean.TRUE)));
    assertTrue(!present.equals(empty));
    assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
    assertTrue(!present.toString().isEmpty());
    assertTrue(!present.toString().equals(presentEmptyString.toString()));
    assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
    assertSame(Boolean.TRUE, present.get());
    try {
        present.ifPresent(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean v) {
                throw new ObscureException();
            }
        });
        fail();
    } catch(ObscureException expected) {

    }
    assertSame(Boolean.TRUE, present.orElse(null));
    assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
    assertSame(Boolean.TRUE, present.orElseGet(null));
    assertSame(Boolean.TRUE, present.orElseGet(new Supplier<Boolean>() {
        @Override
        public Boolean get() {
            return null;
        }
    }));
    assertSame(Boolean.TRUE, present.orElseGet(new Supplier<Boolean>() {
        @Override
        public Boolean get() {
            return Boolean.FALSE;
        }
    }));
    assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow( null));
    assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(new Supplier<RuntimeException>() {
        @Override
        public RuntimeException get() {
            return new ObscureException();
        }
    }));
}