Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EnumeratedPropertyValueRange |
|
|
1.6666666666666667;1.667 |
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.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 | * A property value range backed up by an enumeration of values. |
|
29 | * |
|
30 | * @param <T> the type of the enumeration. |
|
31 | */ |
|
32 | 0 | public final class EnumeratedPropertyValueRange<T extends Enum<T>> implements |
33 | PropertyValueRange<T> |
|
34 | { |
|
35 | // ********************************* Fields ********************************* |
|
36 | ||
37 | // --- constants ------------------------------------------------------------ |
|
38 | ||
39 | /** |
|
40 | * The class version identifier. |
|
41 | * <p> |
|
42 | * The value of this constant is {@value}. |
|
43 | * </p> |
|
44 | */ |
|
45 | private static final long serialVersionUID = 1L; |
|
46 | ||
47 | // --- members -------------------------------------------------------------- |
|
48 | ||
49 | /** |
|
50 | * The enumeration to provide the value for this range. |
|
51 | * |
|
52 | * @serial |
|
53 | */ |
|
54 | private final Class<T> enumeration; |
|
55 | ||
56 | /** |
|
57 | * Translator that supports static <code>fromString</code> methods. |
|
58 | */ |
|
59 | private final EnumIdTranslator<T> translator; |
|
60 | ||
61 | // ****************************** Initializer ******************************* |
|
62 | ||
63 | // ****************************** Constructors ****************************** |
|
64 | ||
65 | /** |
|
66 | * Default constructor. |
|
67 | * |
|
68 | * @param enumeration the enumeration to provide the value for this range. |
|
69 | * @throws NullArgumentException of {code enumeration} is <code>null</code>. |
|
70 | */ |
|
71 | public EnumeratedPropertyValueRange(final Class<T> enumeration) |
|
72 | throws NullArgumentException |
|
73 | 0 | { |
74 | 0 | this.enumeration = Arg.checkNotNull("enumeration", enumeration); |
75 | 0 | this.translator = new EnumIdTranslator<T>(enumeration); |
76 | 0 | } |
77 | ||
78 | // ****************************** Inner Classes ***************************** |
|
79 | ||
80 | // ********************************* Methods ******************************** |
|
81 | ||
82 | // --- init ----------------------------------------------------------------- |
|
83 | ||
84 | // --- get&set -------------------------------------------------------------- |
|
85 | ||
86 | /** |
|
87 | * Returns the enumeration to provide the value for this range. |
|
88 | * |
|
89 | * @return the enumeration to provide the value for this range. |
|
90 | */ |
|
91 | public Class<T> getEnumeration() |
|
92 | { |
|
93 | 0 | return enumeration; |
94 | } |
|
95 | ||
96 | // --- business ------------------------------------------------------------- |
|
97 | ||
98 | @Override |
|
99 | public List<T> getValues() |
|
100 | { |
|
101 | 0 | final List<T> list = new ArrayList<T>(EnumSet.allOf(enumeration)); |
102 | 0 | return list; |
103 | } |
|
104 | ||
105 | @Override |
|
106 | public T fromString(final String valueAsString) |
|
107 | throws IllegalArgumentException |
|
108 | { |
|
109 | 0 | return translator.fromString(valueAsString);// Enum.valueOf(enumeration, |
110 | // valueAsString); |
|
111 | } |
|
112 | ||
113 | @Override |
|
114 | public boolean isCommented() |
|
115 | { |
|
116 | 0 | return true; |
117 | } |
|
118 | ||
119 | // --- object basics -------------------------------------------------------- |
|
120 | ||
121 | @Override |
|
122 | public int hashCode() |
|
123 | { |
|
124 | 0 | return enumeration.hashCode(); |
125 | } |
|
126 | ||
127 | @Override |
|
128 | public boolean equals(final Object object) |
|
129 | { |
|
130 | 0 | if (this == object) |
131 | { |
|
132 | 0 | return true; |
133 | } |
|
134 | 0 | else if (object == null || getClass() != object.getClass()) |
135 | { |
|
136 | 0 | return false; |
137 | } |
|
138 | ||
139 | 0 | final EnumeratedPropertyValueRange<?> other = |
140 | (EnumeratedPropertyValueRange<?>) object; |
|
141 | ||
142 | 0 | return (enumeration.equals(other.enumeration)); |
143 | } |
|
144 | ||
145 | @Override |
|
146 | public String toString(final Locale locale) |
|
147 | { |
|
148 | 0 | final StringBuilder buffer = new StringBuilder(64); |
149 | 0 | for (T element : EnumSet.allOf(enumeration)) |
150 | { |
|
151 | 0 | buffer.append(element).append(", "); |
152 | } |
|
153 | 0 | buffer.setLength(buffer.length() - 2); |
154 | 0 | return buffer.toString(); |
155 | } |
|
156 | ||
157 | @Override |
|
158 | public String toString() |
|
159 | { |
|
160 | 0 | return toString(Locale.getDefault()); |
161 | } |
|
162 | } |