Skip to contentMethod: MultilingualString()
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 cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
21:
22: import java.io.Serializable;
23: import java.util.Collections;
24: import java.util.HashMap;
25: import java.util.Map;
26: import java.util.Objects;
27:
28: /**
29: * Value object representing the same string in multiple languages.
30: * <p>
31: * This is not a general-purpose type, it should be used only in instances where multiple translations of the same
32: * string need to be treated as a single value. Its main purpose is to allow support for multilingual strings in
33: * referenced lists (e.g., {@link cz.cvut.kbss.ontodriver.Lists#loadReferencedList(ReferencedListDescriptor)}).
34: */
35: public final class MultilingualString implements Serializable {
36:
37: private final Map<String, String> value;
38:
39: public MultilingualString() {
40: this.value = new HashMap<>();
41: }
42:
43: public MultilingualString(Map<String, String> value) {
44: this.value = new HashMap<>(Objects.requireNonNull(value));
45: }
46:
47: /**
48: * Sets translation in the specified language.
49: *
50: * @param language Language tag value
51: * @param value Translation in the specified language
52: */
53: public void set(String language, String value) {
54: Objects.requireNonNull(value);
55: this.value.put(language, value);
56: }
57:
58: /**
59: * Gets translation in the specified language (if present).
60: *
61: * @param language Language tag
62: * @return Translation in the specified language, {@code null} if it is not available
63: */
64: public String get(String language) {
65: return value.get(language);
66: }
67:
68: /**
69: * Gets an unmodifiable view of the internal representation of this multilingual string.
70: *
71: * @return Unmodifiable map
72: */
73: public Map<String, String> getValue() {
74: return Collections.unmodifiableMap(value);
75: }
76:
77: @Override
78: public boolean equals(Object o) {
79: if (this == o) {
80: return true;
81: }
82: if (!(o instanceof MultilingualString)) {
83: return false;
84: }
85: MultilingualString that = (MultilingualString) o;
86: return value.equals(that.value);
87: }
88:
89: @Override
90: public int hashCode() {
91: return Objects.hash(value);
92: }
93:
94: @Override
95: public String toString() {
96: return value.toString();
97: }
98: }