Example usage for java.util Optional ofNullable

List of usage examples for java.util Optional ofNullable

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public static <T> Optional<T> ofNullable(T value) 

Source Link

Document

Returns an Optional describing the given value, if non- null , otherwise returns an empty Optional .

Usage

From source file:com.spotify.ffwd.generated.GeneratedInputPlugin.java

@JsonCreator
public GeneratedInputPlugin(@JsonProperty("sameHost") Boolean sameHost) {
    this.sameHost = Optional.ofNullable(sameHost).orElse(false);
}

From source file:com.tesobe.obp.transport.json.nov2016.ParameterDecoder.java

@Override
public Optional<String> get(String name) {
    return Optional.ofNullable(json.optString(name, null));
}

From source file:de.adorsys.multibanking.hbci.job.LoadBalanceJob.java

public static List<BankAccount> loadBalances(LoadBalanceRequest loadBalanceRequest) {
    HbciDialogRequest dialogRequest = HbciDialogRequest.builder()
            .bankCode(loadBalanceRequest.getBankCode() != null ? loadBalanceRequest.getBankCode()
                    : loadBalanceRequest.getBankAccess().getBankCode())
            .customerId(loadBalanceRequest.getBankAccess().getBankLogin())
            .login(loadBalanceRequest.getBankAccess().getBankLogin2())
            .hbciPassportState(loadBalanceRequest.getBankAccess().getHbciPassportState())
            .pin(loadBalanceRequest.getPin()).build();

    dialogRequest.setProduct(Optional.ofNullable(loadBalanceRequest.getProduct())
            .map(product -> new Product(product.getName(), product.getVersion())).orElse(null));
    dialogRequest.setBpd(loadBalanceRequest.getBpd());

    HBCIDialog dialog = HbciDialogFactory.createDialog(null, dialogRequest);

    Map<AbstractHBCIJob, BankAccount> jobs = new HashMap<>();

    loadBalanceRequest.getBankAccounts().stream().forEach(bankAccount -> {
        Konto account = createAccount(dialog, bankAccount);
        jobs.put(createBalanceJob(dialog, account), bankAccount);
    });/*from   w w  w . j  a  va 2 s .co m*/

    // Let the Handler submitAuthorizationCode all jobs in one batch
    HBCIExecStatus status = dialog.execute(true);
    if (!status.isOK()) {
        log.error("Status of balance job not OK " + status);

        if (initFailed(status)) {
            throw new HBCI_Exception(status.getErrorString());
        }
    }

    List<BankAccount> bankAccounts = new ArrayList<>();
    jobs.keySet().stream().forEach(job -> {
        if (job.getJobResult().getJobStatus().hasErrors()) {
            log.error("Balance job not OK");
            throw new HBCI_Exception(job.getJobResult().getJobStatus().getErrorString());
        }
        BankAccount bankAccount = jobs.get(job);
        bankAccount.setBalances(HbciMapping.createBalance((GVRSaldoReq) job.getJobResult()));
        bankAccounts.add(bankAccount);
    });
    return bankAccounts;
}

From source file:com.cognifide.qa.bb.email.SubjectSearchTerm.java

@Override
public boolean match(Message msg) {
    return Optional.ofNullable(msg).map(this::getSubjectFromMessage)
            .map(emailSubject -> StringUtils.equals(emailSubject, subject)).orElse(false);
}

From source file:fi.helsinki.opintoni.security.authorization.portfolio.PublicVisibilityResolver.java

public Optional<PublicVisibility> resolve(Object handler) {
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    try {//w w  w .  j  a v a 2s.  com
        return Optional.ofNullable(
                findAnnotation(Class.forName(handlerMethod.getBeanType().getName()), PublicVisibility.class));
    } catch (ClassNotFoundException e) {
        return Optional.empty();
    }
}

From source file:io.github.carlomicieli.footballdb.starter.parsers.TeamRosterParser.java

private static Optional<Element> rosterTable(Document doc) {
    Element table = doc.select("#result").first();
    return Optional.ofNullable(table);
}

