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:de.adorsys.multibanking.hbci.model.HbciDialogFactory.java

public static HBCIDialog createDialog(HbciPassport passport, HbciDialogRequest dialogRequest) {
    BankInfo bankInfo = Optional.ofNullable(HBCIUtils.getBankInfo(dialogRequest.getBankCode()))
            .orElseThrow(() -> new IllegalArgumentException(
                    "Bank [" + dialogRequest.getBankCode() + "] not " + "supported"));

    HBCIProduct hbciProduct = Optional.ofNullable(dialogRequest.getProduct())
            .map(product -> new HBCIProduct(product.getName(), product.getVersion())).orElse(null);

    HbciPassport newPassport = Optional.ofNullable(passport)
            .orElseGet(() -> createPassport(bankInfo.getPinTanVersion().getId(), dialogRequest.getBankCode(),
                    dialogRequest.getCustomerId(), dialogRequest.getLogin(), hbciProduct,
                    dialogRequest.getCallback()));

    Optional.ofNullable(dialogRequest.getHbciPassportState()).ifPresent(
            s -> HbciPassport.State.fromJson(dialogRequest.getHbciPassportState()).apply(newPassport));

    Optional.ofNullable(dialogRequest.getBpd()).ifPresent(newPassport::setBPD);

    newPassport.setPIN(dialogRequest.getPin());

    String url = bankInfo.getPinTanAddress();
    String proxyPrefix = System.getProperty("proxyPrefix", null);
    if (proxyPrefix != null) {
        url = proxyPrefix + url;//from w w w .java2 s .com
    }
    newPassport.setHost(url);

    return new HBCIDialog(newPassport);
}

From source file:ru.mystamps.web.support.spring.security.SecurityContextUtils.java

/**
 * @author Sergey Chechenev/* ww w . ja v a2  s .  c  o  m*/
 */
public static boolean hasAuthority(GrantedAuthority authority) {
    return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
            .map(Authentication::getAuthorities).orElse(Collections.emptyList()).contains(authority);
}

From source file:com.homeadvisor.kafdrop.util.JmxUtils.java

public static int getJmxPort(final Environment environment) {
    Optional<Integer> jmxPort = Optional.empty();

    final Properties managementProperties = Agent.getManagementProperties();
    if (managementProperties != null) {
        final String portProperty = managementProperties.getProperty(JMX_PORT_PROPERTY);
        if (portProperty != null) {
            final Optional<Integer> port = Optional.ofNullable(Ints.tryParse(portProperty));
            jmxPort = port;/*from  www .  j av  a2 s  .  c o  m*/
        }
    }
    return jmxPort.orElse(0);
}

From source file:controllers.Mock.java

public static F.Promise<Result> mock(final String serviceName, final int vehicleId) {
    final int secsDelayed = Optional.ofNullable(request().getQueryString("secsDelayed"))
            .map(str -> Integer.parseInt(str)).orElse(0);
    final boolean boom = Optional.ofNullable(request().getQueryString("boom")).map(str -> Boolean.valueOf(str))
            .orElse(false);//from  w  ww. j  a  va2s .  c o  m

    if (boom) {
        return F.Promise.throwing(new RuntimeException("boom!!!"));
    }

    if (vehicleId != 1) {
        return F.Promise.pure(notFound(Json.toJson(ImmutableMap.of("error", "content not found!"))));
    }

    switch (serviceName) {
    case "vehicleData":
        return respond(Json.toJson(fakeVehicleData()), secsDelayed);
    case "vehicleImage":
        return respond(Json.toJson(fakeVehicleImage()), secsDelayed);
    case "searchResults":
        return respond(Json.toJson(fakeSearchResults()), secsDelayed);
    default:
        return F.Promise.pure(badRequest(String.format("serviceName %s not supported!", serviceName)));
    }
}

From source file:io.github.retz.scheduler.Applications.java

public static Optional<Application> get(String appName) {
    return Optional.ofNullable(get().get(appName));
}

From source file:com.ethercamp.harmony.service.BlockchainConsts.java

/**
 * Return pair of name and explorer url.
 *///from w  w  w .  ja  v a2  s  .c  o  m
public static Pair<String, Optional<String>> getNetworkInfo(Environment env, String genesisHash) {
    final String networkNameKey = String.format("network.%s.networkName", genesisHash);
    final String explorerUrlKey = String.format("network.%s.explorerUrl", genesisHash);

    return Optional.ofNullable(env.getProperty(networkNameKey))
            .map(name -> Pair.of(name, Optional.ofNullable(env.getProperty(explorerUrlKey))))
            .orElse(Pair.of("Unknown network", Optional.empty()));
}

From source file:Main.java

/**
 * @param collectionOfCollection// w  ww . j a  v  a2 s . c o m
 *                               a {@code Collection&lt;Collection&lt;T&gt;&gt;}
 * @param <T>                    the element type
 *
 * @return a Set&lt;T&gt; containing all values of all Collections&lt;T&gt;
 *         without any duplicates
 */
public static <T> Set<T> unionOfListOfLists(
        Collection<? extends Collection<? extends T>> collectionOfCollection) {
    return Optional.ofNullable(collectionOfCollection).map(Collection::stream).orElseGet(Stream::empty)
            .filter(Objects::nonNull).flatMap(c -> c.stream()).filter(Objects::nonNull)
            .collect(Collectors.toSet());
}

From source file:Main.java

/**
 * @param <T> the element type/*from w ww . jav  a2  s . c o  m*/
 * @param l   the list
 *
 * @return an <b>UNMODIFIABLE</b> List&lt;T&gt;
 */
public static <T> List<? extends T> unmodifiableList(List<? extends T> l) {
    return Optional.ofNullable(l).map(Collections::unmodifiableList).orElseGet(Collections::emptyList);
}

From source file:com.tesobe.obp.transport.json.TokenDecoder.java

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

From source file:Main.java

/**
 * Find an ancestor of a component that matches a condition.
 * @param component the starting component
 * @param condition the condition to match
 * @return the first ancestor for which {@code condition} is true
 *//*from w  w w  .ja  v  a2  s  .  c  o m*/
public static Optional<Component> findAncestor(Component component, Predicate<Component> condition) {
    while (component != null && !condition.test(component)) {
        component = getParent(component);
    }
    return Optional.ofNullable(component);
}