Example usage for java.util Optional orElse

List of usage examples for java.util Optional orElse

Introduction

In this page you can find the example usage for java.util Optional orElse.

Prototype

public T orElse(T other) 

Source Link

Document

If a value is present, returns the value, otherwise returns other .

Usage

From source file:org.openmhealth.shim.fitbit.mapper.FitbitBodyMassIndexDataPointMapper.java

@Override
protected Optional<DataPoint<BodyMassIndex>> asDataPoint(JsonNode node) {

    TypedUnitValue<BodyMassIndexUnit> bmiValue = new TypedUnitValue<>(KILOGRAMS_PER_SQUARE_METER,
            asRequiredDouble(node, "bmi"));

    BodyMassIndex.Builder builder = new BodyMassIndex.Builder(bmiValue);

    Optional<OffsetDateTime> dateTime = combineDateTimeAndTimezone(node);

    if (dateTime.isPresent()) {
        builder.setEffectiveTimeFrame(dateTime.get());
    }/*  w  ww .j av  a  2 s .c  o m*/

    Optional<Long> externalId = asOptionalLong(node, "logId");

    return Optional.of(newDataPoint(builder.build(), externalId.orElse(null)));
}

From source file:com.vsct.dt.strowgr.admin.gui.cli.InitializationCommand.java

@Override
protected void run(Bootstrap bootstrap, Namespace namespace, StrowgrConfiguration strowgrConfiguration)
        throws Exception {
    CloseableHttpClient httpClient = HttpClients.createDefault();

    NSQHttpClient nsqHttpClient = new NSQHttpClient(
            "http://" + strowgrConfiguration.getNsqProducerFactory().getHost() + ":"
                    + strowgrConfiguration.getNsqProducerFactory().getHttpPort(),
            httpClient);/*from ww  w .j  a  va  2  s.c  o m*/
    ConsulRepository consulRepository = strowgrConfiguration.getConsulRepositoryFactory().build();

    // ports
    Optional<Boolean> portsInitialized = consulRepository.initPorts();
    if (portsInitialized.orElse(Boolean.FALSE)) {
        LOGGER.info("key/value for ports is initialized in repository");
    } else {
        LOGGER.warn("key/value for ports can't be initialized (already done?).");
    }

    // initialize haproxy producer queue
    for (String prefix : Arrays.asList("commit_requested_", "delete_requested_")) {
        String topicName = prefix + namespace.getString("haproxy-name");
        if (nsqHttpClient.createTopic(topicName)) {
            LOGGER.info("topic {} has been initialized on nsqd", topicName);
        } else {
            LOGGER.info("topic {} can't be initialized on nsqd", topicName);
        }
    }

    // initialize vip of an haproxy cluster
    if (namespace.get("vip") != null) {
        consulRepository.setHaproxyProperty(namespace.get("haproxy-name"), "vip", namespace.get("vip"));
    }
}

From source file:org.openmhealth.shim.googlefit.mapper.GoogleFitStepCountDataPointMapper.java

@Override
protected Optional<DataPoint<StepCount>> asDataPoint(JsonNode listNode) {

    JsonNode listValueNode = asRequiredNode(listNode, "value");
    long stepCountValue = asRequiredLong(listValueNode.get(0), "intVal");
    if (stepCountValue == 0) {
        return Optional.empty();
    }/*from   w  w  w .  j a va  2 s . c o  m*/
    StepCount.Builder stepCountBuilder = new StepCount.Builder(stepCountValue);
    setEffectiveTimeFrameIfPresent(stepCountBuilder, listNode);
    StepCount stepCount = stepCountBuilder.build();
    Optional<String> originSourceId = asOptionalString(listNode, "originDataSourceId");
    return Optional.of(newDataPoint(stepCount, originSourceId.orElse(null)));
}

From source file:org.openmhealth.shim.googlefit.mapper.GoogleFitHeartRateDataPointMapper.java

@Override
protected Optional<DataPoint<HeartRate>> asDataPoint(JsonNode listNode) {

    JsonNode valueListNode = asRequiredNode(listNode, "value");
    double heartRateValue = asRequiredDouble(valueListNode.get(0), "fpVal");
    if (heartRateValue == 0) {
        return Optional.empty();
    }//  w ww .  ja  v a  2  s  .  c o  m
    HeartRate.Builder heartRateBuilder = new HeartRate.Builder(heartRateValue);
    setEffectiveTimeFrameIfPresent(heartRateBuilder, listNode);
    HeartRate heartRate = heartRateBuilder.build();
    Optional<String> originDataSourceId = asOptionalString(listNode, "originDataSourceId");
    return Optional.of(newDataPoint(heartRate, originDataSourceId.orElse(null)));
}

From source file:org.trustedanalytics.servicecatalog.service.rest.ApplicationsController.java

