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

The following examples show how to use java8.util.Optional#get() . 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: ProjectRepository.java    From ground-android with Apache License 2.0 6 votes vote down vote up
private Flowable<Loadable<Project>> loadProject(Optional<String> projectId) {
  // Empty id indicates intent to deactivate the current project. Used on sign out.
  if (projectId.isEmpty()) {
    return Flowable.just(Loadable.notLoaded());
  }
  String id = projectId.get();

  return syncProjectWithRemote(id)
      .doOnSubscribe(__ -> Log.d(TAG, "Activating project " + id))
      .doOnError(err -> Log.d(TAG, "Error loading project from remote", err))
      .onErrorResumeNext(
          __ ->
              localDataStore
                  .getProjectById(id)
                  .toSingle()
                  .doOnError(err -> Log.d(TAG, "Error loading project from local db", err)))
      .doOnSuccess(__ -> localValueStore.setLastActiveProjectId(id))
      .toFlowable()
      .compose(Loadable::loadingOnceAndWrap);
}
 
Example 2
Source File: MainGrid.java    From settlers-remake with MIT License 5 votes vote down vote up
@Override
public FerryEntrance ferryAtPosition(ShortPoint2D position, byte playerId) {
	Optional<ILogicMovable> ferryOptional = HexGridArea.stream(position.x, position.y, 0, Constants.MAX_FERRY_ENTRANCE_SEARCH_DISTANCE)
													   .filterBounds(width, height)
													   .filter((x, y) -> landscapeGrid.getLandscapeTypeAt(x, y).isWater())
													   .iterateForResult((x, y) -> {
														   ILogicMovable movable = movableGrid.getMovableAt(x, y);
														   return Optional.ofNullable(movable).filter(m -> m.getMovableType() == EMovableType.FERRY);
													   });

	if (!ferryOptional.isPresent()) {
		return null;
	}

	ILogicMovable ferry = ferryOptional.get();
	ShortPoint2D ferryPosition = ferry.getPosition();
	Optional<ShortPoint2D> entranceOptional = HexGridArea.stream(ferryPosition.x, ferryPosition.y, 0, Constants.MAX_FERRY_ENTRANCE_SEARCH_DISTANCE)
														 .filterBounds(width, height)
														 .filter((x, y) -> !isBlocked(x, y))
														 .getFirst();

	if (!entranceOptional.isPresent()) {
		return null;
	}

	return new FerryEntrance(ferry, entranceOptional.get());
}
 
Example 3
Source File: OriginalMapFileContentReader.java    From settlers-remake with MIT License 5 votes vote down vote up
private MapResourceInfo findAndDecryptFilePartSafe(EOriginalMapFilePartType partType) throws MapLoadException {
	Optional<MapResourceInfo> filePart = findAndDecryptFilePart(partType);
	if (filePart.isPresent()) {
		return filePart.get();
	} else {
		throw new MapLoadException("No " + partType + " information available in mapfile!");
	}
}
 
Example 4
Source File: OriginalMapFileContentReader.java    From settlers-remake with MIT License 5 votes vote down vote up
void readStacks() throws MapLoadException {
	Optional<MapResourceInfo> filePartOptional = findAndDecryptFilePart(EOriginalMapFilePartType.STACKS);

	if (filePartOptional.isPresent()) {
		MapResourceInfo filePart = filePartOptional.get();
		// - file position
		int pos = filePart.offset;

		// - Number of buildings
		int stackCount = readBEIntFrom(pos);
		pos += 4;

		// - safety check
		if ((stackCount * 8 > filePart.size) || (stackCount < 0)) {
			throw new MapLoadException("wrong number of stacks in map File: " + stackCount);
		}

		// - read all Stacks
		for (int i = 0; i < stackCount; i++) {

			int posX = readBEWordFrom(pos);
			pos += 2;
			int posY = readBEWordFrom(pos);
			pos += 2;

			int stackType = readByteFrom(pos++);
			int count = readByteFrom(pos++);

			pos += 2; // not used - maybe: padding to size of 8 (2 INTs)

			// -------------
			// - update data
			mapData.setStack(posX, posY, stackType, count);
		}
	}
}
 
