Example usage for java.util Optional isPresent

List of usage examples for java.util Optional isPresent

Introduction

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

Prototype

public boolean isPresent() 

Source Link

Document

If a value is present, returns true , otherwise false .

Usage

From source file:com.toptal.conf.SecurityUtils.java

/**
 * Finds user in the database with actual username.
 * @return User instance.//from w w w.ja  v  a 2  s .c o  m
 */
public static User actualUser() {
    final UserDao dao = context.getBean(UserDao.class);
    User result = null;
    final Optional<User> vaadin = actualUserVaadin(dao);
    if (vaadin.isPresent()) {
        result = vaadin.get();
    }
    if (result == null) {
        result = dao.findByName(actualUserName());
    }
    return result;
}

From source file:com.synopsys.integration.blackduck.service.model.RequestFactory.java

public static Request.Builder addBlackDuckQuery(Request.Builder requestBuilder,
        Optional<BlackDuckQuery> blackDuckQuery) {
    if (blackDuckQuery.isPresent()) {
        requestBuilder.addQueryParameter(RequestFactory.Q_PARAMETER, blackDuckQuery.get().getParameter());
    }//from  ww  w.j ava  2 s  .co m
    return requestBuilder;
}

From source file:com.toptal.conf.SecurityUtils.java

/**
 * Currently authenticated user name.//from   w ww.  j a va 2 s.  c  om
 * @return User name.
 */
private static String actualUserName() {
    String result = null;
    final Optional<Authentication> auth = getAuthentication();
    if (auth.isPresent()) {
        result = auth.get().getName();
    }
    return result;
}

From source file:com.github.horrorho.inflatabledonkey.cloud.clients.KeyBagClient.java

static Optional<KeyBag> keyBag(CloudKit.RecordRetrieveResponse response, ProtectionZone zone) {
    Optional<byte[]> keyBagData = field(response.getRecord(), "keybagData", zone::decrypt);
    if (!keyBagData.isPresent()) {
        logger.warn("-- keyBag() - failed to acquire key bag");
        return Optional.empty();
    }/*from w w w . j a  v  a  2s .com*/

    Optional<byte[]> secret = field(response.getRecord(), "secret", zone::decrypt);
    if (!secret.isPresent()) {
        logger.warn("-- keyBag() - failed to acquire key bag pass code");
        return Optional.empty();
    }

    KeyBag keyBag = KeyBagFactory.create(keyBagData.get(), secret.get());
    return Optional.of(keyBag);
}

From source file:com.github.horrorho.inflatabledonkey.pcs.xfile.FileAssembler.java

static InputStream inputStream(List<Chunk> chunkData, Optional<byte[]> key) throws UncheckedIOException {
    return key.isPresent() ? inputStream(chunkData, key.get()) : inputStream(chunkData);
}

From source file:com.github.horrorho.inflatabledonkey.cloud.clients.KeyBagClient.java

public static Optional<KeyBag> keyBag(HttpClient httpClient, CloudKitty kitty, ProtectionZone zone,
        String keyBagUUID) throws IOException {
    logger.debug("-- keyBag() - keybag UUID: {}", keyBagUUID);

    List<CloudKit.RecordRetrieveResponse> responses = kitty.recordRetrieveRequest(httpClient, "mbksync",
            "K:" + keyBagUUID);

    if (responses.size() != 1) {
        logger.warn("-- keyBag() - bad response list size: {}", responses);
        return Optional.empty();
    }/*from  w w  w  . j  a  va  2s  .  c  o m*/

    CloudKit.ProtectionInfo protectionInfo = responses.get(0).getRecord().getProtectionInfo();

    Optional<ProtectionZone> optionalNewZone = zone.newProtectionZone(protectionInfo);
    if (!optionalNewZone.isPresent()) {
        logger.warn("-- keyBag() - failed to retrieve protection info");
        return Optional.empty();
    }
    ProtectionZone newZone = optionalNewZone.get();

    return keyBag(responses.get(0), newZone);
}

From source file:com.github.mavogel.ilias.Starter.java

/**
 * Adding a shutdown hook to perform a logout if the user quits the program incorrectly or
 * it is shut down by another process of the system.
 *
 * @param stateMachine the statemachine//from   w  w w .j  a va 2s . co  m
 */
private static void addShutdownHook(final ToolStateMachine stateMachine) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            if (stateMachine != null) {
                Optional<UserDataIds> userDataIds = stateMachine.getUserDataIds();
                IliasEndpoint endPoint = stateMachine.getEndpoint();
                if (userDataIds.isPresent() && endPoint != null && !stateMachine.isInEndState()) {
                    endPoint.logout(" before shutting down!");
                }
            } else {
                LOG.info("No connection had to be closed on shutdown hook!");
            }
        }
    });
}

From source file:com.formkiq.core.form.bean.FormFieldMapper.java

/**
 * Map {@link Object} to {@link FormJSON} by value key.
 * @param source {@link Object}/* w  w w. j a v  a 2s.c om*/
 * @param dest {@link FormJSON}
 * @throws ReflectiveOperationException ReflectiveOperationException
 */
public static void mapByValue(final Object source, final FormJSON dest) throws ReflectiveOperationException {

    BeanUtilsBean bean = ObjectBuilder.getBeanUtils();
    Map<String, String> values = bean.describe(source);

    Collection<FormJSONField> destList = transformToIdMap(dest).values();
    for (FormJSONField d : destList) {
        String val = values.get(d.getValuekey());
        if (val != null) {
            switch (d.getType()) {
            case SELECTBOX:
                Optional<String> op = FormFinder.findOption(d, val);
                if (op.isPresent()) {
                    d.setValue(op.get());
                    break;
                }
                //$FALL-THROUGH$
            default:
                d.setValue(val);
            }
        }
    }
}

From source file:org.rapidpm.microservice.optionals.service.ServiceWrapper.java

private static void shutdownMicroservice() {

    String restBaseUrl = buildBaseUrl();

    Optional<Annotation> pathAnnotation = getAnnotation();

    if (pathAnnotation.isPresent()) {
        sendShutdownToService(restBaseUrl, pathAnnotation);
    } else {// w ww.j  ava2s  . c  om
        LOGGER.warning(
                "Could not locate Path of rest service. Could it be you forgot to add the admin optional?");
        exitHandler.exit(1);
    }

    try {
        Files.delete(MICROSERVICE_REST_FILE);
    } catch (IOException e) {
        LOGGER.warning("Could not delete file " + MICROSERVICE_REST_FILE + "Cleanup file before restart");
        exitHandler.exit(1);
    }

}

From source file:com.github.erchu.beancp.MapperExecutorSelector.java

/**
 * Returns first value in {@code collection} which is not equal to null and matches filter. If
 * there is not such element then will return null.
 *
 * @param <T> result type./*from  w  w  w  .  j a v a  2  s .c o  m*/
 * @param collection collection to search in.
 * @param filter filter predicate.
 * @return first value in {@code collection} which is not equal to null and matches filter, if
 * there is not such element then will return null.
 */
private static <T> T firstOrNull(final Collection<T> collection, final Predicate<T> filter) {
    Optional<T> findFirst = collection.stream().filter(filter).findFirst();

    return (findFirst.isPresent() ? findFirst.get() : null);
}