1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
36
37 public final class CollectionPropertyValueRange<T> implements
38 PropertyValueRange<T>, Serializable
39 {
40
41
42
43
44
45
46
47 private static final long serialVersionUID = 1L;
48
49
50
51
52
53
54
55
56 private final Map<String, T> valueRange;
57
58
59
60
61
62
63
64
65
66
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
78
79
80
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
91
92
93
94
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
117
118
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
152
153
154
155
156
157
158 @Override
159 public int hashCode()
160 {
161 return valueRange.hashCode();
162 }
163
164
165
166
167
168
169
170
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 }