Example 5
Source File: OriginalMapFileContentReader.java    From settlers-remake with MIT License 5 votes vote down vote up
void readSettlers() throws MapLoadException {
	Optional<MapResourceInfo> filePartOptional = findAndDecryptFilePart(EOriginalMapFilePartType.SETTLERS);

	if (filePartOptional.isPresent()) {
		MapResourceInfo filePart = filePartOptional.get();
		// - file position
		int pos = filePart.offset;

		// - Number of buildings
		int settlerCount = readBEIntFrom(pos);
		pos += 4;

		// - safety check
		if ((settlerCount * 6 > filePart.size) || (settlerCount < 0)) {
			throw new MapLoadException("wrong number of settlers in map File: " + settlerCount);
		}

		// - read all Stacks
		for (int i = 0; i < settlerCount; i++) {

			int party = readByteFrom(pos++);
			int settlerType = readByteFrom(pos++);

			int posX = readBEWordFrom(pos);
			pos += 2;
			int posY = readBEWordFrom(pos);
			pos += 2;

			// -------------
			// - update data
			mapData.setSettler(posX, posY, settlerType, party);
		}
	}
}
 
Example 6
Source File: Basic.java    From streamsupport with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NoSuchElementException.class)
public void testEmptyGet() {
    Optional<Boolean> empty = Optional.empty();

    Boolean got = empty.get();
}
 
Example 7
Source File: OriginalMapFileContentReader.java    From settlers-remake with MIT License 4 votes vote down vote up
void readBuildings() throws MapLoadException {
	hasBuildings = false;

	Optional<MapResourceInfo> filePartOptional = findAndDecryptFilePart(EOriginalMapFilePartType.BUILDINGS);

	if (filePartOptional.isPresent()) {
		MapResourceInfo filePart = filePartOptional.get();
		// - file position
		int pos = filePart.offset;

		// - Number of buildings
		int buildingsCount = readBEIntFrom(pos);
		pos += 4;

		// - safety check
		if ((buildingsCount * 12 > filePart.size) || (buildingsCount < 0)) {
			throw new MapLoadException("wrong number of buildings in map File: " + buildingsCount);
		}

		hasBuildings = true;

		// - read all Buildings
		for (int i = 0; i < buildingsCount; i++) {

			int party = readByteFrom(pos++); // - Party starts with 0
			int buildingType = readByteFrom(pos++);
			int posX = readBEWordFrom(pos);
			pos += 2;
			int posY = readBEWordFrom(pos);
			pos += 2;

			pos++; // not used - maybe a filling byte to make the record 12 Byte (= 3 INTs) long or unknown?!

			// -----------
			// - number of soldier in building is saved as 4-Bit (=Nibble):
			int countSword1 = readHighNibbleFrom(pos);
			int countSword2 = readLowNibbleFrom(pos);
			pos++;

			int countArcher2 = readHighNibbleFrom(pos);
			int countArcher3 = readLowNibbleFrom(pos);
			pos++;

			int countSword3 = readHighNibbleFrom(pos);
			int countArcher1 = readLowNibbleFrom(pos);
			pos++;

			int countSpear3 = readHighNibbleFrom(pos);
			// low nibble is a not used count
			pos++;

			int countSpear1 = readHighNibbleFrom(pos);
			int countSpear2 = readLowNibbleFrom(pos);
			pos++;

			// -------------
			// - update data
			mapData.setBuilding(posX, posY, buildingType, party, countSword1, countSword2, countSword3, countArcher1, countArcher2, countArcher3, countSpear1, countSpear2, countSpear3);
		}
	}
}