1 /***
2 * ComparatorType.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2002 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.enumerations;
27
28 import java.io.Serializable;
29 import java.util.ArrayList;
30 import java.util.Comparator;
31 import java.util.Hashtable;
32
33 /***
34 * This class defines an Enumeration of Comparators. This can be used to select
35 * comparators in Applications that need sorting. The class defines two basic
36 * comparatortype instances: NONE which holds a dummy comparator. DEFAULT which
37 * holds a dummy comparator for Comparable objects (it simply applies the
38 * Comparable's method).
39 *
40 * @author $Author: dbregeon $
41 * @version $Revision: 1.4 $
42 */
43 public class ComparatorType extends Enumeration {
44 static final long serialVersionUID = 475337581038423430L;
45
46 /***
47 * The ComparatorType instances by their name.
48 */
49 private static final Hashtable fromName = new Hashtable();
50
51 /***
52 * The Name of the NONE ComparatorType.
53 */
54 public static final String S_NONE = "NONE";
55
56 /***
57 * The type for which the comparator in the NONE ComparatorType is
58 * applicable.
59 */
60 public static final Class C_NONE = Object.class;
61
62 /***
63 *
64 *
65 * @author $Author: dbregeon $
66 * @version $Revision: 1.4 $
67 */
68 private static final class NoneComparator implements Comparator,
69 Serializable {
70 static final long serialVersionUID = -2704221401840339841L;
71
72 /***
73 * @see Comparator#compare(java.lang.Object, java.lang.Object)
74 */
75 public int compare(final Object o1, final Object o2) {
76 return 0;
77 }
78 }
79
80 /***
81 * The NONE ComparatorType.
82 */
83 public static final ComparatorType NONE = new ComparatorType(S_NONE);
84 static {
85 NONE.setDataType(C_NONE);
86 NONE.setComparator(new NoneComparator());
87 }
88
89 /***
90 * The name of the DEFAULT ComparatorType.
91 */
92 public static final String S_DEFAULT = "DEFAULT";
93
94 /***
95 * The Class to which the DEFAULT Comparator should be applied.
96 */
97 public static final Class C_DEFAULT = Comparable.class;
98
99 /***
100 * The DEFAULT ComparatorType.
101 */
102 public static final ComparatorType DEFAULT = new ComparatorType(S_DEFAULT);
103
104 /***
105 *
106 *
107 * @author $Author: dbregeon $
108 * @version $Revision: 1.4 $
109 */
110 private static final class DefaultComparator implements Comparator,
111 Serializable {
112
113 static final long serialVersionUID = 7081200714691635737L;
114
115 /***
116 * @see Comparator#compare(java.lang.Object, java.lang.Object)
117 */
118 public int compare(final Object o1, final Object o2) {
119 int result = -2;
120 if (o1 instanceof Comparable) {
121 result = ((Comparable) o1).compareTo(o2);
122 }
123 return result;
124 }
125 }
126
127 static {
128 DEFAULT.setDataType(C_DEFAULT);
129 DEFAULT.setComparator(new DefaultComparator());
130 }
131
132 /***
133 * The type to which the associated Comparator applies.
134 */
135 private Class dataType = null;
136
137 /***
138 * The Comparator related to this ComparatorType.
139 */
140 private Comparator comparator = null;
141
142 /***
143 * Creates a new instance of ComparatorType
144 *
145 * @param name
146 * the name of the enumerated value.
147 */
148 protected ComparatorType(String name) {
149 super(name);
150 }
151
152 /***
153 * Enables to set the type that can be used with the associated
154 * ComparatorType.
155 *
156 * @param type
157 * the type used by the associated ComparatorType.
158 */
159 public void setDataType(final Class type) {
160 this.dataType = type;
161 }
162
163 /***
164 * Gives access to the type that is associated to the ComparatorType.
165 *
166 * @return the type currently associated to the ComparatorType
167 */
168 public Class getDataType() {
169 return this.dataType;
170 }
171
172 /***
173 * Enables to set the Comparator for this ComparatorType.
174 *
175 * @param c
176 * the Comparator to use for this ComparatorType.
177 */
178 public void setComparator(final Comparator c) {
179 this.comparator = c;
180 }
181
182 /***
183 * Gives access to the Comparator of this ComparatorType.
184 *
185 * @return the Comparator in this ComparatorType.
186 */
187 public Comparator getComparator() {
188 return this.comparator;
189 }
190
191 /***
192 * This method retrieves a ComparatorType from its name. If no existing
193 * ComparatorType matches this name, a new one is created.
194 *
195 * @param name
196 * the name of the ComparatorType to find.
197 * @return the ComparatorType of the given name.
198 */
199 public static ComparatorType get(final String name) {
200 ComparatorType result = null;
201 if (name != null) {
202 result = (ComparatorType) fromName.get(name);
203 if (result == null) {
204 return new ComparatorType(name);
205 }
206 }
207 return result;
208 }
209
210 /***
211 * This methods gives the instances of this class.
212 *
213 * @return all the instances of this class.
214 */
215 public static ComparatorType[] getComparatorTypes() {
216 return (ComparatorType[]) fromName.values().toArray(
217 new ComparatorType[0]);
218 }
219
220 /***
221 * Looks into the existing ComparatorTypes to find those that apply to a
222 * given type.
223 *
224 * @param dataType
225 * the type for which we look for ComparatorTypes.
226 * @return the existing ComparatorTypes that apply to the given type.
227 */
228 public static ComparatorType[] getComparatorTypes(final Class dataType) {
229 final ArrayList result = new ArrayList();
230 if (dataType != null) {
231 final ComparatorType[] comparators = getComparatorTypes();
232 for (int i = 0; i < comparators.length; i++) {
233 if (comparators[i].getDataType().isAssignableFrom(dataType)) {
234 result.add(comparators[i]);
235 }
236 }
237 }
238 return (ComparatorType[]) result.toArray(new ComparatorType[result
239 .size()]);
240 }
241
242 /***
243 * This method enables to retrieve all the possible values of an Enumeration
244 * class.
245 *
246 * @return the values for this class.
247 */
248 public static Enumeration[] getValues() {
249 return getComparatorTypes();
250 }
251
252 /***
253 * This method is for internal use only.
254 *
255 * @return the Hashtable that links the enumerated values names with the
256 * actual enumerated value.
257 */
258 protected Hashtable getFromName() {
259 return fromName;
260 }
261 }