View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.spi.core.value;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.Map;
25  
26  import de.smartics.properties.api.core.domain.PropertyValueRange;
27  import de.smartics.util.lang.Arguments;
28  import de.smartics.util.lang.NullArgumentException;
29  
30  /**
31   * Defines a set of valid enumerable elements for values of a property.
32   *
33   * @param <T> the type of the property values within the given range. Please
34   *          note that the type is required to be serializable. Otherwise
35   *          instances of this class are also not serializable.
36   */
37  public final class CollectionPropertyValueRange<T> implements
38      PropertyValueRange<T>, Serializable
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    /**
45     * The class version identifier.
46     */
47    private static final long serialVersionUID = 1L;
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The values mapped by their string representation to their value instance.
53     *
54     * @serial
55     */
56    private final Map<String, T> valueRange;
57  
58    // ****************************** Initializer *******************************
59  
60    // ****************************** Constructors ******************************
61  
62    /**
63     * Constructor accepting an array of values.
64     *
65     * @param values the allowed values for this value range.
66     * @throws NullArgumentException if {@code values} is <code>null</code>.
67     */
68    public CollectionPropertyValueRange(final T[] values)
69      throws NullArgumentException
70    {
71      Arguments.checkNotNull("values", values);
72  
73      this.valueRange = createMap(values);
74    }
75  
76    /**
77     * Constructor accepting a collection of values.
78     *
79     * @param values the allowed values for this value range.
80     * @throws NullArgumentException if {@code values} is <code>null</code>.
81     */
82    public CollectionPropertyValueRange(final Collection<T> values)
83      throws NullArgumentException
84    {
85      Arguments.checkNotNull("values", values);
86  
87      this.valueRange = createMap(values);
88    }
89  
90    // ****************************** Inner Classes *****************************
91  
92    // ********************************* Methods ********************************
93  
94    // --- init -----------------------------------------------------------------
95  
96    private static <T> Map<String, T> createMap(final T[] values)
97    {
98      final Map<String, T> map = new LinkedHashMap<String, T>(values.length);
99      for (T value : values)
100     {
101       map.put(String.valueOf(value), value);
102     }
103     return map;
104   }
105 
106   private static <T> Map<String, T> createMap(final Collection<T> values)
107   {
108     final Map<String, T> map = new LinkedHashMap<String, T>(values.size());
109     for (T value : values)
110     {
111       map.put(String.valueOf(value), value);
112     }
113     return map;
114   }
115 
116   // --- get&set --------------------------------------------------------------
117 
118   // --- business -------------------------------------------------------------
119 
120   @Override
121   public List<T> getValues()
122   {
123     final List<T> list = new ArrayList<T>(valueRange.values());
124     return list;
125   }
126 
127   @Override
128   public T fromString(final String valueAsString)
129     throws IllegalArgumentException
130   {
131     final T value = valueRange.get(valueAsString);
132     if (value != null)
133     {
134       return value;
135     }
136     else
137     {
138       throw new IllegalArgumentException(
139           "The value '" + valueAsString
140               + "' is not a valid value. Valid values are "
141               + valueRange.values());
142     }
143   }
144 
145   @Override
146   public boolean isCommented()
147   {
148     return false;
149   }
150 
151   // --- object basics --------------------------------------------------------
152 
153   /**
154    * Returns the hash code of the object.
155    *
156    * @return the hash code.
157    */
158   @Override
159   public int hashCode()
160   {
161     return valueRange.hashCode();
162   }
163 
164   /**
165    * Returns <code>true</code> if the given object is semantically equal to the
166    * given object, <code>false</code> otherwise.
167    *
168    * @param object the instance to compare to.
169    * @return <code>true</code> if the given object is semantically equal to the
170    *         given object, <code>false</code> otherwise.
171    */
172   @Override
173   public boolean equals(final Object object)
174   {
175     if (this == object)
176     {
177       return true;
178     }
179     else if (object == null || getClass() != object.getClass())
180     {
181       return false;
182     }
183 
184     final CollectionPropertyValueRange<?> other =
185         (CollectionPropertyValueRange<?>) object;
186 
187     return (valueRange.equals(other.valueRange));
188   }
189 
190   @Override
191   public String toString(final Locale locale)
192   {
193     final StringBuilder buffer = new StringBuilder(64);
194     for (String value : valueRange.keySet())
195     {
196       buffer.append(value).append(", ");
197     }
198 
199     final int length = buffer.length();
200     if (length > 0)
201     {
202       buffer.setLength(length - 2);
203     }
204 
205     return buffer.toString();
206   }
207 
208   @Override
209   public String toString()
210   {
211     return toString(Locale.getDefault());
212   }
213 }