Example usage for java.util Optional get

List of usage examples for java.util Optional get

Introduction

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

Prototype

public T get() 

Source Link

Document

If a value is present, returns the value, otherwise throws NoSuchElementException .

Usage

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

/**
 * Finds user in the database with actual username.
 * @return User instance.//  www.  j  ava  2s .co  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.toptal.conf.SecurityUtils.java

/**
 * Currently authenticated user name.//from  www  . ja v  a  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.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.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./*  ww  w. ja v  a  2  s .  c om*/
 * @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);
}

From source file:org.trustedanalytics.hadoop.admin.tools.HadoopClientParamsImporter.java

static void performAction(CLIParameters params) throws IOException, XPathExpressionException {
    Optional<InputStream> inputStreamOptional = getSourceInputStream(params);
    if (inputStreamOptional.isPresent()) {
        try (InputStream sourceStream = inputStreamOptional.get()) {
            LOGGER.info(returnJSON(scanConfigZipArchive(sourceStream).get()));
        }/*from  w  w w  .  ja v  a  2 s.  co  m*/
    }
}

From source file:com.schnobosoft.semeval.cortical.PrintCorrelations.java

private static double getPearson(List<Optional> gold, List<Optional> scores) {
    assert gold.size() == scores.size();

    List<Double> gsList = new ArrayList<>(gold.size());
    List<Double> sList = new ArrayList<>(scores.size());

    for (int i = 0; i < gold.size(); i++) {
        Optional goldOpt = gold.get(i);
        Optional scoreOpt = scores.get(i);
        if (goldOpt.isPresent() && scoreOpt.isPresent()) {
            gsList.add((Double) goldOpt.get());
            sList.add((Double) scoreOpt.get());
        } else {//  w w w.ja  va 2s  .  c  o m
            LOG.warn("No score found in line " + i + ".");
        }
    }
    return new PearsonsCorrelation().correlation(listToArray(gsList), listToArray(sList));
}

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

/**
 * Map {@link Object} to {@link FormJSON} by value key.
 * @param source {@link Object}//from  w  w w  .ja v a 2  s  .c o m
 * @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:com.liferay.blade.cli.util.Prompter.java

public static boolean confirm(String question, InputStream in, PrintStream out,
        Optional<Boolean> defaultAnswer) {
    String questionWithPrompt = _buildQuestionWithPrompt(question, defaultAnswer);

    Optional<Boolean> answer = _getBooleanAnswer(questionWithPrompt, in, out, defaultAnswer);

    if (answer.isPresent()) {
        return answer.get();
    } else {// w w w.  j  av  a 2  s  .  co  m
        throw new NoSuchElementException("Unable to acquire an answer");
    }
}

From source file:com.github.blindpirate.gogradle.crossplatform.Os.java

private static Os detectOs() {
    Optional<Map.Entry<Os, Boolean>> result = OS_DETECTION_MAP.entrySet().stream().filter(Map.Entry::getValue)
            .findFirst();/*  w  w w  .  ja v a 2  s  .co m*/
    if (result.isPresent()) {
        return result.get().getKey();
    }
    throw new IllegalStateException("Unrecognized operation system:" + System.getProperty("os.name"));
}

From source file:io.pravega.controller.store.stream.tables.IndexRecord.java

private static Pair<Integer, Optional<IndexRecord>> binarySearchIndex(final int lower, final int upper,
        final long timestamp, final byte[] indexTable) {
    if (upper < lower || indexTable.length == 0) {
        return new ImmutablePair<>(0, Optional.empty());
    }//  w  w  w .  j a v a  2  s . c  o m

    final int offset = ((lower + upper) / 2) * IndexRecord.INDEX_RECORD_SIZE;

    final IndexRecord record = IndexRecord.readRecord(indexTable, offset).get();

    final Optional<IndexRecord> next = IndexRecord.fetchNext(indexTable, offset);

    if (record.getEventTime() <= timestamp) {
        if (!next.isPresent() || (next.get().getEventTime() > timestamp)) {
            return new ImmutablePair<>(offset, Optional.of(record));
        } else {
            return binarySearchIndex((lower + upper) / 2 + 1, upper, timestamp, indexTable);
        }
    } else {
        return binarySearchIndex(lower, (lower + upper) / 2 - 1, timestamp, indexTable);
    }
}