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:com.fredhopper.connector.index.provider.AbstractAttributeProvider.java

protected void addValues(final Table<Optional<String>, Optional<Locale>, String> table, final Locale locale,
        final Object values, final MetaAttributeData metaAttribute) {

    if (values != null) {
        final Optional<Locale> loc = locale != null ? Optional.of(locale) : Optional.empty();
        if (values instanceof Collection) {
            for (final Object value : (Collection) values) {
                final Optional<String> valueId = Optional
                        .ofNullable(generateAttributeValueId(metaAttribute, value));
                table.put(valueId, loc, value.toString());
            }/*from   w ww.  j a v a  2s . c  o m*/
        } else {
            final Optional<String> valueId = Optional
                    .ofNullable(generateAttributeValueId(metaAttribute, values));
            table.put(valueId, loc, values.toString());
        }
    }
}

From source file:co.runrightfast.core.utils.ConfigUtils.java

static Optional<Long> getDuration(final Config config, final TimeUnit unit, final String path,
        final String... paths) {
    if (!hasPath(config, path, paths)) {
        return Optional.empty();
    }//from w  w w  .j av a  2s  .  c  o  m
    return Optional.of(config.getDuration(configPath(path, paths), unit));
}

From source file:com.nike.cerberus.service.S3StoreService.java

public Optional<String> get(String path) {
    GetObjectRequest request = new GetObjectRequest(s3Bucket, getFullPath(path));

    try {//from w  w  w. j  a  va  2 s  . c o  m
        S3Object s3Object = s3Client.getObject(request);
        InputStream object = s3Object.getObjectContent();
        return Optional.of(IOUtils.toString(object, ConfigConstants.DEFAULT_ENCODING));
    } catch (AmazonServiceException ase) {
        if (StringUtils.equalsIgnoreCase(ase.getErrorCode(), "NoSuchKey")) {
            logger.debug(String.format("The S3 object doesn't exist. Bucket: %s, Key: %s", s3Bucket,
                    request.getKey()));
            return Optional.empty();
        } else {
            logger.error("Unexpected error communicating with AWS.", ase);
            throw ase;
        }
    } catch (IOException e) {
        String errorMessage = String.format(
                "Unable to read contents of S3 object. Bucket: %s, Key: %s, Expected Encoding: %s", s3Bucket,
                request.getKey(), ConfigConstants.DEFAULT_ENCODING);
        logger.error(errorMessage);
        throw new UnexpectedDataEncodingException(errorMessage, e);
    }
}

From source file:tds.assessment.web.endpoints.ItemControllerIntegrationTests.java

@Test
public void shouldReturnStimulusItemMetadata() throws Exception {
    String url = "/assessments/item/metadata?clientName=SBAC_PT&bankKey=1&stimulusKey=3";

    ItemFileMetadata itemFileMetadata = ItemFileMetadata.create(ItemFileType.STIMULUS, "1-3", "stimulusFile",
            "stimulusFile/");
    when(mockItemService.findItemFileMetadataByStimulusKey("SBAC_PT", 1, 3))
            .thenReturn(Optional.of(itemFileMetadata));

    MvcResult result = http.perform(get(url).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
            .andReturn();/* w  w w  .  jav  a2 s  . c o  m*/

    ItemFileMetadata parsedResponse = objectMapper.readValue(result.getResponse().getContentAsByteArray(),
            ItemFileMetadata.class);
    assertThat(parsedResponse.getId()).isEqualTo("1-3");
    assertThat(parsedResponse.getFileName()).isEqualTo("stimulusFile");
    assertThat(parsedResponse.getFilePath()).isEqualTo("stimulusFile/");
    assertThat(parsedResponse.getItemType()).isEqualTo(ItemFileType.STIMULUS);
}

From source file:io.github.lxgaming.teleportbow.commands.AbstractCommand.java

@Override
public final Optional<Text> getHelp(CommandSource commandSource) {
    return Optional.of(Text.of(TextColors.BLUE, "Use ", TextColors.GREEN, "/", Reference.PLUGIN_ID, " help ",
            TextColors.BLUE, "to view available commands."));
}

From source file:org.xwiki.contrib.repository.npm.internal.NpmExtensionFile.java

@Override
public InputStream openStream() throws IOException {
    if (!webJarFileOptional.isPresent()) {
        webJarFileOptional = Optional.of(downloadJSPackageAndPrepareWebJar());
    }//w ww.j  ava2  s .  c  om
    File webJarFile = webJarFileOptional.get();
    return new FileInputStream(webJarFile);
}

From source file:nu.yona.server.ThymeleafConfiguration.java

private TemplateEngine templateEngine(ResourceBundleMessageSource messageSource,
        AbstractTemplateResolver... templateResolvers) {
    return templateEngine(Optional.of(messageSource), templateResolvers);
}

From source file:de.rnd7.urlcache.URLCacheLoader.java

private Optional<CachedElement> loadFromDisk(final URLCacheKey key) throws IOException {
    final File localFile = this.toLocalFile(key);

    Optional<CachedElement> result = Optional.empty();

    if (localFile.exists()) {
        try (final InputStream in = new FileInputStream(localFile);
                final ObjectInputStream objIn = new ObjectInputStream(in);) {

            final CachedElement element = (CachedElement) objIn.readObject();
            result = Optional.of(element);
        } catch (final ClassNotFoundException e) {
            throw new IOException(e);
        }/*from   ww w  .ja  va2 s . c  om*/
    }

    if (result.isPresent()) {
        if (!result.get().isValid()) {
            localFile.delete();
            result = Optional.empty();
        }
    }

    return result;
}

From source file:io.kamax.mxisd.backend.ldap.LdapAuthProvider.java

private Optional<String> getMsisdn(String phoneNumber) {
    try { // FIXME export into dedicated ThreePid class within SDK (copy from Firebase Auth)
        return Optional.of(phoneUtil.format(phoneUtil.parse(phoneNumber, null // No default region
        ), PhoneNumberUtil.PhoneNumberFormat.E164).substring(1)); // We want without the leading +
    } catch (NumberParseException e) {
        log.warn("Invalid phone number: {}", phoneNumber);
        return Optional.empty();
    }// w w  w  .  j  a va 2s  .  com
}