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.UUID;
22  
23  import org.apache.commons.rdf.api.BlankNodeOrIRI;
24  import org.apache.commons.rdf.api.RDFTerm;
25  import org.apache.commons.rdf.api.Triple;
26  import org.apache.commons.rdf.rdf4j.RDF4J;
27  import org.apache.commons.rdf.rdf4j.RDF4JTriple;
28  import org.eclipse.rdf4j.model.Statement;
29  
30  final class TripleImpl implements RDF4JTriple {
31      private final UUID salt;
32      private final Statement statement;
33  
34      TripleImpl(final Statement statement, final UUID salt) {
35          this.statement = statement;
36          this.salt = salt;
37      }
38  
39      @Override
40      public Statement asStatement() {
41          return statement;
42      }
43  
44      @Override
45      public boolean equals(final Object obj) {
46          if (obj instanceof Triple) {
47              final Triple triple = (Triple) obj;
48              return getSubject().equals(triple.getSubject()) && getPredicate().equals(triple.getPredicate())
49                      && getObject().equals(triple.getObject());
50          }
51          return false;
52      }
53  
54      @Override
55      public RDFTerm getObject() {
56          return RDF4J.asRDFTerm(statement.getObject(), salt);
57      }
58  
59      @Override
60      public org.apache.commons.rdf.api.IRI getPredicate() {
61          return (org.apache.commons.rdf.api.IRI) RDF4J.asRDFTerm(statement.getPredicate(), null);
62      }
63  
64      @Override
65      public BlankNodeOrIRI getSubject() {
66          return (BlankNodeOrIRI) RDF4J.asRDFTerm(statement.getSubject(), salt);
67      }
68  
69      @Override
70      public int hashCode() {
71          return Objects.hash(getSubject(), getPredicate(), getObject());
72      }
73  
74      @Override
75      public String toString() {
76          return statement.toString();
77      }
78  }