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  
23  package org.opencastproject.elasticsearch.impl;
24  
25  import org.opencastproject.elasticsearch.api.Language;
26  import org.opencastproject.elasticsearch.api.SearchMetadata;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Default implementation for the {@link SearchMetadata}.
36   */
37  public class SearchMetadataImpl<T> implements SearchMetadata<T> {
38  
39    /** The name of this metadata item */
40    protected String name = null;
41  
42    /** Values */
43    protected List<T> values = new ArrayList<T>();
44  
45    /** Localized values */
46    protected Map<Language, List<T>> localizedValues = new HashMap<Language, List<T>>();
47  
48    /** True to add the values to the fulltext index */
49    protected boolean addToText = false;
50  
51    /**
52     * Creates a new metadata item with the given name.
53     *
54     * @param name
55     *          the name
56     */
57    public SearchMetadataImpl(String name) {
58      this.name = name;
59    }
60  
61    /**
62     * {@inheritDoc}
63     * 
64     * @see SearchMetadata#getName()
65     */
66    public String getName() {
67      return name;
68    }
69  
70    /**
71     * {@inheritDoc}
72     * 
73     * @see SearchMetadata#isLocalized()
74     */
75    public boolean isLocalized() {
76      return localizedValues != null && localizedValues.size() > 0;
77    }
78  
79    /**
80     * Adds <code>value</code> to the list of language neutral values.
81     *
82     * @param language
83     *          the language
84     * @param v
85     *          the value
86     */
87    public void addLocalizedValue(Language language, T v) {
88      if (localizedValues == null) {
89        localizedValues = new HashMap<Language, List<T>>();
90      }
91      List<T> values = localizedValues.get(language);
92      if (values == null) {
93        values = new ArrayList<T>();
94      }
95      if (!values.contains(v)) {
96        values.add(v);
97      }
98      localizedValues.put(language, values);
99    }
100 
101   /**
102    * {@inheritDoc}
103    * 
104    * @see SearchMetadata#getLocalizedValues()
105    */
106   public Map<Language, List<T>> getLocalizedValues() {
107     if (localizedValues == null) {
108       return Collections.emptyMap();
109     }
110     return localizedValues;
111   }
112 
113   /**
114    * {@inheritDoc}
115    * 
116    * @see SearchMetadata#addValue(java.lang.Object)
117    */
118   public void addValue(T v) {
119     if (values == null) {
120       values = new ArrayList<T>();
121     }
122     if (!values.contains(v)) {
123       values.add(v);
124     }
125   }
126 
127   /**
128    * {@inheritDoc}
129    * 
130    * @see SearchMetadata#getValues()
131    */
132   public List<T> getValues() {
133     if (values == null) {
134       return Collections.emptyList();
135     }
136     return values;
137   }
138 
139   /**
140    * {@inheritDoc}
141    * 
142    * @see SearchMetadata#getValue()
143    */
144   @Override
145   public T getValue() {
146     if (values == null || values.size() == 0) {
147       return null;
148     }
149     return values.get(0);
150   }
151 
152   /**
153    * {@inheritDoc}
154    * 
155    * @see SearchMetadata#setAddToText(boolean)
156    */
157   public void setAddToText(boolean addToText) {
158     this.addToText = addToText;
159   }
160 
161   /**
162    * {@inheritDoc}
163    */
164   public boolean addToText() {
165     return addToText;
166   }
167 
168   /**
169    * {@inheritDoc}
170    * 
171    * @see SearchMetadata#clear()
172    */
173   public void clear() {
174     if (values != null) {
175       values.clear();
176     }
177     if (localizedValues != null) {
178       localizedValues.clear();
179     }
180   }
181 
182   /**
183    * {@inheritDoc}
184    *
185    * @see java.lang.Object#hashCode()
186    */
187   @Override
188   public int hashCode() {
189     return name.hashCode();
190   }
191 
192   /**
193    * {@inheritDoc}
194    *
195    * @see java.lang.Object#equals(java.lang.Object)
196    */
197   @Override
198   public boolean equals(Object obj) {
199     if (!(obj instanceof SearchMetadata<?>)) {
200       return false;
201     }
202     return name.equals(((SearchMetadata<?>) obj).getName());
203   }
204 
205   /**
206    * {@inheritDoc}
207    *
208    * @see java.lang.Object#toString()
209    */
210   @Override
211   public String toString() {
212     return name;
213   }
214 
215 }