Skip to content

Method: set(String, String)

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