View Javadoc

1   /***
2    * SortOrder.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.security.InvalidParameterException;
29  import java.util.Hashtable;
30  
31  /***
32   * This class represents a SortOrder (Ascending, Descending, None).
33   * 
34   * The order can be used to multiply with the result of a comparison to get the
35   * actual sort result.
36   * 
37   * This class can be used as a sample for Enumeration sub classes.
38   * 
39   * @author $Author: dbregeon $
40   * @version $Revision: 1.4 $
41   */
42  public class SortOrder extends Enumeration {
43  	static final long serialVersionUID = 5485883576649057849L;
44  
45  	/***
46  	 * The SortOrder instances by their name.
47  	 */
48  	private static final Hashtable fromName = new Hashtable();
49  
50  	/***
51  	 * The name for the 'NONE' enumeration value.
52  	 */
53  	public static final String S_NONE = "NONE";
54  
55  	/***
56  	 * The value for the 'NONE' enumeration value.
57  	 */
58  	public static final int I_NONE = 0;
59  
60  	/***
61  	 * The 'NONE' enumeration value.
62  	 */
63  	public static final SortOrder NONE = new SortOrder(S_NONE, I_NONE);
64  
65  	/***
66  	 * The name for the 'ASCENDING' enumeration value.
67  	 */
68  	public static final String S_ASCENDING = "ASC";
69  
70  	/***
71  	 * The value for the 'ASCENDING' enumeration value.
72  	 */
73  	public static final int I_ASCENDING = 1;
74  
75  	/***
76  	 * The 'ASCENDING' enumeration value.
77  	 */
78  	public static final SortOrder ASCENDING = new SortOrder(S_ASCENDING,
79  			I_ASCENDING);
80  
81  	/***
82  	 * The name for the 'DESCENDING' enumeration value.
83  	 */
84  	public static final String S_DESCENDING = "DESC";
85  
86  	/***
87  	 * The value for the 'DESCENDING' enumeration value.
88  	 */
89  	public static final int I_DESCENDING = -1;
90  
91  	/***
92  	 * The 'DESCENDING' enumeration value.
93  	 */
94  	public static final SortOrder DESCENDING = new SortOrder(S_DESCENDING,
95  			I_DESCENDING);
96  
97  	/***
98  	 * This member represents the order of the instance. It can be multiplied
99  	 * with the result of a comparison to get the acual order to apply.
100 	 */
101 	private final int order;
102 
103 	/***
104 	 * Creates new SortOrder.
105 	 * 
106 	 * @param name
107 	 *            the name of this enumerated value. It cannot be null.
108 	 * @param order
109 	 *            the order value (-1,0 or 1)
110 	 * @throws InvalidParameterException
111 	 *             if name is null,
112 	 * @see Enumeration#Enumeration(String)
113 	 */
114 	protected SortOrder(String name, int order)
115 			throws InvalidParameterException {
116 		super(name);
117 		this.order = order;
118 	}
119 
120 	/***
121 	 * This method enables access to the order member.
122 	 * 
123 	 * @return the order value.
124 	 */
125 	public int getOrder() {
126 		return this.order;
127 	}
128 
129 	/***
130 	 * This method is for internal use only.
131 	 * 
132 	 * @return the Hashtable that links the enumerated values names with the
133 	 *         actual enumerated value.
134 	 */
135 	protected Hashtable getFromName() {
136 		return fromName;
137 	}
138 
139 	/***
140 	 * This method retrieves a SortOrder instance from its name.
141 	 * 
142 	 * @param name
143 	 *            the name of the SortOrder instance.
144 	 * @return the SortOrder that has this name. Null if no value corresponding
145 	 *         to this name was found.
146 	 */
147 	public static SortOrder get(final String name) {
148 		SortOrder result = null;
149 		if (name != null) {
150 			result = (SortOrder) fromName.get(name);
151 		}
152 		return result;
153 	}
154 
155 	/***
156 	 * This methods gives the instances of this class.
157 	 * 
158 	 * @return all the instances of this class.
159 	 */
160 	public static SortOrder[] getSortOrders() {
161 		return (SortOrder[]) fromName.values().toArray(new SortOrder[0]);
162 	}
163 
164 	/***
165 	 * This method enables to retrieve all the possible values of an Enumeration
166 	 * class.
167 	 * 
168 	 * @return the values for this class.
169 	 */
170 	public static Enumeration[] getValues() {
171 		return getSortOrders();
172 	}
173 }