@ApiOperation(value = "Removes application, cascade option allows removing bounded service instances for given application", notes = "Privilege level: Consumer of this endpoint must have access to space within application is running."
        + " Verification is performed by Cloud Controller using user token")
@RequestMapping(value = DELETE_APP_URL, method = DELETE)
public void deleteApp(@PathVariable UUID app, @RequestParam(value = "cascade") Optional<Boolean> cascade) {
    if (cascade.orElse(false)) {
        applicationsService.deleteAppCascade(app);
    } else {/*from   w  ww .  j  a va  2  s  .  c  o m*/
        applicationsService.deleteApp(app);
    }
}

From source file:org.openmhealth.shim.fitbit.mapper.FitbitStepCountDataPointMapper.java

@Override
protected Optional<DataPoint<StepCount>> asDataPoint(JsonNode node) {

    int stepCountValue = Integer.parseInt(asRequiredString(node, "value"));

    if (stepCountValue == 0) {
        return Optional.empty();
    }// w ww. ja v  a2 s .  co  m

    StepCount.Builder builder = new StepCount.Builder(stepCountValue);

    Optional<LocalDate> stepDate = asOptionalLocalDate(node, "dateTime");

    if (stepDate.isPresent()) {
        LocalDateTime startDateTime = stepDate.get().atTime(0, 0, 0, 0);

        builder.setEffectiveTimeFrame(TimeInterval.ofStartDateTimeAndDuration(
                combineDateTimeAndTimezone(startDateTime), new DurationUnitValue(DAY, 1)));
    }

    StepCount measure = builder.build();
    Optional<Long> externalId = asOptionalLong(node, "logId");

    return Optional.of(newDataPoint(measure, externalId.orElse(null)));
}

From source file:spring.travel.site.controllers.GeoLocator.java

public Location locate(Request request) {
    Optional<Location> location = request.getUser().flatMap(u -> u.getAddress().map(a -> a.getLocation()));

    // if no user data, try ip address

    return location.orElse(centralLondon);
}

From source file:com.netflix.spinnaker.echo.artifacts.TemplateBasedArtifactExtractor.java

@Autowired
public TemplateBasedArtifactExtractor(Optional<WebhookProperties> webhookProperties,
        ObjectMapper objectMapper) {//from w  ww .  j  a  v a2 s.  c o m
    this.webhookProperties = webhookProperties.orElse(null);
    this.objectMapper = objectMapper;
}

From source file:org.openmhealth.shim.fitbit.mapper.FitbitSleepDurationDataPointMapper.java

@Override
protected Optional<DataPoint<SleepDuration>> asDataPoint(JsonNode node) {

    DurationUnitValue unitValue = new DurationUnitValue(MINUTE, asRequiredDouble(node, "minutesAsleep"));
    SleepDuration.Builder sleepDurationBuilder = new SleepDuration.Builder(unitValue);

    Optional<LocalDateTime> localStartTime = asOptionalLocalDateTime(node, "startTime");

    if (localStartTime.isPresent()) {

        OffsetDateTime offsetStartDateTime = combineDateTimeAndTimezone(localStartTime.get());
        Optional<Double> timeInBed = asOptionalDouble(node, "timeInBed");

        if (timeInBed.isPresent()) {
            sleepDurationBuilder.setEffectiveTimeFrame(TimeInterval.ofStartDateTimeAndDuration(
                    offsetStartDateTime, new DurationUnitValue(MINUTE, timeInBed.get())));
        } else {/*from  w w  w . ja  v  a 2  s.  c  o  m*/
            // in this case, there is no "time in bed" value, however we still have a start time, so we can set
            // the data point to a single date time point
            sleepDurationBuilder.setEffectiveTimeFrame(offsetStartDateTime);
        }
    }

    SleepDuration measure = sleepDurationBuilder.build();

    Optional<Long> externalId = asOptionalLong(node, "logId");

    return Optional.of(newDataPoint(measure, externalId.orElse(null)));
}

From source file:org.openmhealth.shim.googlefit.mapper.GoogleFitBodyHeightDataPointMapper.java

@Override
public Optional<DataPoint<BodyHeight>> asDataPoint(JsonNode listNode) {

    JsonNode valueListNode = asRequiredNode(listNode, getValueListNodeName());
    double bodyHeightValue = asRequiredDouble(valueListNode.get(0), "fpVal");

    if (bodyHeightValue == 0) {
        return Optional.empty();
    }/*from  w ww  . ja v a  2  s. co m*/

    BodyHeight.Builder bodyHeightBuilder = new BodyHeight.Builder(new LengthUnitValue(METER, bodyHeightValue));

    setEffectiveTimeFrameIfPresent(bodyHeightBuilder, listNode);

    BodyHeight bodyHeight = bodyHeightBuilder.build();
    Optional<String> originDataSourceId = asOptionalString(listNode, "originDataSourceId");

    return Optional.of(newDataPoint(bodyHeight, originDataSourceId.orElse(null)));
}