View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.metadata.dublincore;
23  
24  import static java.lang.String.format;
25  import static org.opencastproject.util.EqualsUtil.eq;
26  
27  import org.opencastproject.mediapackage.EName;
28  import org.opencastproject.util.EqualsUtil;
29  import org.opencastproject.util.RequireUtil;
30  
31  import java.io.Serializable;
32  import java.util.Optional;
33  
34  import javax.annotation.ParametersAreNonnullByDefault;
35  import javax.annotation.concurrent.Immutable;
36  
37  /**
38   * Representation of a DublinCore conforming property value.
39   * <p>
40   * See <a
41   * href="http://dublincore.org/documents/dc-xml-guidelines/">http://dublincore.org/documents/dc-xml-guidelines/</a> for
42   * further details.
43   */
44  @Immutable
45  @ParametersAreNonnullByDefault
46  public final class DublinCoreValue implements Serializable {
47    private static final long serialVersionUID = 7660583858714438266L;
48  
49    private final String value;
50    private final String language;
51    private final Optional<EName> encodingScheme;
52  
53    /**
54     * Create a new Dublin Core value.
55     *
56     * @param value
57     *          the value
58     * @param language
59     *          the language (two letter ISO 639)
60     * @param encodingScheme
61     *          the encoding scheme used to encode the value
62     */
63    public DublinCoreValue(String value, String language, Optional<EName> encodingScheme) {
64      this.value = RequireUtil.notNull(value, "value");
65      this.language = RequireUtil.notNull(language, "language");
66      this.encodingScheme = RequireUtil.notNull(encodingScheme, "encodingScheme");
67    }
68  
69    /**
70     * Create a new Dublin Core value.
71     *
72     * @param value
73     *         the value
74     * @param language
75     *         the language (two letter ISO 639)
76     * @param encodingScheme
77     *         the encoding scheme used to encode the value
78     */
79    public static DublinCoreValue mk(String value, String language, Optional<EName> encodingScheme) {
80      return new DublinCoreValue(value, language, encodingScheme);
81    }
82  
83    /**
84     * Create a new Dublin Core value.
85     *
86     * @param value
87     *         the value
88     * @param language
89     *         the language (two letter ISO 639)
90     * @param encodingScheme
91     *         the encoding scheme used to encode the value
92     */
93    public static DublinCoreValue mk(String value, String language, EName encodingScheme) {
94      return new DublinCoreValue(value, language, Optional.of(encodingScheme));
95    }
96  
97    /**
98     * Creates a new Dublin Core value without an encoding scheme.
99     *
100    * @param value
101    *          the value
102    * @param language
103    *          the language (two letter ISO 639)
104    */
105   public static DublinCoreValue mk(String value, String language) {
106     return new DublinCoreValue(value, language, Optional.empty());
107   }
108 
109   /**
110    * Create a new Dublin Core value with the language set to undefined and no particular encoding scheme.
111    *
112    * @param value
113    *          the value
114    * @see org.opencastproject.metadata.dublincore.DublinCore#LANGUAGE_UNDEFINED
115    */
116   public static DublinCoreValue mk(String value) {
117     return new DublinCoreValue(value, DublinCore.LANGUAGE_UNDEFINED, Optional.empty());
118   }
119 
120   /**
121    * Return the value of the property.
122    */
123   public String getValue() {
124     return value;
125   }
126 
127   /**
128    * Return the language.
129    */
130   public String getLanguage() {
131     return language;
132   }
133 
134   /**
135    * Return the encoding scheme.
136    */
137   public Optional<EName> getEncodingScheme() {
138     return encodingScheme;
139   }
140 
141   public boolean hasEncodingScheme() {
142     return encodingScheme.isPresent();
143   }
144 
145   @Override
146   public boolean equals(Object that) {
147     return (this == that) || (that instanceof DublinCoreValue && eqFields((DublinCoreValue) that));
148   }
149 
150   private boolean eqFields(DublinCoreValue that) {
151     return eq(value, that.value) && eq(language, that.language) && eq(encodingScheme, that.encodingScheme);
152   }
153 
154   @Override
155   public int hashCode() {
156     return EqualsUtil.hash(value, language, encodingScheme);
157   }
158 
159   @Override public String toString() {
160     return format("DublinCoreValue(%s,%s,%s)", value, language, encodingScheme);
161   }
162 }