View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe 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
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * This class represents a label of a MCRCategory.
36   * 
37   * @author Thomas Scheffler (yagee)
38   * @version $Revision$ $Date$
39   * @since 2.0
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       * @param lang see {@link #setLang(String)}
66       * @param text see {@link #setText(String)}
67       * @param description see {@link #setDescription(String)}
68       * @throws NullPointerException if lang or text is null
69       * @throws IllegalArgumentException if lang or text is invalid
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       * @param lang language tag in RFC4646 form
86       * @throws NullPointerException if lang is null
87       * @throws IllegalArgumentException if lang is somehow invalid (empty or 'und')
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      * @param text required attribute of label
109      * @throws NullPointerException if text is null
110      * @throws IllegalArgumentException if text is empty
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             // Can not happen
139         }
140         return clone;
141     }
142 
143     @Override
144     public String toString() {
145         return getLang() + '(' + getText() + ')';
146     }
147 
148     /*
149      * (non-Javadoc)
150      * 
151      * @see java.lang.Object#hashCode()
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      * (non-Javadoc)
165      * 
166      * @see java.lang.Object#equals(java.lang.Object)
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             //both are not null
203             if (this.getLang() == other.getLang()) { // this intentionally uses == to allow for both null
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 }