Example usage for java.util.concurrent ConcurrentMap putIfAbsent

List of usage examples for java.util.concurrent ConcurrentMap putIfAbsent

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentMap putIfAbsent.

Prototype

V putIfAbsent(K key, V value);

Source Link

Document

If the specified key is not already associated with a value, associates it with the given value.

Usage

From source file:com.github.podd.example.ExamplePoddClient.java

private void populateProjectUriMap(final Model currentUnpublishedArtifacts,
        final ConcurrentMap<String, ConcurrentMap<URI, InferredOWLOntologyID>> projectUriMap)
        throws PoddClientException {
    for (final InferredOWLOntologyID nextArtifact : OntologyUtils
            .modelToOntologyIDs(currentUnpublishedArtifacts, true, false)) {
        final Model nextTopObject = this.getTopObject(nextArtifact, currentUnpublishedArtifacts);

        // DebugUtils.printContents(nextTopObject);

        final Model types = nextTopObject.filter(null, RDF.TYPE, PODD.PODD_SCIENCE_PROJECT);
        if (types.isEmpty()) {
            // We only map project based artifacts, others are ignored with
            // log messages here
            this.log.info("Found a non-project based artifact, ignoring as it isn't relevant here: {}",
                    nextArtifact);/* w ww.  java  2 s .  c  o m*/
        } else if (types.subjects().size() > 1) {
            // We only map single-project based artifacts, others are
            // ignored with log messages here
            this.log.error("Found multiple projects for an artifact: {} {}", nextArtifact, types.subjects());
        } else {
            final Resource project = types.subjects().iterator().next();

            if (!(project instanceof URI)) {
                // We only map URI references, as blank nodes which are
                // allowable, cannot be reserialised to update the artifact,
                // and should not exist
                this.log.error("Found non-URI project reference for an artifact: {} {}", nextArtifact,
                        types.subjects());
            } else {
                final Model label = nextTopObject.filter(project, RDFS.LABEL, null);

                // DebugUtils.printContents(label);

                if (label.isEmpty()) {
                    this.log.error("Project did not have a label: {} {}", nextArtifact, project);
                } else {
                    for (final Value nextLabel : label.objects()) {
                        if (!(nextLabel instanceof Literal)) {
                            this.log.error("Project had a non-literal label: {} {} {}", nextArtifact, project,
                                    nextLabel);
                        } else {
                            String nextLabelString = nextLabel.stringValue();

                            // take off any descriptions and leave the
                            // project number behind
                            nextLabelString = nextLabelString.split(" ")[0];

                            final Matcher matcher = ExampleSpreadsheetConstants.REGEX_PROJECT
                                    .matcher(nextLabelString);

                            if (!matcher.matches()) {
                                this.log.error(
                                        "Found project label that did not start with expected format: {}",
                                        nextLabel);
                            } else {
                                this.log.debug(
                                        "Found project label with the expected format: '{}' original=<{}>",
                                        nextLabelString, nextLabel);

                                final int nextProjectYear = Integer.parseInt(matcher.group(1));
                                final int nextProjectNumber = Integer.parseInt(matcher.group(2));

                                nextLabelString = String.format(ExampleSpreadsheetConstants.TEMPLATE_PROJECT,
                                        nextProjectYear, nextProjectNumber);

                                this.log.debug("Reformatted project label to: '{}' original=<{}>",
                                        nextLabelString, nextLabel);

                                ConcurrentMap<URI, InferredOWLOntologyID> labelMap = new ConcurrentHashMap<>();
                                final ConcurrentMap<URI, InferredOWLOntologyID> putIfAbsent = projectUriMap
                                        .putIfAbsent(nextLabelString, labelMap);
                                if (putIfAbsent != null) {
                                    this.log.error(
                                            "Found duplicate project name, inconsistent results may follow: {} {} {}",
                                            nextArtifact, project, nextLabel);
                                    // Overwrite our reference with the one that
                                    // already existed
                                    labelMap = putIfAbsent;
                                }
                                final InferredOWLOntologyID existingArtifact = labelMap
                                        .putIfAbsent((URI) project, nextArtifact);
                                // Check for the case where project name maps to
                                // different artifacts
                                if (existingArtifact != null && !existingArtifact.equals(nextArtifact)) {
                                    this.log.error(
                                            "Found duplicate project name across different projects, inconsistent results may follow: {} {} {} {}",
                                            nextArtifact, existingArtifact, project, nextLabel);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:com.github.podd.example.ExamplePoddClient.java

private void populateExperimentUriMap(
        final ConcurrentMap<String, ConcurrentMap<URI, InferredOWLOntologyID>> projectUriMap,
        final ConcurrentMap<String, ConcurrentMap<URI, URI>> experimentUriMap) throws PoddClientException {
    for (final String nextProjectName : projectUriMap.keySet()) {
        final ConcurrentMap<URI, InferredOWLOntologyID> nextProjectNameMapping = projectUriMap
                .get(nextProjectName);//from   www  .ja  va  2s. c  om
        for (final URI projectUri : nextProjectNameMapping.keySet()) {
            final InferredOWLOntologyID artifactId = nextProjectNameMapping.get(projectUri);
            final Model nextSparqlResults = this.doSPARQL(
                    String.format(ExampleSpreadsheetConstants.TEMPLATE_SPARQL_BY_TYPE,
                            RenderUtils.getSPARQLQueryString(PODD.PODD_SCIENCE_EXPERIMENT)),
                    Arrays.asList(artifactId));

            if (nextSparqlResults.isEmpty()) {
                this.log.info("Could not find any existing experiments for project: {} {}", nextProjectName,
                        projectUri);
            }

            for (final Resource nextExperiment : nextSparqlResults
                    .filter(null, RDF.TYPE, PODD.PODD_SCIENCE_EXPERIMENT).subjects()) {
                if (!(nextExperiment instanceof URI)) {
                    this.log.error("Found experiment that was not assigned a URI: {} artifact={}",
                            nextExperiment, artifactId);
                } else {
                    final Model label = nextSparqlResults.filter(nextExperiment, RDFS.LABEL, null);

                    // DebugUtils.printContents(label);

                    if (label.isEmpty()) {
                        this.log.error("Experiment did not have a label: {} {}", artifactId, nextExperiment);
                    } else {
                        for (final Value nextLabel : label.objects()) {
                            if (!(nextLabel instanceof Literal)) {
                                this.log.error("Project had a non-literal label: {} {} {}", artifactId,
                                        nextExperiment, nextLabel);
                            } else {
                                String nextLabelString = nextLabel.stringValue();

                                // take off any descriptions and leave the
                                // project number behind
                                nextLabelString = nextLabelString.split(" ")[0];

                                final Matcher matcher = ExampleSpreadsheetConstants.REGEX_EXPERIMENT
                                        .matcher(nextLabelString);

                                if (!matcher.matches()) {
                                    this.log.error(
                                            "Found experiment label that did not start with expected format: {}",
                                            nextLabel);
                                } else {
                                    this.log.debug(
                                            "Found experiment label with the expected format: '{}' original=<{}>",
                                            nextLabelString, nextLabel);

                                    final int nextProjectYear = Integer.parseInt(matcher.group(1));
                                    final int nextProjectNumber = Integer.parseInt(matcher.group(2));
                                    final int nextExperimentNumber = Integer.parseInt(matcher.group(3));

                                    nextLabelString = String.format(
                                            ExampleSpreadsheetConstants.TEMPLATE_EXPERIMENT, nextProjectYear,
                                            nextProjectNumber, nextExperimentNumber);

                                    this.log.debug("Reformatted experiment label to: '{}' original=<{}>",
                                            nextLabelString, nextLabel);

                                    ConcurrentMap<URI, URI> labelMap = new ConcurrentHashMap<>();
                                    final ConcurrentMap<URI, URI> putIfAbsent = experimentUriMap
                                            .putIfAbsent(nextLabelString, labelMap);
                                    if (putIfAbsent != null) {
                                        this.log.error(
                                                "Found duplicate experiment name, inconsistent results may follow: {} {} {}",
                                                artifactId, nextExperiment, nextLabel);
                                        // Overwrite our reference with the one that already
                                        // existed
                                        labelMap = putIfAbsent;
                                    }
                                    final URI existingProject = labelMap.putIfAbsent((URI) nextExperiment,
                                            projectUri);
                                    // Check for the case where project name maps to different
                                    // artifacts
                                    if (existingProject != null && !existingProject.equals(projectUri)) {
                                        this.log.error(
                                                "Found duplicate experiment name across different projects, inconsistent results may follow: {} {} {} {}",
                                                artifactId, existingProject, projectUri, nextLabel);
                                    }
                                }
                            }
                        }
                    }

                }
            }
        }
    }
}