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 com.entwinemedia.fn.data.Opt;
32  
33  import java.io.Serializable;
34  
35  import javax.annotation.ParametersAreNonnullByDefault;
36  import javax.annotation.concurrent.Immutable;
37  
38  /**
39   * Representation of a DublinCore conforming property value.
40   * <p>
41   * See <a
42   * href="http://dublincore.org/documents/dc-xml-guidelines/">http://dublincore.org/documents/dc-xml-guidelines/</a> for
43   * further details.
44   */
45  @Immutable
46  @ParametersAreNonnullByDefault
47  public final class DublinCoreValue implements Serializable {
48    private static final long serialVersionUID = 7660583858714438266L;
49  
50    private final String value;
51    private final String language;
52    private final Opt<EName> encodingScheme;
53  
54    /**
55     * Create a new Dublin Core value.
56     *
57     * @param value
58     *          the value
59     * @param language
60     *          the language (two letter ISO 639)
61     * @param encodingScheme
62     *          the encoding scheme used to encode the value
63     */
64    public DublinCoreValue(String value, String language, Opt<EName> encodingScheme) {
65      this.value = RequireUtil.notNull(value, "value");
66      this.language = RequireUtil.notNull(language, "language");
67      this.encodingScheme = RequireUtil.notNull(encodingScheme, "encodingScheme");
68    }
69  
70    /**
71     * Create a new Dublin Core value.
72     *
73     * @param value
74     *         the value
75     * @param language
76     *         the language (two letter ISO 639)
77     * @param encodingScheme
78     *         the encoding scheme used to encode the value
79     */
80    public static DublinCoreValue mk(String value, String language, Opt<EName> encodingScheme) {
81      return new DublinCoreValue(value, language, encodingScheme);
82    }
83  
84    /**
85     * Create a new Dublin Core value.
86     *
87     * @param value
88     *         the value
89     * @param language
90     *         the language (two letter ISO 639)
91     * @param encodingScheme
92     *         the encoding scheme used to encode the value
93     */
94    public static DublinCoreValue mk(String value, String language, EName encodingScheme) {
95      return new DublinCoreValue(value, language, Opt.some(encodingScheme));
96    }
97  
98    /**
99     * Creates a new Dublin Core value without an encoding scheme.
100    *
101    * @param value
102    *          the value
103    * @param language
104    *          the language (two letter ISO 639)
105    */
106   public static DublinCoreValue mk(String value, String language) {
107     return new DublinCoreValue(value, language, Opt.<EName>none());
108   }
109 
110   /**
111    * Create a new Dublin Core value with the language set to undefined and no particular encoding scheme.
112    *
113    * @param value
114    *          the value
115    * @see org.opencastproject.metadata.dublincore.DublinCore#LANGUAGE_UNDEFINED
116    */
117   public static DublinCoreValue mk(String value) {
118     return new DublinCoreValue(value, DublinCore.LANGUAGE_UNDEFINED, Opt.<EName>none());
119   }
120 
121   /**
122    * Return the value of the property.
123    */
124   public String getValue() {
125     return value;
126   }
127 
128   /**
129    * Return the language.
130    */
131   public String getLanguage() {
132     return language;
133   }
134 
135   /**
136    * Return the encoding scheme.
137    */
138   public Opt<EName> getEncodingScheme() {
139     return encodingScheme;
140   }
141 
142   public boolean hasEncodingScheme() {
143     return encodingScheme.isSome();
144   }
145 
146   @Override
147   public boolean equals(Object that) {
148     return (this == that) || (that instanceof DublinCoreValue && eqFields((DublinCoreValue) that));
149   }
150 
151   private boolean eqFields(DublinCoreValue that) {
152     return eq(value, that.value) && eq(language, that.language) && eq(encodingScheme, that.encodingScheme);
153   }
154 
155   @Override
156   public int hashCode() {
157     return EqualsUtil.hash(value, language, encodingScheme);
158   }
159 
160   @Override public String toString() {
161     return format("DublinCoreValue(%s,%s,%s)", value, language, encodingScheme);
162   }
163 }