1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.classifications2;
20
21 import java.io.Serializable;
22 import java.util.Locale;
23 import java.util.Objects;
24
25 import com.fasterxml.jackson.annotation.JsonInclude;
26
27 import jakarta.persistence.Column;
28 import jakarta.persistence.Embeddable;
29 import jakarta.xml.bind.annotation.XmlAccessType;
30 import jakarta.xml.bind.annotation.XmlAccessorType;
31 import jakarta.xml.bind.annotation.XmlAttribute;
32 import jakarta.xml.bind.annotation.XmlRootElement;
33
34
35
36
37
38
39
40
41 @XmlRootElement(
42 name = "label")
43 @XmlAccessorType(XmlAccessType.FIELD)
44 @Embeddable
45 @JsonInclude(value = JsonInclude.Include.NON_EMPTY)
46 public class MCRLabel implements Cloneable, Serializable, Comparable<MCRLabel> {
47
48 private static final long serialVersionUID = -843799854929361194L;
49
50 @XmlAttribute(
51 namespace = "http://www.w3.org/XML/1998/namespace")
52 String lang;
53
54 @XmlAttribute
55 String text;
56
57 @XmlAttribute
58 String description;
59
60 public MCRLabel() {
61
62 }
63
64
65
66
67
68
69
70
71 public MCRLabel(String lang, String text, String description)
72 throws NullPointerException, IllegalArgumentException {
73 super();
74 setLang(lang);
75 setText(text);
76 setDescription(description);
77 }
78
79 @Column
80 public String getLang() {
81 return lang;
82 }
83
84
85
86
87
88
89 public void setLang(String lang) {
90 Objects.requireNonNull(lang, "'lang' of label may not be null.");
91 if (lang.trim().isEmpty()) {
92 throw new IllegalArgumentException("'lang' of label may not be empty.");
93 }
94 Locale locale = Locale.forLanguageTag(lang);
95 String languageTag = locale.toLanguageTag();
96 if ("und".equals(languageTag)) {
97 throw new IllegalArgumentException("'lang' of label is not valid language tag (RFC4646):" + lang);
98 }
99 this.lang = languageTag;
100 }
101
102 @Column(length = 4096)
103 public String getText() {
104 return text;
105 }
106
107
108
109
110
111
112 public void setText(String text) {
113 Objects.requireNonNull(text, "'text' of label('" + lang + "') may not be null.");
114 if (text.trim().isEmpty()) {
115 throw new IllegalArgumentException("'text' of label('" + lang + "') may not be empty.");
116 }
117 this.text = text;
118 }
119
120 @Column(length = 4096, nullable = true)
121 public String getDescription() {
122 if (description == null) {
123 return "";
124 }
125 return description;
126 }
127
128 public void setDescription(String description) {
129 this.description = description;
130 }
131
132 @Override
133 public MCRLabel clone() {
134 MCRLabel clone = null;
135 try {
136 clone = (MCRLabel) super.clone();
137 } catch (CloneNotSupportedException ce) {
138
139 }
140 return clone;
141 }
142
143 @Override
144 public String toString() {
145 return getLang() + '(' + getText() + ')';
146 }
147
148
149
150
151
152
153 @Override
154 public int hashCode() {
155 final int prime = 31;
156 int result = 1;
157 result = prime * result + getDescription().hashCode();
158 result = prime * result + (lang == null ? 0 : lang.hashCode());
159 result = prime * result + (text == null ? 0 : text.hashCode());
160 return result;
161 }
162
163
164
165
166
167
168 @Override
169 public boolean equals(Object obj) {
170 if (this == obj) {
171 return true;
172 }
173 if (obj == null) {
174 return false;
175 }
176 if (getClass() != obj.getClass()) {
177 return false;
178 }
179 final MCRLabel other = (MCRLabel) obj;
180 if (lang == null) {
181 if (other.lang != null) {
182 return false;
183 }
184 } else if (!lang.equals(other.lang)) {
185 return false;
186 }
187 if (text == null) {
188 if (other.text != null) {
189 return false;
190 }
191 } else if (!text.equals(other.text)) {
192 return false;
193 }
194 return getDescription().equals(other.getDescription());
195 }
196
197 @Override
198 public int compareTo(MCRLabel other) {
199 if (other == null) {
200 return 1;
201 }
202
203 if (this.getLang() == other.getLang()) {
204 return 0;
205 }
206 if (this.getLang() == null) {
207 return -1;
208 }
209 if (other.getLang() == null) {
210 return 1;
211 }
212 return this.getLang().compareTo(other.getLang());
213 }
214 }