KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > el > PrimitiveObjects


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 1999 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
27  * Foundation" must not be used to endorse or promote products derived
28  * from this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  */

55
56 package org.apache.commons.el;
57
58 /**
59  *
60  * <p>This converts primitive values to their Object counterparts.
61  * For bytes and chars, values from 0 to 255 are cached. For shorts,
62  * ints, and longs, values -1000 to 1000 are cached.
63  *
64  * @author Nathan Abramson - Art Technology Group
65  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
66  **/

67
68 class PrimitiveObjects
69 {
70   //-------------------------------------
71
// Constants
72
//-------------------------------------
73

74   static int BYTE_LOWER_BOUND = 0;
75   static int BYTE_UPPER_BOUND = 255;
76   static int CHARACTER_LOWER_BOUND = 0;
77   static int CHARACTER_UPPER_BOUND = 255;
78   static int SHORT_LOWER_BOUND = -1000;
79   static int SHORT_UPPER_BOUND = 1000;
80   static int INTEGER_LOWER_BOUND = -1000;
81   static int INTEGER_UPPER_BOUND = 1000;
82   static int LONG_LOWER_BOUND = -1000;
83   static int LONG_UPPER_BOUND = 1000;
84
85   //-------------------------------------
86
// Member variables
87
//-------------------------------------
88

89   static Byte JavaDoc [] mBytes = createBytes ();
90   static Character JavaDoc [] mCharacters = createCharacters ();
91   static Short JavaDoc [] mShorts = createShorts ();
92   static Integer JavaDoc [] mIntegers = createIntegers ();
93   static Long JavaDoc [] mLongs = createLongs ();
94
95   //-------------------------------------
96
// Getting primitive values
97
//-------------------------------------
98
public static Boolean JavaDoc getBoolean (boolean pValue)
99   {
100     return
101       pValue ?
102       Boolean.TRUE :
103       Boolean.FALSE;
104   }
105
106   //-------------------------------------
107
public static Byte JavaDoc getByte (byte pValue)
108   {
109     if (pValue >= BYTE_LOWER_BOUND &&
110     pValue <= BYTE_UPPER_BOUND) {
111       return mBytes [((int) pValue) - BYTE_LOWER_BOUND];
112     }
113     else {
114       return new Byte JavaDoc (pValue);
115     }
116   }
117
118   //-------------------------------------
119
public static Character JavaDoc getCharacter (char pValue)
120   {
121     if (pValue >= CHARACTER_LOWER_BOUND &&
122     pValue <= CHARACTER_UPPER_BOUND) {
123       return mCharacters [((int) pValue) - CHARACTER_LOWER_BOUND];
124     }
125     else {
126       return new Character JavaDoc (pValue);
127     }
128   }
129
130   //-------------------------------------
131
public static Short JavaDoc getShort (short pValue)
132   {
133     if (pValue >= SHORT_LOWER_BOUND &&
134     pValue <= SHORT_UPPER_BOUND) {
135       return mShorts [((int) pValue) - SHORT_LOWER_BOUND];
136     }
137     else {
138       return new Short JavaDoc (pValue);
139     }
140   }
141
142   //-------------------------------------
143
public static Integer JavaDoc getInteger (int pValue)
144   {
145     if (pValue >= INTEGER_LOWER_BOUND &&
146     pValue <= INTEGER_UPPER_BOUND) {
147       return mIntegers [((int) pValue) - INTEGER_LOWER_BOUND];
148     }
149     else {
150       return new Integer JavaDoc (pValue);
151     }
152   }
153
154   //-------------------------------------
155
public static Long JavaDoc getLong (long pValue)
156   {
157     if (pValue >= LONG_LOWER_BOUND &&
158     pValue <= LONG_UPPER_BOUND) {
159       return mLongs [((int) pValue) - LONG_LOWER_BOUND];
160     }
161     else {
162       return new Long JavaDoc (pValue);
163     }
164   }
165
166   //-------------------------------------
167
public static Float JavaDoc getFloat (float pValue)
168   {
169     return new Float JavaDoc (pValue);
170   }
171
172   //-------------------------------------
173
public static Double JavaDoc getDouble (double pValue)
174   {
175     return new Double JavaDoc (pValue);
176   }
177
178   //-------------------------------------
179
// Object class equivalents of primitive classes
180
//-------------------------------------
181
/**
182    *
183    * If the given class is a primitive class, returns the object
184    * version of that class. Otherwise, the class is just returned.
185    **/

186   public static Class JavaDoc getPrimitiveObjectClass (Class JavaDoc pClass)
187   {
188     if (pClass == Boolean.TYPE) {
189       return Boolean JavaDoc.class;
190     }
191     else if (pClass == Byte.TYPE) {
192       return Byte JavaDoc.class;
193     }
194     else if (pClass == Short.TYPE) {
195       return Short JavaDoc.class;
196     }
197     else if (pClass == Character.TYPE) {
198       return Character JavaDoc.class;
199     }
200     else if (pClass == Integer.TYPE) {
201       return Integer JavaDoc.class;
202     }
203     else if (pClass == Long.TYPE) {
204       return Long JavaDoc.class;
205     }
206     else if (pClass == Float.TYPE) {
207       return Float JavaDoc.class;
208     }
209     else if (pClass == Double.TYPE) {
210       return Double JavaDoc.class;
211     }
212     else {
213       return pClass;
214     }
215   }
216
217   //-------------------------------------
218
// Initializing the cached values
219
//-------------------------------------
220
static Byte JavaDoc [] createBytes ()
221   {
222     int len = BYTE_UPPER_BOUND - BYTE_LOWER_BOUND + 1;
223     Byte JavaDoc [] ret = new Byte JavaDoc [len];
224     byte val = (byte) BYTE_LOWER_BOUND;
225     for (int i = 0; i < len; i++, val++) {
226       ret [i] = new Byte JavaDoc (val);
227     }
228     return ret;
229   }
230
231   //-------------------------------------
232
static Character JavaDoc [] createCharacters ()
233   {
234     int len = CHARACTER_UPPER_BOUND - CHARACTER_LOWER_BOUND + 1;
235     Character JavaDoc [] ret = new Character JavaDoc [len];
236     char val = (char) CHARACTER_LOWER_BOUND;
237     for (int i = 0; i < len; i++, val++) {
238       ret [i] = new Character JavaDoc (val);
239     }
240     return ret;
241   }
242
243   //-------------------------------------
244
static Short JavaDoc [] createShorts ()
245   {
246     int len = SHORT_UPPER_BOUND - SHORT_LOWER_BOUND + 1;
247     Short JavaDoc [] ret = new Short JavaDoc [len];
248     short val = (short) SHORT_LOWER_BOUND;
249     for (int i = 0; i < len; i++, val++) {
250       ret [i] = new Short JavaDoc (val);
251     }
252     return ret;
253   }
254
255   //-------------------------------------
256
static Integer JavaDoc [] createIntegers ()
257   {
258     int len = INTEGER_UPPER_BOUND - INTEGER_LOWER_BOUND + 1;
259     Integer JavaDoc [] ret = new Integer JavaDoc [len];
260     int val = (int) INTEGER_LOWER_BOUND;
261     for (int i = 0; i < len; i++, val++) {
262       ret [i] = new Integer JavaDoc (val);
263     }
264     return ret;
265   }
266
267   //-------------------------------------
268
static Long JavaDoc [] createLongs ()
269   {
270     int len = LONG_UPPER_BOUND - LONG_LOWER_BOUND + 1;
271     Long JavaDoc [] ret = new Long JavaDoc [len];
272     long val = (long) LONG_LOWER_BOUND;
273     for (int i = 0; i < len; i++, val++) {
274       ret [i] = new Long JavaDoc (val);
275     }
276     return ret;
277   }
278
279   //-------------------------------------
280

281 }
282
Popular Tags