All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.hasmac.rdf.impl.RdfDatasetImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 APICATALOG and HASMAC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SPDX-License-Identifier: Apache-2.0
 */

package no.hasmac.rdf.impl;

import no.hasmac.rdf.RdfDataset;
import no.hasmac.rdf.RdfGraph;
import no.hasmac.rdf.RdfNQuad;
import no.hasmac.rdf.RdfResource;
import no.hasmac.rdf.RdfTriple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

final class RdfDatasetImpl implements RdfDataset {

    private final Map graphs;

    private final List nquads;

    private final RdfGraphImpl defaultGraph;

    protected RdfDatasetImpl() {
        this.graphs = new HashMap<>(1);
        this.nquads = new ArrayList<>();
        this.defaultGraph = new RdfGraphImpl();
    }

    @Override
    public RdfGraph getDefaultGraph() {
        return defaultGraph;
    }

    @Override
    public List toList() {
        return nquads;
    }

    public RdfDataset add(final RdfNQuad nquad) {

        if (nquad == null) {
            throw new IllegalArgumentException();
        }

        final Optional graphName = nquad.getGraphName();

        if (graphName.isPresent()) {

            RdfGraphImpl graph = graphs.get(graphName.get());

            if (graph == null) {

                graph = new RdfGraphImpl();
                graphs.put(graphName.get(), graph);
                graph.add(nquad);
                nquads.add(nquad);

            } else if (!graph.contains(nquad)) {

                graph.add(nquad);
                nquads.add(nquad);
            }

        } else {

            // add to default graph
            if (!defaultGraph.contains(nquad)) {
                defaultGraph.add(nquad);
                nquads.add(nquad);
            }
        }
        return this;
    }

    @Override
    public Set getGraphNames() {
        return graphs.keySet();
    }

    @Override
    public Optional getGraph(final RdfResource graphName) {
        return Optional.ofNullable(graphs.get(graphName));
    }

    @Override
    public int size() {
        return nquads.size();
    }

    @Override
    public RdfDataset add(RdfTriple triple) {

        RdfNQuad nquad = new RdfNQuadImpl(triple.getSubject(), triple.getPredicate(), triple.getObject(), null);

        if (!defaultGraph.contains(nquad)) {
            defaultGraph.add(nquad);
            nquads.add(nquad);
        }

        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy