Skip to contentMethod: getField()
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jsonld.serialization.traversal;
14:
15: import cz.cvut.kbss.jsonld.serialization.context.JsonLdContext;
16: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
17: import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
18:
19: import java.lang.reflect.Field;
20: import java.util.Objects;
21: import java.util.Optional;
22:
23: /**
24: * Represents the current serialization context.
25: * <p>
26: * This means the value being serialized, and optionally the target JSON-LD term identifier and field.
27: * <p>
28: * Note that term and field may not always be available, e.g., when a collection element is being serialized, neither is
29: * set.
30: * <p>
31: * This class also provides access to the JSON-LD context (if it is used). However, note that for selected serialization
32: * methods the context may be just a stub with no bearing on the serialization output.
33: *
34: * @param <T> Type of the value
35: */
36: public class SerializationContext<T> implements JsonLdContext {
37:
38: private String term;
39:
40: private final Field field;
41:
42: private final T value;
43:
44: private JsonLdContext jsonLdContext;
45:
46: public SerializationContext(String term, T value, JsonLdContext jsonLdContext) {
47: this(term, null, value, jsonLdContext);
48: }
49:
50: public SerializationContext(Field field, T value, JsonLdContext jsonLdContext) {
51: this(null, field, value, jsonLdContext);
52: }
53:
54: public SerializationContext(T value, JsonLdContext jsonLdContext) {
55: this(null, null, value, jsonLdContext);
56: }
57:
58: public SerializationContext(String term, Field field, T value, JsonLdContext jsonLdContext) {
59: this.term = term;
60: this.field = field;
61: this.value = value;
62: this.jsonLdContext = jsonLdContext;
63: }
64:
65: public String getTerm() {
66: return term;
67: }
68:
69: public Field getField() {
70: return field;
71: }
72:
73: public String getFieldName() {
74: return field != null ? field.getName() : null;
75: }
76:
77: public T getValue() {
78: return value;
79: }
80:
81: public JsonLdContext getJsonLdContext() {
82: return jsonLdContext;
83: }
84:
85: public void setJsonLdContext(JsonLdContext jsonLdContext) {
86: this.jsonLdContext = jsonLdContext;
87: }
88:
89: @Override
90: public void registerTermMapping(String term, String iri) {
91: jsonLdContext.registerTermMapping(term, iri);
92: this.term = term;
93: }
94:
95: @Override
96: public void registerTermMapping(String term, ObjectNode mappedNode) {
97: jsonLdContext.registerTermMapping(term, mappedNode);
98: this.term = term;
99: }
100:
101: @Override
102: public Optional<JsonNode> getTermMapping(String term) {
103: return jsonLdContext.getTermMapping(term);
104: }
105:
106: @Override
107: public boolean hasTermMapping(String term) {
108: return jsonLdContext.hasTermMapping(term);
109: }
110:
111: @Override
112: public Optional<String> getMappedTerm(String iri) {
113: return jsonLdContext.getMappedTerm(iri);
114: }
115:
116: @Override
117: public boolean isCurrentEmpty() {
118: return jsonLdContext.isCurrentEmpty();
119: }
120:
121: @Override
122: public ObjectNode getContextNode() {
123: return jsonLdContext.getContextNode();
124: }
125:
126: @Override
127: public boolean equals(Object o) {
128: if (this == o) return true;
129: if (o == null || getClass() != o.getClass()) return false;
130: SerializationContext<?> that = (SerializationContext<?>) o;
131: return Objects.equals(getTerm(), that.getTerm()) && Objects
132: .equals(getField(), that.getField()) && Objects.equals(getValue(), that.getValue());
133: }
134:
135: @Override
136: public int hashCode() {
137: return Objects.hash(getTerm(), getField(), getValue());
138: }
139:
140: }