From source file:com.example.config.MyProperties.java

public Optional<String> fileName() {
    return Optional.ofNullable(nest).flatMap(NestProperties::fileName);
}

From source file:com.microsoft.azure.hdinsight.sdk.rest.ObjectConvertUtils.java

public static <T> Optional<T> convertJsonToObject(@NotNull String jsonString, @NotNull Class<T> tClass)
        throws IOException {
    return Optional.ofNullable(objectMapper.readValue(jsonString, tClass));
}

From source file:co.runrightfast.vertx.core.eventbus.MessageHeader.java

public static Optional<Instant> getMessageTimestamp(@NonNull final Message message) {
    return Optional.ofNullable(message.headers().get(MESSAGE_TIMESTAMP.header)).map(Instant::parse);
}

From source file:com.ikanow.aleph2.data_model.utils.TimeUtils.java

/** The simplest date parsing utility - only handles daily/hourly/monthly type strings (1d, d, daily, day - etc). Note "m" is ambiguous and not supported, use "min" or "month"
 * @param human_readable_period - daily/hourly/monthly type strings (1d, d, daily, day - etc). Note "m" is ambiguous and not supported, use "min" or "month"
 * @return a ChronoUnit if successful, else a generic error string
 */// w  ww  .j av a  2 s . c  o  m
public static Validation<String, ChronoUnit> getTimePeriod(final String human_readable_period) {
    return Patterns
            .match(Optional.ofNullable(human_readable_period).orElse("").toLowerCase().replaceAll("\\s+", ""))
            .<Validation<String, ChronoUnit>>andReturn()
            .when(d -> d.equals("1d"), __ -> Validation.success(ChronoUnit.DAYS))
            .when(d -> d.equals("d"), __ -> Validation.success(ChronoUnit.DAYS))
            .when(d -> d.equals("1day"), __ -> Validation.success(ChronoUnit.DAYS))
            .when(d -> d.equals("day"), __ -> Validation.success(ChronoUnit.DAYS))
            .when(d -> d.equals("daily"), __ -> Validation.success(ChronoUnit.DAYS))

            .when(d -> d.equals("1w"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("w"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("1wk"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("wk"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("1week"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("week"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("weekly"), __ -> Validation.success(ChronoUnit.WEEKS))

            .when(d -> d.equals("1month"), __ -> Validation.success(ChronoUnit.MONTHS))
            .when(d -> d.equals("month"), __ -> Validation.success(ChronoUnit.MONTHS))
            .when(d -> d.equals("monthly"), __ -> Validation.success(ChronoUnit.MONTHS))

            .when(d -> d.equals("1sec"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("sec"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("1s"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("s"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("1second"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("second"), __ -> Validation.success(ChronoUnit.SECONDS))

            .when(d -> d.equals("1min"), __ -> Validation.success(ChronoUnit.MINUTES))
            .when(d -> d.equals("min"), __ -> Validation.success(ChronoUnit.MINUTES))
            .when(d -> d.equals("1minute"), __ -> Validation.success(ChronoUnit.MINUTES))
            .when(d -> d.equals("minute"), __ -> Validation.success(ChronoUnit.MINUTES))

            .when(d -> d.equals("1h"), __ -> Validation.success(ChronoUnit.HOURS))
            .when(d -> d.equals("h"), __ -> Validation.success(ChronoUnit.HOURS))
            .when(d -> d.equals("1hour"), __ -> Validation.success(ChronoUnit.HOURS))
            .when(d -> d.equals("hour"), __ -> Validation.success(ChronoUnit.HOURS))
            .when(d -> d.equals("hourly"), __ -> Validation.success(ChronoUnit.HOURS))

            .when(d -> d.equals("1y"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("y"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("1year"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("year"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("1yr"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("yr"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("yearly"), __ -> Validation.success(ChronoUnit.YEARS))

            .otherwise(__ -> Validation
                    .fail(ErrorUtils.get(ErrorUtils.INVALID_DATETIME_FORMAT, human_readable_period)));
}