KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > PrimitiveObjects


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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
17 package org.apache.taglibs.standard.lang.jstl;
18
19 /**
20  *
21  * <p>This converts primitive values to their Object counterparts.
22  * For bytes and chars, values from 0 to 255 are cached. For shorts,
23  * ints, and longs, values -1000 to 1000 are cached.
24  *
25  * @author Nathan Abramson - Art Technology Group
26  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
27  **/

28
29 class PrimitiveObjects
30 {
31   //-------------------------------------
32
// Constants
33
//-------------------------------------
34

35   static int BYTE_LOWER_BOUND = 0;
36   static int BYTE_UPPER_BOUND = 255;
37   static int CHARACTER_LOWER_BOUND = 0;
38   static int CHARACTER_UPPER_BOUND = 255;
39   static int SHORT_LOWER_BOUND = -1000;
40   static int SHORT_UPPER_BOUND = 1000;
41   static int INTEGER_LOWER_BOUND = -1000;
42   static int INTEGER_UPPER_BOUND = 1000;
43   static int LONG_LOWER_BOUND = -1000;
44   static int LONG_UPPER_BOUND = 1000;
45
46   //-------------------------------------
47
// Member variables
48
//-------------------------------------
49

50   static Byte JavaDoc [] mBytes = createBytes ();
51   static Character JavaDoc [] mCharacters = createCharacters ();
52   static Short JavaDoc [] mShorts = createShorts ();
53   static Integer JavaDoc [] mIntegers = createIntegers ();
54   static Long JavaDoc [] mLongs = createLongs ();
55
56   //-------------------------------------
57
// Getting primitive values
58
//-------------------------------------
59
public static Boolean JavaDoc getBoolean (boolean pValue)
60   {
61     return
62       pValue ?
63       Boolean.TRUE :
64       Boolean.FALSE;
65   }
66
67   //-------------------------------------
68
public static Byte JavaDoc getByte (byte pValue)
69   {
70     if (pValue >= BYTE_LOWER_BOUND &&
71     pValue <= BYTE_UPPER_BOUND) {
72       return mBytes [((int) pValue) - BYTE_LOWER_BOUND];
73     }
74     else {
75       return new Byte JavaDoc (pValue);
76     }
77   }
78
79   //-------------------------------------
80
public static Character JavaDoc getCharacter (char pValue)
81   {
82     if (pValue >= CHARACTER_LOWER_BOUND &&
83     pValue <= CHARACTER_UPPER_BOUND) {
84       return mCharacters [((int) pValue) - CHARACTER_LOWER_BOUND];
85     }
86     else {
87       return new Character JavaDoc (pValue);
88     }
89   }
90
91   //-------------------------------------
92
public static Short JavaDoc getShort (short pValue)
93   {
94     if (pValue >= SHORT_LOWER_BOUND &&
95     pValue <= SHORT_UPPER_BOUND) {
96       return mShorts [((int) pValue) - SHORT_LOWER_BOUND];
97     }
98     else {
99       return new Short JavaDoc (pValue);
100     }
101   }
102
103   //-------------------------------------
104
public static Integer JavaDoc getInteger (int pValue)
105   {
106     if (pValue >= INTEGER_LOWER_BOUND &&
107     pValue <= INTEGER_UPPER_BOUND) {
108       return mIntegers [((int) pValue) - INTEGER_LOWER_BOUND];
109     }
110     else {
111       return new Integer JavaDoc (pValue);
112     }
113   }
114
115   //-------------------------------------
116
public static Long JavaDoc getLong (long pValue)
117   {
118     if (pValue >= LONG_LOWER_BOUND &&
119     pValue <= LONG_UPPER_BOUND) {
120       return mLongs [((int) pValue) - LONG_LOWER_BOUND];
121     }
122     else {
123       return new Long JavaDoc (pValue);
124     }
125   }
126
127   //-------------------------------------
128
public static Float JavaDoc getFloat (float pValue)
129   {
130     return new Float JavaDoc (pValue);
131   }
132
133   //-------------------------------------
134
public static Double JavaDoc getDouble (double pValue)
135   {
136     return new Double JavaDoc (pValue);
137   }
138
139   //-------------------------------------
140
// Object class equivalents of primitive classes
141
//-------------------------------------
142
/**
143    *
144    * If the given class is a primitive class, returns the object
145    * version of that class. Otherwise, the class is just returned.
146    **/

147   public static Class JavaDoc getPrimitiveObjectClass (Class JavaDoc pClass)
148   {
149     if (pClass == Boolean.TYPE) {
150       return Boolean JavaDoc.class;
151     }
152     else if (pClass == Byte.TYPE) {
153       return Byte JavaDoc.class;
154     }
155     else if (pClass == Short.TYPE) {
156       return Short JavaDoc.class;
157     }
158     else if (pClass == Character.TYPE) {
159       return Character JavaDoc.class;
160     }
161     else if (pClass == Integer.TYPE) {
162       return Integer JavaDoc.class;
163     }
164     else if (pClass == Long.TYPE) {
165       return Long JavaDoc.class;
166     }
167     else if (pClass == Float.TYPE) {
168       return Float JavaDoc.class;
169     }
170     else if (pClass == Double.TYPE) {
171       return Double JavaDoc.class;
172     }
173     else {
174       return pClass;
175     }
176   }
177
178   //-------------------------------------
179
// Initializing the cached values
180
//-------------------------------------
181
static Byte JavaDoc [] createBytes ()
182   {
183     int len = BYTE_UPPER_BOUND - BYTE_LOWER_BOUND + 1;
184     Byte JavaDoc [] ret = new Byte JavaDoc [len];
185     byte val = (byte) BYTE_LOWER_BOUND;
186     for (int i = 0; i < len; i++, val++) {
187       ret [i] = new Byte JavaDoc (val);
188     }
189     return ret;
190   }
191
192   //-------------------------------------
193
static Character JavaDoc [] createCharacters ()
194   {
195     int len = CHARACTER_UPPER_BOUND - CHARACTER_LOWER_BOUND + 1;
196     Character JavaDoc [] ret = new Character JavaDoc [len];
197     char val = (char) CHARACTER_LOWER_BOUND;
198     for (int i = 0; i < len; i++, val++) {
199       ret [i] = new Character JavaDoc (val);
200     }
201     return ret;
202   }
203
204   //-------------------------------------
205
static Short JavaDoc [] createShorts ()
206   {
207     int len = SHORT_UPPER_BOUND - SHORT_LOWER_BOUND + 1;
208     Short JavaDoc [] ret = new Short JavaDoc [len];
209     short val = (short) SHORT_LOWER_BOUND;
210     for (int i = 0; i < len; i++, val++) {
211       ret [i] = new Short JavaDoc (val);
212     }
213     return ret;
214   }
215
216   //-------------------------------------
217
static Integer JavaDoc [] createIntegers ()
218   {
219     int len = INTEGER_UPPER_BOUND - INTEGER_LOWER_BOUND + 1;
220     Integer JavaDoc [] ret = new Integer JavaDoc [len];
221     int val = (int) INTEGER_LOWER_BOUND;
222     for (int i = 0; i < len; i++, val++) {
223       ret [i] = new Integer JavaDoc (val);
224     }
225     return ret;
226   }
227
228   //-------------------------------------
229
static Long JavaDoc [] createLongs ()
230   {
231     int len = LONG_UPPER_BOUND - LONG_LOWER_BOUND + 1;
232     Long JavaDoc [] ret = new Long JavaDoc [len];
233     long val = (long) LONG_LOWER_BOUND;
234     for (int i = 0; i < len; i++, val++) {
235       ret [i] = new Long JavaDoc (val);
236     }
237     return ret;
238   }
239
240   //-------------------------------------
241

242 }
243
Popular Tags