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.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.RDFTerm;
23  import org.apache.commons.rdf.api.Triple;
24  
25  import java.util.Objects;
26  
27  /**
28   * A simple implementation of Triple.
29   */
30  final class TripleImpl implements Triple {
31  
32      private final BlankNodeOrIRI subject;
33      private final IRI predicate;
34      private final RDFTerm object;
35  
36      /**
37       * Construct Triple from its constituent parts.
38       * <p>
39       * The objects are not changed. All mapping of BNode objects is done in
40       * {@link SimpleRDF#createTriple(BlankNodeOrIRI, IRI, RDFTerm)}.
41       *
42       * @param subject
43       *            subject of triple
44       * @param predicate
45       *            predicate of triple
46       * @param object
47       *            object of triple
48       */
49      public TripleImpl(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
50          this.subject = Objects.requireNonNull(subject);
51          this.predicate = Objects.requireNonNull(predicate);
52          this.object = Objects.requireNonNull(object);
53      }
54  
55      @Override
56      public BlankNodeOrIRI getSubject() {
57          return subject;
58      }
59  
60      @Override
61      public IRI getPredicate() {
62          return predicate;
63      }
64  
65      @Override
66      public RDFTerm getObject() {
67          return object;
68      }
69  
70      @Override
71      public String toString() {
72          return getSubject().ntriplesString() + " " + getPredicate().ntriplesString() + " "
73                  + getObject().ntriplesString() + " .";
74      }
75  
76      @Override
77      public int hashCode() {
78          return Objects.hash(subject, predicate, object);
79      }
80  
81      @Override
82      public boolean equals(final Object obj) {
83          if (!(obj instanceof Triple)) {
84              return false;
85          }
86          final Triple other = (Triple) obj;
87          return getSubject().equals(other.getSubject()) && getPredicate().equals(other.getPredicate())
88                  && getObject().equals(other.getObject());
89      }
90  
91  }