001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package org.apache.commons.el;
057:
058: /**
059: *
060: * <p>This converts primitive values to their Object counterparts.
061: * For bytes and chars, values from 0 to 255 are cached. For shorts,
062: * ints, and longs, values -1000 to 1000 are cached.
063: *
064: * @author Nathan Abramson - Art Technology Group
065: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
066: **/
067:
068: class PrimitiveObjects {
069: //-------------------------------------
070: // Constants
071: //-------------------------------------
072:
073: static int BYTE_LOWER_BOUND = 0;
074: static int BYTE_UPPER_BOUND = 255;
075: static int CHARACTER_LOWER_BOUND = 0;
076: static int CHARACTER_UPPER_BOUND = 255;
077: static int SHORT_LOWER_BOUND = -1000;
078: static int SHORT_UPPER_BOUND = 1000;
079: static int INTEGER_LOWER_BOUND = -1000;
080: static int INTEGER_UPPER_BOUND = 1000;
081: static int LONG_LOWER_BOUND = -1000;
082: static int LONG_UPPER_BOUND = 1000;
083:
084: //-------------------------------------
085: // Member variables
086: //-------------------------------------
087:
088: static Byte[] mBytes = createBytes();
089: static Character[] mCharacters = createCharacters();
090: static Short[] mShorts = createShorts();
091: static Integer[] mIntegers = createIntegers();
092: static Long[] mLongs = createLongs();
093:
094: //-------------------------------------
095: // Getting primitive values
096: //-------------------------------------
097: public static Boolean getBoolean(boolean pValue) {
098: return pValue ? Boolean.TRUE : Boolean.FALSE;
099: }
100:
101: //-------------------------------------
102: public static Byte getByte(byte pValue) {
103: if (pValue >= BYTE_LOWER_BOUND && pValue <= BYTE_UPPER_BOUND) {
104: return mBytes[((int) pValue) - BYTE_LOWER_BOUND];
105: } else {
106: return new Byte(pValue);
107: }
108: }
109:
110: //-------------------------------------
111: public static Character getCharacter(char pValue) {
112: if (pValue >= CHARACTER_LOWER_BOUND
113: && pValue <= CHARACTER_UPPER_BOUND) {
114: return mCharacters[((int) pValue) - CHARACTER_LOWER_BOUND];
115: } else {
116: return new Character(pValue);
117: }
118: }
119:
120: //-------------------------------------
121: public static Short getShort(short pValue) {
122: if (pValue >= SHORT_LOWER_BOUND && pValue <= SHORT_UPPER_BOUND) {
123: return mShorts[((int) pValue) - SHORT_LOWER_BOUND];
124: } else {
125: return new Short(pValue);
126: }
127: }
128:
129: //-------------------------------------
130: public static Integer getInteger(int pValue) {
131: if (pValue >= INTEGER_LOWER_BOUND
132: && pValue <= INTEGER_UPPER_BOUND) {
133: return mIntegers[((int) pValue) - INTEGER_LOWER_BOUND];
134: } else {
135: return new Integer(pValue);
136: }
137: }
138:
139: //-------------------------------------
140: public static Long getLong(long pValue) {
141: if (pValue >= LONG_LOWER_BOUND && pValue <= LONG_UPPER_BOUND) {
142: return mLongs[((int) pValue) - LONG_LOWER_BOUND];
143: } else {
144: return new Long(pValue);
145: }
146: }
147:
148: //-------------------------------------
149: public static Float getFloat(float pValue) {
150: return new Float(pValue);
151: }
152:
153: //-------------------------------------
154: public static Double getDouble(double pValue) {
155: return new Double(pValue);
156: }
157:
158: //-------------------------------------
159: // Object class equivalents of primitive classes
160: //-------------------------------------
161: /**
162: *
163: * If the given class is a primitive class, returns the object
164: * version of that class. Otherwise, the class is just returned.
165: **/
166: public static Class getPrimitiveObjectClass(Class pClass) {
167: if (pClass == Boolean.TYPE) {
168: return Boolean.class;
169: } else if (pClass == Byte.TYPE) {
170: return Byte.class;
171: } else if (pClass == Short.TYPE) {
172: return Short.class;
173: } else if (pClass == Character.TYPE) {
174: return Character.class;
175: } else if (pClass == Integer.TYPE) {
176: return Integer.class;
177: } else if (pClass == Long.TYPE) {
178: return Long.class;
179: } else if (pClass == Float.TYPE) {
180: return Float.class;
181: } else if (pClass == Double.TYPE) {
182: return Double.class;
183: } else {
184: return pClass;
185: }
186: }
187:
188: //-------------------------------------
189: // Initializing the cached values
190: //-------------------------------------
191: static Byte[] createBytes() {
192: int len = BYTE_UPPER_BOUND - BYTE_LOWER_BOUND + 1;
193: Byte[] ret = new Byte[len];
194: byte val = (byte) BYTE_LOWER_BOUND;
195: for (int i = 0; i < len; i++, val++) {
196: ret[i] = new Byte(val);
197: }
198: return ret;
199: }
200:
201: //-------------------------------------
202: static Character[] createCharacters() {
203: int len = CHARACTER_UPPER_BOUND - CHARACTER_LOWER_BOUND + 1;
204: Character[] ret = new Character[len];
205: char val = (char) CHARACTER_LOWER_BOUND;
206: for (int i = 0; i < len; i++, val++) {
207: ret[i] = new Character(val);
208: }
209: return ret;
210: }
211:
212: //-------------------------------------
213: static Short[] createShorts() {
214: int len = SHORT_UPPER_BOUND - SHORT_LOWER_BOUND + 1;
215: Short[] ret = new Short[len];
216: short val = (short) SHORT_LOWER_BOUND;
217: for (int i = 0; i < len; i++, val++) {
218: ret[i] = new Short(val);
219: }
220: return ret;
221: }
222:
223: //-------------------------------------
224: static Integer[] createIntegers() {
225: int len = INTEGER_UPPER_BOUND - INTEGER_LOWER_BOUND + 1;
226: Integer[] ret = new Integer[len];
227: int val = (int) INTEGER_LOWER_BOUND;
228: for (int i = 0; i < len; i++, val++) {
229: ret[i] = new Integer(val);
230: }
231: return ret;
232: }
233:
234: //-------------------------------------
235: static Long[] createLongs() {
236: int len = LONG_UPPER_BOUND - LONG_LOWER_BOUND + 1;
237: Long[] ret = new Long[len];
238: long val = (long) LONG_LOWER_BOUND;
239: for (int i = 0; i < len; i++, val++) {
240: ret[i] = new Long(val);
241: }
242: return ret;
243: }
244:
245: //-------------------------------------
246:
247: }
|