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.jsonldjava;
019
020import java.util.Optional;
021
022import org.apache.commons.rdf.api.QuadLike;
023import org.apache.commons.rdf.api.RDFTerm;
024
025import com.github.jsonldjava.core.RDFDataset.Quad;
026
027public interface JsonLdQuadLike<G extends RDFTerm> extends QuadLike<G>, JsonLdTripleLike {
028
029}
030
031class JsonLdQuadLikeImpl<S extends RDFTerm, P extends RDFTerm, O extends RDFTerm, G extends RDFTerm>
032        implements JsonLdQuadLike<G> {
033
034    // Note: We always pass the blankNodePrefix and don't rely on the internal
035    // blankNodePrefix in this static factory
036    private static JsonLdRDF rdfTermFactory = new JsonLdRDF();
037
038    private final Quad quad;
039    private final String blankNodePrefix;
040
041    JsonLdQuadLikeImpl(final Quad jsonldQuad, final String blankNodePrefix) {
042        this.quad = jsonldQuad;
043        this.blankNodePrefix = blankNodePrefix;
044    }
045
046    @SuppressWarnings("unchecked")
047    @Override
048    public Optional<G> getGraphName() {
049        final G g = (G) rdfTermFactory.asRDFTerm(quad.getGraph(), blankNodePrefix);
050        return Optional.ofNullable(g);
051    }
052
053    @SuppressWarnings("unchecked")
054    @Override
055    public S getSubject() {
056        return (S) rdfTermFactory.asRDFTerm(quad.getSubject(), blankNodePrefix);
057    }
058
059    @SuppressWarnings("unchecked")
060    @Override
061    public P getPredicate() {
062        return (P) rdfTermFactory.asRDFTerm(quad.getPredicate(), blankNodePrefix);
063    }
064
065    @SuppressWarnings("unchecked")
066    @Override
067    public O getObject() {
068        return (O) rdfTermFactory.asRDFTerm(quad.getObject(), blankNodePrefix);
069    }
070
071    @Override
072    public Quad asJsonLdQuad() {
073        return quad;
074    }
075
076    @Override
077    public String toString() {
078        return quad.toString();
079    }
080}