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:lumbermill.internal.geospatial.GeoIPTest.java

@Ignore
@Test//from w ww  . java2  s.  co  m
public void test_geoip_all_fields() {
    GeoIP geoIP = GeoIP.Factory.create("client_ip", Optional.of("geoip"),
            Optional.of(new File("/tmp/GeoLite2-City.mmdb")), Optional.empty());
    JsonEvent event = Codecs.TEXT_TO_JSON.from("Hello").put("client_ip", "37.139.156.40");
    geoIP.decorate(event);

    JsonNode geoip = event.unsafe().get("geoip");
    assertThat(geoip.get("country_code2").asText()).isEqualTo("SE");
    assertThat(geoip.get("country_code3").asText()).isEqualTo("SE");
    assertThat(geoip.get("timezone").asText()).isEqualTo("Europe/Stockholm");
    assertThat(geoip.get("continent_code").asText()).isEqualTo("EU");
    assertThat(geoip.get("continent_name").asText()).isEqualTo("Europe");
    assertThat(geoip.get("country_name").asText()).isEqualTo("Sweden");
    assertThat(geoip.get("city_name").asText()).isEqualTo("Sodra Sandby");
    assertThat(geoip.get("longitude").asDouble()).isEqualTo(13.3333);
    assertThat(geoip.get("latitude").asDouble()).isEqualTo(55.7167);
    ArrayNode location = (ArrayNode) geoip.get("location");
    assertThat(location.get(0).asDouble()).isEqualTo(13.3333);
    assertThat(location.get(1).asDouble()).isEqualTo(55.7167);
    System.out.println(event);
}

From source file:io.dfox.junit.http.example.NoteRepository.java

public synchronized Optional<InputStream> getNote(final String name) throws IOException {
    File note = new File(dir, name);
    if (note.exists()) {
        return Optional.of(IOUtils.toBufferedInputStream(FileUtils.openInputStream(note)));
    } else {/*w  w  w  .j a  va 2  s.  c o m*/
        return Optional.empty();
    }
}

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

/** Returns the sub-object of a config, or empty if
 * @param config the configuration object
 * @param key the root key of the sub object
 * @return/*ww w . jav a  2 s  . c o  m*/
 */
public static Optional<ConfigObject> getSubConfigObject(final Config config, final String key) {
    try {
        return Optional.of(config.getObject(key));
    } catch (Exception e) {
        return Optional.empty();
    }
}

From source file:org.ulyssis.ipp.snapshot.TeamStates.java

public Optional<TeamState> getStateForTeam(int teamNb) {
    if (teamNbToState.containsKey(teamNb)) {
        return Optional.of(teamNbToState.get(teamNb));
    } else {/*from   w  w  w .j  av a  2 s .c o m*/
        return Optional.empty();
    }
}

From source file:it.polimi.diceH2020.launcher.service.Validator.java

public <T> Optional<T> objectFromPath(Path pathFile, Class<T> cls) {

    String serialized;//  w w w.  jav a  2s.c o  m
    try {
        serialized = new String(Files.readAllBytes(pathFile));
        T data = mapper.readValue(serialized, cls);
        return Optional.of(data);
    } catch (IOException e) {
        return Optional.empty();
    }
}

From source file:net.sourceforge.fullsync.cli.CliRuntimeConfiguration.java

public CliRuntimeConfiguration(CommandLine args) {
    profileToRun = parseProfileToRun(args);
    daemon = Optional.of(args.hasOption('d'));
    startMinimized = Optional.of(args.hasOption('m'));
}

From source file:org.openmhealth.shim.jawbone.mapper.JawboneStepCountDataPointMapper.java

@Override
protected Optional<StepCount> getMeasure(JsonNode listEntryNode) {

    long stepCountValue = asRequiredLong(listEntryNode, "details.steps");

    if (stepCountValue <= 0) {
        return Optional.empty();
    }/*w w  w  . j a va  2s .c  om*/

    StepCount.Builder stepCountBuilder = new StepCount.Builder(stepCountValue);

    setEffectiveTimeFrame(stepCountBuilder, listEntryNode);

    return Optional.of(stepCountBuilder.build());
}

From source file:com.twosigma.beakerx.kernel.EvaluatorParameters.java

public <T> Optional<T> getParam(String key, Class<T> clazz) {
    if (params.containsKey(key)) {
        return Optional.of(clazz.cast(params.get(key)));
    }//from w  w w  .j av a2 s . c  o m
    return Optional.empty();
}

From source file:org.zalando.riptide.BufferingClientHttpResponse.java

public static BufferingClientHttpResponse buffer(final ClientHttpResponse response) throws IOException {
    if (response.getBody() == null) {
        return new BufferingClientHttpResponse(response, empty());
    } else {//from   w  w w  .  j  a v  a 2 s.c o  m
        final byte[] bytes = ByteStreams.toByteArray(response.getBody());
        return new BufferingClientHttpResponse(response, Optional.of(bytes));
    }
}

From source file:io.kloudwork.util.MultipartParamHolder.java

public MultipartParamHolder(Map<String, String> parameters, Map<String, FileItemStream> files) {
    this.parameters = parameters;
    this.files = Optional.of(files);
}