001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.commons.rdf.simple;
019
020import java.util.UUID;
021
022import org.apache.commons.rdf.api.BlankNode;
023import org.apache.commons.rdf.api.BlankNodeOrIRI;
024import org.apache.commons.rdf.api.Dataset;
025import org.apache.commons.rdf.api.Graph;
026import org.apache.commons.rdf.api.IRI;
027import org.apache.commons.rdf.api.Literal;
028import org.apache.commons.rdf.api.Quad;
029import org.apache.commons.rdf.api.RDFTerm;
030import org.apache.commons.rdf.api.RDF;
031import org.apache.commons.rdf.api.Triple;
032
033/**
034 * Simple RDF implementation.
035 * <p>
036 * The {@link RDFTerm}, {@link Triple}, {@link Quad}, {@link Graph} and
037 * {@link Dataset} instances created by SimpleRDF are simple in-memory
038 * Implementations that are not thread-safe or efficient, but which may be
039 * useful for testing and prototyping purposes.
040 */
041public class SimpleRDF implements RDF {
042
043    /**
044     * Marker interface to say that this RDFTerm is part of the Simple
045     * implementation. Used by {@link GraphImpl} to avoid double remapping.
046     * <p>
047     * This method is package protected to avoid any third-party subclasses.
048     *
049     */
050    static interface SimpleRDFTerm extends RDFTerm {
051    }
052
053    /**
054     * Unique salt per instance, for {@link #createBlankNode(String)}
055     */
056    private final UUID SALT = UUID.randomUUID();
057
058    @Override
059    public BlankNode createBlankNode() {
060        return new BlankNodeImpl();
061    }
062
063    @Override
064    public BlankNode createBlankNode(final String name) {
065        return new BlankNodeImpl(SALT, name);
066    }
067
068    @Override
069    public Graph createGraph() {
070        // Creates a GraphImpl object using this object as the factory for
071        // delegating all object creation to
072        return new GraphImpl(this);
073    }
074
075    @Override
076    public Dataset createDataset() throws UnsupportedOperationException {
077        return new DatasetImpl(this);
078    }
079
080    @Override
081    public IRI createIRI(final String iri) {
082        final IRI result = new IRIImpl(iri);
083        // Reuse any IRI objects already created in Types
084        return Types.get(result).orElse(result);
085    }
086
087    @Override
088    public Literal createLiteral(final String literal) {
089        return new LiteralImpl(literal);
090    }
091
092    @Override
093    public Literal createLiteral(final String literal, final IRI dataType) {
094        return new LiteralImpl(literal, dataType);
095    }
096
097    @Override
098    public Literal createLiteral(final String literal, final String language) {
099        return new LiteralImpl(literal, language);
100    }
101
102    @Override
103    public Triple createTriple(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
104        return new TripleImpl(subject, predicate, object);
105    }
106
107    @Override
108    public Quad createQuad(final BlankNodeOrIRI graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object)
109            throws IllegalArgumentException {
110        return new QuadImpl(graphName, subject, predicate, object);
111    }
112}