Skip to content

Method: toString()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.model;
19:
20: import java.io.Serializable;
21: import java.util.Objects;
22:
23: /**
24: * Represents an RDF literal consisting of a lexical form and datatype IRI.
25: * <p>
26: * To represent language-tagged string values, use {@link LangString}.
27: */
28: public class Literal implements Serializable {
29:
30: private final String lexicalForm;
31:
32: private final String datatype;
33:
34: public Literal(String lexicalForm, String datatype) {
35: this.lexicalForm = Objects.requireNonNull(lexicalForm);
36: this.datatype = Objects.requireNonNull(datatype);
37: }
38:
39: /**
40: * Gets the lexical form of this literal.
41: *
42: * @return String containing the lexical form
43: */
44: public String getLexicalForm() {
45: return lexicalForm;
46: }
47:
48: /**
49: * Gets the datatype identifier.
50: *
51: * @return String containing the datatype IRI
52: */
53: public String getDatatype() {
54: return datatype;
55: }
56:
57: @Override
58: public boolean equals(Object o) {
59: if (this == o) {
60: return true;
61: }
62: if (!(o instanceof Literal)) {
63: return false;
64: }
65: Literal literal = (Literal) o;
66: return lexicalForm.equals(literal.lexicalForm) && datatype.equals(literal.datatype);
67: }
68:
69: @Override
70: public int hashCode() {
71: return Objects.hash(lexicalForm, datatype);
72: }
73:
74: @Override
75: public String toString() {
76: return "\"" + lexicalForm + "\"^^<" + datatype + ">";
77: }
78:
79: /**
80: * Creates a literal instance from the specified lexical form and datatype.
81: * <p>
82: * Convenience factory method alternative to constructor.
83: *
84: * @param lexicalForm Lexical form of the literal
85: * @param datatype Datatype of the literal
86: * @return Literal instance
87: */
88: public static Literal from(String lexicalForm, String datatype) {
89: return new Literal(lexicalForm, datatype);
90: }
91: }