Example usage for java.util Optional of

List of usage examples for java.util Optional of

Introduction

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

Prototype

public static <T> Optional<T> of(T value) 

Source Link

Document

Returns an Optional describing the given non- null value.

Usage

From source file:org.openmhealth.shim.withings.mapper.WithingsBodyHeightDataPointMapper.java

@Override
public Optional<Measure.Builder<BodyHeight, ?>> newMeasureBuilder(JsonNode measuresNode) {

    Optional<BigDecimal> value = getValueForMeasureType(measuresNode, BODY_HEIGHT);

    if (!value.isPresent()) {
        return empty();
    }//from   www  . j ava2  s .co  m

    return Optional.of(new BodyHeight.Builder(new LengthUnitValue(METER, value.get())));
}

From source file:com.facebook.buck.util.unarchive.Untar.java

public static Untar xzUnarchiver() {
    return new Untar(Optional.of(CompressorStreamFactory.XZ));
}

From source file:com.macrossx.wechat.impl.WechatHelper.java

/**
 * {@code _token_time}<tokentoken(7200-60)
 *///  w w  w.j  av a 2s  . co m
public Optional<WechatAccessToken> getAccessToken() {
    try {
        if (System.currentTimeMillis() < _token_time) {
            return Optional.of(_access_token);
        }
        long current = System.currentTimeMillis();
        HttpGet httpGet = new HttpGet();
        httpGet.setURI(new URI(MessageFormat.format(WechatConstants.ACCESS_TOKEN_URL, appid, appsecret)));
        return new WechatHttpClient().send(httpGet, WechatAccessToken.class).map((e) -> {
            _access_token = e;
            _token_time = current + _access_token.getExpires_in() - 60;
            return _access_token;
        });
    } catch (URISyntaxException e) {
        log.info(e.getMessage());
        return Optional.empty();
    }

}

From source file:org.openmhealth.shim.ihealth.mapper.IHealthBodyMassIndexDataPointMapper.java

@Override
protected Optional<String> getMeasureUnitNodeName() {
    return Optional.of("WeightUnit");
}

From source file:com.comphenix.protocol.events.PacketMetadata.java

public static <T> Optional<T> get(Object packet, String key) {
    Validate.notNull(key, "Null keys are not permitted!");

    if (META_CACHE == null) {
        return Optional.empty();
    }/*from   ww w.  j  a va 2  s.  co  m*/

    List<MetaObject> meta = META_CACHE.getIfPresent(packet);
    if (meta == null) {
        return Optional.empty();
    }

    for (MetaObject object : meta) {
        if (object.key.equals(key)) {
            return Optional.of((T) object.value);
        }
    }

    return Optional.empty();
}

From source file:it.unibo.alchemist.SupportedIncarnations.java

/**
 * Fetches an incarnation whose name matches the supplied string.
 * //from   w w w  . ja v a  2 s .c  om
 * @param s
 *            the name of the {@link Incarnation}
 * @param <T>
 *            {@link Concentration} type
 * @return an {@link Optional} containing the incarnation, if one with a
 *         matching name exists
 */
@SuppressWarnings("unchecked")
public static <T> Optional<Incarnation<T>> get(final String s) {
    final String cmp = preprocess(s);
    return INCARNATIONS.stream()
            .map(clazz -> new Pair<>(METRIC.distance(preprocess(clazz.getSimpleName()), cmp), clazz))
            .min((p1, p2) -> Double.compare(p1.getFirst(), p2.getFirst())).map(Pair::getSecond)
            .flatMap(clazz -> {
                try {
                    return Optional.of(clazz.newInstance());
                } catch (Exception e) {
                    L.error("Unable to instance incarnation " + clazz + " (closest match to " + s + " among "
                            + INCARNATIONS + ")", e);
                    return Optional.empty();
                }
            });
}

From source file:org.openmhealth.shim.runkeeper.mapper.RunkeeperCaloriesBurnedDataPointMapper.java

@Override
protected Optional<DataPoint<CaloriesBurned>> asDataPoint(JsonNode itemNode) {

    Optional<CaloriesBurned> caloriesBurned = getMeasure(itemNode);

    if (caloriesBurned.isPresent()) {
        return Optional
                .of(new DataPoint<>(getDataPointHeader(itemNode, caloriesBurned.get()), caloriesBurned.get()));
    } else {/*www  .  j a va  2  s . c o  m*/
        return Optional.empty(); // return empty if there was no calories information to generate a datapoint
    }

}

From source file:org.trustedanalytics.cloud.cc.api.CcSpace.java

@JsonIgnore
public UUID getGuid() {
    Optional<CcSpace> space = Optional.of(this);
    return space.map(CcSpace::getMetadata).map(CcMetadata::getGuid).orElse(null);
}

From source file:ch.zweivelo.renderer.simple.shapes.Plane.java

@Override
public Optional<Double> calculateIntersectionDistance(final Ray ray) {
    double numerator = origin.subtract(ray.getOrigin()).dotProduct(normal);
    double denominator = ray.getDirection().dotProduct(normal);

    if (denominator == 0d) {
        if (numerator != 0d) {
            return Optional.empty();
        }//  www.  j  a  va  2 s.  c o m
        return Optional.of(ray.getInterval().getStart());
    }

    double distance = numerator / denominator;
    return ray.isValidT(distance) ? Optional.of(distance) : Optional.empty();
}

From source file:de.mas.wiiu.jnus.utils.FSTUtils.java

public static Optional<FSTEntry> getFileEntryDir(FSTEntry curEntry, String string) {
    string = string.replace(File.separator, "/");

    // We add the "/" at the end so we don't get false results when using the "startWith" function.
    if (!string.endsWith("/")) {
        string += "/";
    }//from w  ww . j a v  a 2 s.  co  m
    for (val curChild : curEntry.getDirChildren()) {
        String compareTo = curChild.getFullPath();
        if (!compareTo.endsWith("/")) {
            compareTo += "/";
        }
        if (string.startsWith(compareTo)) {
            if (string.equals(compareTo)) {
                return Optional.of(curChild);
            }
            return getFileEntryDir(curChild, string);
        }
    }

    return Optional.empty();
}