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.Arg;
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 Arg.checkNotNull("values", values);
72 this.valueRange = createMap(values);
73 }
74
75
76
77
78
79
80
81 public CollectionPropertyValueRange(final Collection<T> values)
82 throws NullArgumentException
83 {
84 Arg.checkNotNull("values", values);
85 this.valueRange = createMap(values);
86 }
87
88
89
90
91
92
93
94 private static <T> Map<String, T> createMap(final T[] values)
95 {
96 final Map<String, T> map = new LinkedHashMap<String, T>(values.length);
97 for (T value : values)
98 {
99 map.put(String.valueOf(value), value);
100 }
101 return map;
102 }
103
104 private static <T> Map<String, T> createMap(final Collection<T> values)
105 {
106 final Map<String, T> map = new LinkedHashMap<String, T>(values.size());
107 for (T value : values)
108 {
109 map.put(String.valueOf(value), value);
110 }
111 return map;
112 }
113
114
115
116
117
118 @Override
119 public List<T> getValues()
120 {
121 final List<T> list = new ArrayList<T>(valueRange.values());
122 return list;
123 }
124
125 @Override
126 public T fromString(final String valueAsString)
127 throws IllegalArgumentException
128 {
129 final T value = valueRange.get(valueAsString);
130 if (value != null)
131 {
132 return value;
133 }
134 else
135 {
136 throw new IllegalArgumentException(
137 "The value '" + valueAsString
138 + "' is not a valid value. Valid values are "
139 + valueRange.values());
140 }
141 }
142
143 @Override
144 public boolean isCommented()
145 {
146 return false;
147 }
148
149
150
151
152
153
154
155
156 @Override
157 public int hashCode()
158 {
159 return valueRange.hashCode();
160 }
161
162
163
164
165
166
167
168
169
170 @Override
171 public boolean equals(final Object object)
172 {
173 if (this == object)
174 {
175 return true;
176 }
177 else if (object == null || getClass() != object.getClass())
178 {
179 return false;
180 }
181
182 final CollectionPropertyValueRange<?> other =
183 (CollectionPropertyValueRange<?>) object;
184
185 return (valueRange.equals(other.valueRange));
186 }
187
188 @Override
189 public String toString(final Locale locale)
190 {
191 final StringBuilder buffer = new StringBuilder(64);
192 for (String value : valueRange.keySet())
193 {
194 buffer.append(value).append(", ");
195 }
196
197 final int length = buffer.length();
198 if (length > 0)
199 {
200 buffer.setLength(length - 2);
201 }
202
203 return buffer.toString();
204 }
205
206 @Override
207 public String toString()
208 {
209 return toString(Locale.getDefault());
210 }
211 }