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.Arg;
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 this.enumeration = Arg.checkNotNull("enumeration", enumeration);
75 this.translator = new EnumIdTranslator<T>(enumeration);
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public Class<T> getEnumeration()
92 {
93 return enumeration;
94 }
95
96
97
98 @Override
99 public List<T> getValues()
100 {
101 final List<T> list = new ArrayList<T>(EnumSet.allOf(enumeration));
102 return list;
103 }
104
105 @Override
106 public T fromString(final String valueAsString)
107 throws IllegalArgumentException
108 {
109 return translator.fromString(valueAsString);
110
111 }
112
113 @Override
114 public boolean isCommented()
115 {
116 return true;
117 }
118
119
120
121 @Override
122 public int hashCode()
123 {
124 return enumeration.hashCode();
125 }
126
127 @Override
128 public boolean equals(final Object object)
129 {
130 if (this == object)
131 {
132 return true;
133 }
134 else if (object == null || getClass() != object.getClass())
135 {
136 return false;
137 }
138
139 final EnumeratedPropertyValueRange<?> other =
140 (EnumeratedPropertyValueRange<?>) object;
141
142 return (enumeration.equals(other.enumeration));
143 }
144
145 @Override
146 public String toString(final Locale locale)
147 {
148 final StringBuilder buffer = new StringBuilder(64);
149 for (T element : EnumSet.allOf(enumeration))
150 {
151 buffer.append(element).append(", ");
152 }
153 buffer.setLength(buffer.length() - 2);
154 return buffer.toString();
155 }
156
157 @Override
158 public String toString()
159 {
160 return toString(Locale.getDefault());
161 }
162 }