View Javadoc

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.Arguments;
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  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    {
74      Arguments.checkNotNull("enumeration", enumeration);
75  
76      this.enumeration = enumeration;
77      this.translator = new EnumIdTranslator<T>(enumeration);
78    }
79  
80    // ****************************** Inner Classes *****************************
81  
82    // ********************************* Methods ********************************
83  
84    // --- init -----------------------------------------------------------------
85  
86    // --- get&set --------------------------------------------------------------
87  
88    /**
89     * Returns the enumeration to provide the value for this range.
90     *
91     * @return the enumeration to provide the value for this range.
92     */
93    public Class<T> getEnumeration()
94    {
95      return enumeration;
96    }
97  
98    // --- business -------------------------------------------------------------
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);// Enum.valueOf(enumeration,
112                                                 // valueAsString);
113   }
114 
115   @Override
116   public boolean isCommented()
117   {
118     return true;
119   }
120 
121   // --- object basics --------------------------------------------------------
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 }