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.util.ArrayList;
19 import java.util.EnumSet;
20 import java.util.List;
21 import java.util.Locale;
22
23 import de.smartics.properties.api.core.domain.PropertyValueRange;
24 import de.smartics.util.lang.Arguments;
25 import de.smartics.util.lang.NullArgumentException;
26
27
28
29
30
31
32 public final class EnumeratedPropertyValueRange<T extends Enum<T>> implements
33 PropertyValueRange<T>
34 {
35
36
37
38
39
40
41
42
43
44
45 private static final long serialVersionUID = 1L;
46
47
48
49
50
51
52
53
54 private final Class<T> enumeration;
55
56
57
58
59 private final EnumIdTranslator<T> translator;
60
61
62
63
64
65
66
67
68
69
70
71 public EnumeratedPropertyValueRange(final Class<T> enumeration)
72 throws NullArgumentException
73 {
74 Arguments.checkNotNull("enumeration", enumeration);
75
76 this.enumeration = enumeration;
77 this.translator = new EnumIdTranslator<T>(enumeration);
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public Class<T> getEnumeration()
94 {
95 return enumeration;
96 }
97
98
99
100 @Override
101 public List<T> getValues()
102 {
103 final List<T> list = new ArrayList<T>(EnumSet.allOf(enumeration));
104 return list;
105 }
106
107 @Override
108 public T fromString(final String valueAsString)
109 throws IllegalArgumentException
110 {
111 return translator.fromString(valueAsString);
112
113 }
114
115 @Override
116 public boolean isCommented()
117 {
118 return true;
119 }
120
121
122
123 @Override
124 public int hashCode()
125 {
126 return enumeration.hashCode();
127 }
128
129 @Override
130 public boolean equals(final Object object)
131 {
132 if (this == object)
133 {
134 return true;
135 }
136 else if (object == null || getClass() != object.getClass())
137 {
138 return false;
139 }
140
141 final EnumeratedPropertyValueRange<?> other =
142 (EnumeratedPropertyValueRange<?>) object;
143
144 return (enumeration.equals(other.enumeration));
145 }
146
147 @Override
148 public String toString(final Locale locale)
149 {
150 final StringBuilder buffer = new StringBuilder(64);
151 for (T element : EnumSet.allOf(enumeration))
152 {
153 buffer.append(element).append(", ");
154 }
155 buffer.setLength(buffer.length() - 2);
156 return buffer.toString();
157 }
158
159 @Override
160 public String toString()
161 {
162 return toString(Locale.getDefault());
163 }
164 }