View Javadoc
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.rdf4j.impl;
19  
20  import java.util.Objects;
21  import java.util.Optional;
22  import java.util.UUID;
23  
24  import org.apache.commons.rdf.api.BlankNodeOrIRI;
25  import org.apache.commons.rdf.api.Quad;
26  import org.apache.commons.rdf.api.RDFTerm;
27  import org.apache.commons.rdf.api.Triple;
28  import org.apache.commons.rdf.rdf4j.RDF4J;
29  import org.apache.commons.rdf.rdf4j.RDF4JQuad;
30  import org.eclipse.rdf4j.model.Statement;
31  
32  final class QuadImpl implements RDF4JQuad {
33      private transient int hashCode = 0;
34      private final UUID salt;
35      private final Statement statement;
36  
37      QuadImpl(final Statement statement, final UUID salt) {
38          this.statement = statement;
39          this.salt = salt;
40      }
41  
42      @Override
43      public Statement asStatement() {
44          return statement;
45      }
46  
47      @Override
48      public Triple asTriple() {
49          return new TripleImpl(statement, salt);
50      }
51  
52      @Override
53      public boolean equals(final Object obj) {
54          if (obj instanceof Quad) {
55              final Quad quad = (Quad) obj;
56              return getGraphName().equals(quad.getGraphName()) &&
57                      getSubject().equals(quad.getSubject()) &&
58                      getPredicate().equals(quad.getPredicate()) &&
59                      getObject().equals(quad.getObject());
60          }
61          return false;
62      }
63  
64      @Override
65      public Optional<BlankNodeOrIRI> getGraphName() {
66          if (statement.getContext() == null) {
67              return Optional.empty();
68          }
69          final BlankNodeOrIRI g = (BlankNodeOrIRI) RDF4J.asRDFTerm(statement.getContext(), salt);
70          return Optional.of(g);
71      }
72  
73      @Override
74      public RDFTerm getObject() {
75          return RDF4J.asRDFTerm(statement.getObject(), salt);
76      }
77  
78      @Override
79      public org.apache.commons.rdf.api.IRI getPredicate() {
80          return (org.apache.commons.rdf.api.IRI) RDF4J.asRDFTerm(statement.getPredicate(), null);
81      }
82  
83      @Override
84      public BlankNodeOrIRI getSubject() {
85          return (BlankNodeOrIRI) RDF4J.asRDFTerm(statement.getSubject(), salt);
86      }
87  
88      @Override
89      public int hashCode() {
90          if (hashCode != 0) {
91              return hashCode;
92          }
93          return hashCode = Objects.hash(getSubject(), getPredicate(), getObject(), getGraphName());
94      }
95  
96      @Override
97      public String toString() {
98          return statement.toString();
99      }
100 }