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.Locale;
21  import java.util.Objects;
22  import java.util.Optional;
23  
24  import org.apache.commons.rdf.rdf4j.RDF4JLiteral;
25  import org.eclipse.rdf4j.model.vocabulary.XMLSchema;
26  import org.eclipse.rdf4j.rio.turtle.TurtleUtil;
27  
28  final class LiteralImpl extends AbstractRDFTerm<org.eclipse.rdf4j.model.Literal> implements RDF4JLiteral {
29  
30      private static final String QUOTE = "\"";
31  
32      LiteralImpl(final org.eclipse.rdf4j.model.Literal literal) {
33          super(literal);
34      }
35  
36      private static String lowerCase(final String langTag) {
37          return langTag.toLowerCase(Locale.ROOT);
38      }
39  
40      @Override
41      public boolean equals(final Object obj) {
42          if (obj == this) {
43              return true;
44          }
45          if (obj instanceof org.apache.commons.rdf.api.Literal) {
46              final org.apache.commons.rdf.api.Literal other = (org.apache.commons.rdf.api.Literal) obj;
47              return getLexicalForm().equals(other.getLexicalForm()) &&
48                      getDatatype().equals(other.getDatatype()) &&
49                      getLanguageTag().map(LiteralImpl::lowerCase).equals(
50                              other.getLanguageTag().map(LiteralImpl::lowerCase));
51          }
52          return false;
53      }
54  
55      @Override
56      public org.apache.commons.rdf.api.IRI getDatatype() {
57          return new IRIImpl(value.getDatatype());
58      }
59  
60      @Override
61      public Optional<String> getLanguageTag() {
62          return value.getLanguage();
63      }
64  
65      @Override
66      public String getLexicalForm() {
67          return value.getLabel();
68      }
69  
70      @Override
71      public int hashCode() {
72          return Objects.hash(value.getLabel(), value.getDatatype(),
73                  getLanguageTag().map(LiteralImpl::lowerCase));
74      }
75  
76      @Override
77      public String ntriplesString() {
78          // TODO: Use a more efficient StringBuffer
79          final String escaped = QUOTE + TurtleUtil.encodeString(value.getLabel()) + QUOTE;
80          if (value.getLanguage().isPresent()) {
81              return escaped + "@" + value.getLanguage().get();
82          }
83          if (value.getDatatype().equals(XMLSchema.STRING)) {
84              return escaped;
85          }
86          return escaped + "^^<" + TurtleUtil.encodeURIString(value.getDatatype().toString()) + ">";
87      }
88  
89      @Override
90      public String toString() {
91          return ntriplesString();
92      }
93  }