Example usage for java.util Optional orElseThrow

List of usage examples for java.util Optional orElseThrow

Introduction

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

Prototype

public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X 

Source Link

Document

If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.

Usage

From source file:org.kitodo.dataaccess.storage.memory.MemoryNode.java

@Override
public MemoryResult getLast() {
    Optional<Long> last = last();
    if (last.isPresent()) {
        return get(last.orElseThrow(() -> {
            return new IllegalStateException();
        }));//from  w w w .  j  av a 2  s .co  m
    } else {
        return MemoryResult.EMPTY;
    }
}

From source file:org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl.java

@Override
public boolean isTypeOf(NodeTemplate nodeTemplate, String nodeType, ServiceTemplate serviceTemplate,
        ToscaServiceModel toscaServiceModel) {
    if (nodeTemplate == null) {
        return false;
    }/*from   w ww .  j a va 2 s  .  c  om*/

    if (isNodeTemplateOfTypeNodeType(nodeTemplate, nodeType)) {
        return true;
    }

    Optional<Boolean> nodeTypeExistInServiceTemplateHierarchy = isNodeTypeExistInServiceTemplateHierarchy(
            nodeType, nodeTemplate.getType(), serviceTemplate, toscaServiceModel, null);
    return nodeTypeExistInServiceTemplateHierarchy.orElseThrow(
            () -> new CoreException(new ToscaNodeTypeNotFoundErrorBuilder(nodeTemplate.getType()).build()));
}

From source file:org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl.java

@Override
public Optional<Map.Entry<String, NodeTemplate>> getSubstitutionMappedNodeTemplateByExposedReq(
        String substituteServiceTemplateFileName, ServiceTemplate substituteServiceTemplate,
        String requirementId) {//  w  w w. j av  a 2  s.c o m
    if (isSubstitutionServiceTemplate(substituteServiceTemplateFileName, substituteServiceTemplate)) {
        Map<String, List<String>> substitutionMappingRequirements = substituteServiceTemplate
                .getTopology_template().getSubstitution_mappings().getRequirements();
        if (substitutionMappingRequirements != null) {
            List<String> requirementMapping = substitutionMappingRequirements.get(requirementId);
            if (requirementMapping != null && !requirementMapping.isEmpty()) {
                String mappedNodeTemplateId = requirementMapping.get(0);
                Optional<NodeTemplate> mappedNodeTemplate = getNodeTemplateById(substituteServiceTemplate,
                        mappedNodeTemplateId);
                mappedNodeTemplate.orElseThrow(() -> new CoreException(
                        new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", mappedNodeTemplateId)
                                .build()));
                Map.Entry<String, NodeTemplate> mappedNodeTemplateEntry = new Map.Entry<String, NodeTemplate>() {
                    @Override
                    public String getKey() {
                        return mappedNodeTemplateId;
                    }

                    @Override
                    public NodeTemplate getValue() {
                        return mappedNodeTemplate.get();
                    }

                    @Override
                    public NodeTemplate setValue(NodeTemplate value) {
                        return null;
                    }
                };
                return Optional.of(mappedNodeTemplateEntry);
            }
        }
    }
    return Optional.empty();
}

From source file:org.sakaiproject.contentreview.impl.hbm.BaseReviewServiceImpl.java

@Override
public boolean saveOrUpdateActivityConfigEntry(String name, String value, String activityId, String toolId,
        int providerId, boolean overrideIfSet) {
    if (StringUtils.isBlank(name) || StringUtils.isBlank(value) || StringUtils.isBlank(activityId)
            || StringUtils.isBlank(toolId)) {
        return false;
    }/*ww w.java  2s.  c om*/

    Optional<ContentReviewActivityConfigEntry> optEntry = getActivityConfigEntry(name, activityId, toolId,
            providerId);
    if (!optEntry.isPresent()) {
        try {
            dao.create(new ContentReviewActivityConfigEntry(name, value, activityId, toolId, providerId));
            return true;
        } catch (DataIntegrityViolationException | ConstraintViolationException e) {
            // there is a uniqueness constraint on entry keys in the database
            // a row with the same key was written after we checked, retrieve new data and continue
            optEntry = getActivityConfigEntry(name, activityId, toolId, providerId);
        }
    }

    if (overrideIfSet) {
        ContentReviewActivityConfigEntry entry = optEntry.orElseThrow(() -> new RuntimeException(
                "Unique constraint violated during insert attempt, yet unable to retrieve row."));
        entry.setValue(value);
        dao.update(entry);
        return true;
    }

    return false;
}

From source file:org.trellisldp.http.impl.GetHandler.java

private Optional<String> getBinaryDigest(final IRI dsid, final String algorithm) {
    final Optional<InputStream> b = binaryService.getContent(req.getPartition(), dsid);
    try (final InputStream is = b
            .orElseThrow(() -> new WebApplicationException("Couldn't fetch binary content"))) {
        return binaryService.digest(algorithm, is);
    } catch (final IOException ex) {
        LOGGER.error("Error computing digest on content: {}", ex.getMessage());
        throw new WebApplicationException("Error handling binary content: " + ex.getMessage());
    }//from www. j a  v a  2s .  co m
}

From source file:processing.app.debug.Compiler.java

static public File findCompiledSketch(PreferencesMap prefs) throws PreferencesMapException {
    List<String> paths = Arrays.asList("{build.path}/sketch/{build.project_name}.with_bootloader.hex",
            "{build.path}/sketch/{build.project_name}.hex",
            "{build.path}/{build.project_name}.with_bootloader.hex", "{build.path}/{build.project_name}.hex",
            "{build.path}/sketch/{build.project_name}.bin", "{build.path}/{build.project_name}.bin");
    Optional<File> sketch = paths.stream().map(path -> StringReplacer.replaceFromMapping(path, prefs))
            .map(File::new).filter(File::exists).findFirst();
    return sketch.orElseThrow(() -> new IllegalStateException(_("No compiled sketch found")));
}

From source file:processing.app.debug.OldCompiler.java

static public File findCompiledSketch(PreferencesMap prefs) throws PreferencesMapException {
    List<String> paths = Arrays.asList("{build.path}/sketch/{build.project_name}.with_bootloader.hex",
            "{build.path}/sketch/{build.project_name}.hex",
            "{build.path}/{build.project_name}.with_bootloader.hex", "{build.path}/{build.project_name}.hex",
            "{build.path}/sketch/{build.project_name}.bin", "{build.path}/{build.project_name}.bin");
    Optional<File> sketch = paths.stream().map(path -> StringReplacer.replaceFromMapping(path, prefs))
            .map(File::new).filter(File::exists).findFirst();
    return sketch.orElseThrow(() -> new IllegalStateException(tr("No compiled sketch found")));
}