Example usage for java.util Optional map

List of usage examples for java.util Optional map

Introduction

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

Prototype

public <U> Optional<U> map(Function<? super T, ? extends U> mapper) 

Source Link

Document

If a value is present, returns an Optional describing (as if by #ofNullable ) the result of applying the given mapping function to the value, otherwise returns an empty Optional .

Usage

From source file:org.trustedanalytics.metricsprovider.cloudadapter.api.CfServiceInstance.java

@JsonIgnore
public String getServicePlanName() {
    Optional<CfServiceInstance> serviceInstance = Optional.of(this);
    return serviceInstance.map(CfServiceInstance::getServicePlan).map(CfServicePlan::getName).orElse(null);
}

From source file:org.trustedanalytics.metricsprovider.cloudadapter.api.CfServiceInstance.java

@JsonIgnore
public UUID getServiceGuid() {
    Optional<CfServiceInstance> serviceInstance = Optional.of(this);
    return serviceInstance.map(CfServiceInstance::getServicePlan).map(CfServicePlan::getService)
            .map(CfService::getGuid).orElse(null);
}

From source file:com.juliuskrah.multipart.service.impl.UserDetailsServiceImpl.java

@Override
@Transactional(readOnly = true)//  w  ww . j  a v  a2s  . c o  m
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    log.debug("Starting authentication for {}...", username);
    String lowercaseUsername = username.toLowerCase(Locale.ENGLISH);
    Optional<Account> userFromDb = accountRepository.findByUsername(lowercaseUsername);
    // @formatter:off
    return userFromDb.map(user -> {
        if (!user.isActivated())
            throw new UserNotActivatedException(String.format("User %s is not activated", lowercaseUsername));
        List<GrantedAuthority> grantedAuthorities = user.getAuthorities().stream()
                .map(authority -> new SimpleGrantedAuthority(authority.getName())).collect(Collectors.toList());
        return new User(lowercaseUsername, user.getPassword(), grantedAuthorities);
    }).orElseThrow(() -> new UsernameNotFoundException(
            String.format("User %s was not found in the database", lowercaseUsername)));
    // @formatter:on
}

From source file:net.pkhsolutions.pecsapp.control.PictureFileStorage.java

@NotNull
private Path getDirectoryForLayout(@NotNull Optional<PageLayout> layout) throws IOException {
    final Path path = directory.resolve(layout.map(PageLayout::name).orElse("raw").toLowerCase());
    try {//from   w w  w . j a v  a 2 s .  co m
        return Files.createDirectories(path);
    } catch (FileAlreadyExistsException ex) {
        return path;
    }
}

From source file:org.trustedanalytics.servicecatalog.service.rest.ApplicationsController.java

@ApiOperation(value = "Get applications from given space", notes = "Privilege level: Consumer of this endpoint must be a member of specified space")
@RequestMapping(value = GET_ALL_APPS_URL, method = GET, produces = APPLICATION_JSON_VALUE)
public Collection<CcApp> getFilteredApplications(@RequestParam(required = false, value = "space") UUID space,
        @RequestParam(value = "service_label") Optional<String> serviceLabel) {
    return serviceLabel
            .map(label -> applicationsService.getSpaceAppsByService(space,
                    service -> (service.getServicePlan() != null)
                            && label.equals(service.getServicePlan().getService().getLabel())))
            .orElse(applicationsService.getSpaceApps(space));
}

From source file:com.github.lukaszkusek.xml.comparator.diff.DifferenceInformation.java

private String getNodeXpath(Optional<Node> node) {
    return node.map(Node::getXPath).orElse("[null]");
}

From source file:com.github.lukaszkusek.xml.comparator.diff.DifferenceInformation.java

private String getNodeValue(Optional<Node> node) {
    return node.map(Node::getValue).orElse("[null]");
}

From source file:com.github.lukaszkusek.xml.comparator.diff.DifferenceInformation.java

private String getNodeAttributeValue(Optional<Node> node) {
    return node.map(Node::getAttributes).map(attributes -> attributes.get(attributeName)).orElse("[null]");
}

From source file:io.divolte.server.recordmapping.DslRecordMapping.java

private static Optional<ValidationError> validateTrivialUnion(final Schema targetSchema,
        final Function<Schema, Boolean> validator, final String messageIfInvalid, Object... messageParameters) {
    final Optional<Schema> resolvedSchema = unpackNullableUnion(targetSchema);
    final Optional<Optional<ValidationError>> unionValidation = resolvedSchema
            .map(s -> validator.apply(s) ? Optional.empty()
                    : validationError(String.format(messageIfInvalid, messageParameters), targetSchema));
    return unionValidation.orElseGet(() -> validationError(
            "mapping to non-trivial unions (" + targetSchema + ") is not supported", targetSchema));
}

From source file:com.github.horrorho.inflatabledonkey.cache.FileCache.java

public Optional<byte[]> load(Path file, byte[] password) throws IOException {
    byte[] bs = null;
    if (Files.exists(file)) {
        try (InputStream is = fileCryptor.newCipherInputStream(Files.newInputStream(file), password)) {
            bs = IOUtils.toByteArray(is);
        } catch (RuntimeCryptoException | InvalidCipherTextIOException ex) {
            logger.debug("-- load() - exception: ", ex);
        }//from   www  .j a  v a 2s.  com
    } else {
        logger.debug("-- load() - no file: {}", file);
    }
    Optional<byte[]> _bs = Optional.ofNullable(bs);
    logger.debug("-- load() - file: {} bs: 0x{}", file, _bs.map(Hex::toHexString));
    return _bs;
}