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.rdf4j.impl;
019
020import java.util.UUID;
021
022import org.apache.commons.rdf.api.BlankNode;
023import org.apache.commons.rdf.rdf4j.RDF4JBlankNode;
024import org.apache.commons.rdf.rdf4j.RDF4JDataset;
025import org.apache.commons.rdf.rdf4j.RDF4JGraph;
026import org.apache.commons.rdf.rdf4j.RDF4JIRI;
027import org.apache.commons.rdf.rdf4j.RDF4JLiteral;
028import org.apache.commons.rdf.rdf4j.RDF4JQuad;
029import org.apache.commons.rdf.rdf4j.RDF4JTerm;
030import org.apache.commons.rdf.rdf4j.RDF4J;
031import org.apache.commons.rdf.rdf4j.RDF4JTriple;
032import org.eclipse.rdf4j.model.BNode;
033import org.eclipse.rdf4j.model.IRI;
034import org.eclipse.rdf4j.model.Literal;
035import org.eclipse.rdf4j.model.Model;
036import org.eclipse.rdf4j.model.Resource;
037import org.eclipse.rdf4j.model.Statement;
038import org.eclipse.rdf4j.repository.Repository;
039
040/**
041 * Factory for {@link RDF4JTerm} instances.
042 * <p>
043 * <strong>Internal class:</strong> This "abstract" class is intended for
044 * internal use by Commons RDF and may change in any minor update. Use instead
045 * {@link RDF4J} methods like {@link RDF4J#createBlankNode()},
046 * {@link RDF4J#asRDFTerm(org.eclipse.rdf4j.model.Value)} and
047 * {@link RDF4J#asGraph(Repository, Option...)}
048 * <p>
049 * This class exists as a <code>public</code> bridge between the packages
050 * {@link org.apache.commons.rdf.rdf4j} and
051 * {@link org.apache.commons.rdf.rdf4j.impl} by exposing the package-public
052 * constructors.
053 *
054 * @see RDF4J
055 */
056public abstract class InternalRDF4JFactory {
057
058    /**
059     * Construct a {@link RDF4JBlankNode} from a RDF4J {@link BNode}.
060     *
061     * @param bNode
062     *            RDF4J {@link BNode} to adapt
063     * @param salt
064     *            {@link UUID} to use for {@link BlankNode#uniqueReference()} in
065     *            combination with {@link BNode#getID()}
066     * @return Adapted {@link RDF4JBlankNode}
067     */
068    public RDF4JBlankNode createBlankNodeImpl(final BNode bNode, final UUID salt) {
069        return new BlankNodeImpl(bNode, salt);
070    }
071
072    /**
073     * Construct a {@link RDF4JIRI} from a RDF4J {@link IRI}.
074     *
075     * @param iri
076     *            RDF4J {@link IRI} to adapt
077     * @return Adapted {@link RDF4JIRI}
078     */
079    public RDF4JIRI createIRIImpl(final IRI iri) {
080        return new IRIImpl(iri);
081    }
082
083    /**
084     * Construct a {@link RDF4JLiteral} from a RDF4J {@link Literal}.
085     *
086     * @param literal
087     *            RDF4J {@link Literal}
088     * @return Adapted {@link RDF4JLiteral}
089     */
090    public RDF4JLiteral createLiteralImpl(final Literal literal) {
091        return new LiteralImpl(literal);
092    }
093
094    /**
095     * Construct a {@link RDF4JGraph} from a RDF4J {@link Model}.
096     * <p>
097     * Changes in the graph will be reflected in the model, and vice versa.
098     *
099     * @param model
100     *            RDF4J {@link Model} to adapt
101     * @param rdf4jTermFactory
102     *            factory to use for adapting graph triples
103     * @return Adapted {@link RDF4JGraph}
104     */
105    public RDF4JGraph createModelGraphImpl(final Model model, final RDF4J rdf4jTermFactory) {
106        return new ModelGraphImpl(model, rdf4jTermFactory);
107    }
108
109    /**
110     * Construct a {@link RDF4JQuad} from a RDF4J {@link Statement}.
111     *
112     * @param statement
113     *            RDF4J {@link Statement} to adapt
114     * @param salt
115     *            {@link UUID} for adapting any {@link BNode}s
116     * @return Adapted {@link RDF4JQuad}
117     */
118    public RDF4JQuad createQuadImpl(final Statement statement, final UUID salt) {
119        return new QuadImpl(statement, salt);
120    }
121
122    /**
123     * Construct a {@link RDF4JDataset} from a RDF4J {@link Repository}.
124     * <p>
125     * Changes in the dataset will be reflected in the repsitory, and vice
126     * versa.
127     *
128     * @param repository
129     *            RDF4J {@link Repository} to adapt
130     * @param handleInitAndShutdown
131     *            If <code>true</code>, the {@link RDF4JDataset} will initialize
132     *            the repository (if needed), and shut it down on
133     *            {@link RDF4JDataset#close()}.
134     * @param includeInferred
135     *            If true, any inferred quads are included in the dataset
136     *
137     * @return Adapted {@link RDF4JDataset}
138     */
139    public RDF4JDataset createRepositoryDatasetImpl(final Repository repository, final boolean handleInitAndShutdown,
140            final boolean includeInferred) {
141        return new RepositoryDatasetImpl(repository, UUID.randomUUID(), handleInitAndShutdown, includeInferred);
142    }
143
144    /**
145     * Construct a {@link RDF4JGraph} from a RDF4J {@link Model}.
146     * <p>
147     * Changes in the graph will be reflected in the model, and vice versa.
148     *
149     * @param repository
150     *            RDF4J {@link Repository} to adapt
151     * @param handleInitAndShutdown
152     *            If <code>true</code>, the {@link RDF4JGraph} will initialize
153     *            the repository (if needed), and shut it down on
154     *            {@link RDF4JGraph#close()}.
155     * @param includeInferred
156     *            If true, any inferred quads are included in the dataset
157     * @param contextMask
158     *            Zero or more {@link Resource}s contexts. The array may contain
159     *            the value <code>null</code> for the default graph - however
160     *            care must be taken to not provide a null-array
161     *            <code>(Resource[]) null</code>.
162     * @return Adapted {@link RDF4JGraph}
163     */
164    public RDF4JGraph createRepositoryGraphImpl(final Repository repository, final boolean handleInitAndShutdown,
165            final boolean includeInferred, final Resource... contextMask) {
166        return new RepositoryGraphImpl(repository, UUID.randomUUID(), handleInitAndShutdown, includeInferred,
167                contextMask);
168    }
169
170    /**
171     * Construct a {@link RDF4JTriple} from a RDF4J {@link Statement}.
172     *
173     * @param statement
174     *            RDF4J {@link Statement} to adapt
175     * @param salt
176     *            {@link UUID} for adapting any {@link BNode}s
177     * @return Adapted {@link RDF4JTriple}
178     */
179    public RDF4JTriple createTripleImpl(final Statement statement, final UUID salt) {
180        return new TripleImpl(statement, salt);
181    }
182
183}