Debian Codegraph @

commons-rdf-simple/src/main/java/org/apache/commons/rdf/simple/QuadImpl.java

raw
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.commons.rdf.simple;
19
20 import org.apache.commons.rdf.api.BlankNodeOrIRI;
21 import org.apache.commons.rdf.api.IRI;
22 import org.apache.commons.rdf.api.Quad;
23 import org.apache.commons.rdf.api.RDFTerm;
24 import org.apache.commons.rdf.api.Triple;
25
26 import java.util.Objects;
27 import java.util.Optional;
28
29 /**
30 * A simple implementation of Quad.
31 */
32 final class QuadImpl implements Quad {
33
34 private final BlankNodeOrIRI graphName;
35 private final BlankNodeOrIRI subject;
36 private final IRI predicate;
37 private final RDFTerm object;
38
39 /**
40 * Construct Quad from its constituent parts.
41 * <p>
42 * The objects are not changed. All mapping of BNode objects is done in
43 * {@link SimpleRDF#createQuad(BlankNodeOrIRI, BlankNodeOrIRI, IRI, RDFTerm)}.
44 *
45 * @param graphName
46 * graphName of triple
47 * @param subject
48 * subject of triple
49 * @param predicate
50 * predicate of triple
51 * @param object
52 * object of triple
53 */
54 public QuadImpl(final BlankNodeOrIRI graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
55 this.graphName = graphName; // possibly null
56 this.subject = Objects.requireNonNull(subject);
57 this.predicate = Objects.requireNonNull(predicate);
58 this.object = Objects.requireNonNull(object);
59 }
60
61 @Override
62 public Optional<BlankNodeOrIRI> getGraphName() {
63 return Optional.ofNullable(graphName);
64 }
65
66 @Override
67 public BlankNodeOrIRI getSubject() {
68 return subject;
69 }
70
71 @Override
72 public IRI getPredicate() {
73 return predicate;
74 }
75
76 @Override
77 public RDFTerm getObject() {
78 return object;
79 }
80
81 @Override
82 public String toString() {
83 return getSubject().ntriplesString() + " " + getPredicate().ntriplesString() + " "
84 + getObject().ntriplesString() + " " + getGraphName().map(g -> g.ntriplesString() + " ").orElse("")
85 + ".";
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(subject, predicate, object, graphName);
91 }
92
93 @Override
94 public boolean equals(final Object obj) {
95 if (!(obj instanceof Quad)) {
96 return false;
97 }
98 final Quad other = (Quad) obj;
99 return getGraphName().equals(other.getGraphName()) && getSubject().equals(other.getSubject())
100 && getPredicate().equals(other.getPredicate()) && getObject().equals(other.getObject());
101 }
102
103 @Override
104 public Triple asTriple() {
105 return new TripleImpl(getSubject(), getPredicate(), getObject());
106 }
107
108 }