View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.el;
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: bayard $
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 [] mBytes = createBytes ();
51    static Character [] mCharacters = createCharacters ();
52    static Short [] mShorts = createShorts ();
53    static Integer [] mIntegers = createIntegers ();
54    static Long [] mLongs = createLongs ();
55  
56    //-------------------------------------
57    // Getting primitive values
58    //-------------------------------------
59    public static Boolean getBoolean (boolean pValue)
60    {
61      return
62        pValue ?
63        Boolean.TRUE :
64        Boolean.FALSE;
65    }
66  
67    //-------------------------------------
68    public static Byte 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 (pValue);
76      }
77    }
78  
79    //-------------------------------------
80    public static Character 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 (pValue);
88      }
89    }
90  
91    //-------------------------------------
92    public static Short 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 (pValue);
100     }
101   }
102 
103   //-------------------------------------
104   public static Integer 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 (pValue);
112     }
113   }
114 
115   //-------------------------------------
116   public static Long 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 (pValue);
124     }
125   }
126 
127   //-------------------------------------
128   public static Float getFloat (float pValue)
129   {
130     return new Float (pValue);
131   }
132 
133   //-------------------------------------
134   public static Double getDouble (double pValue)
135   {
136     return new Double (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 getPrimitiveObjectClass (Class pClass)
148   {
149     if (pClass == Boolean.TYPE) {
150       return Boolean.class;
151     }
152     else if (pClass == Byte.TYPE) {
153       return Byte.class;
154     }
155     else if (pClass == Short.TYPE) {
156       return Short.class;
157     }
158     else if (pClass == Character.TYPE) {
159       return Character.class;
160     }
161     else if (pClass == Integer.TYPE) {
162       return Integer.class;
163     }
164     else if (pClass == Long.TYPE) {
165       return Long.class;
166     }
167     else if (pClass == Float.TYPE) {
168       return Float.class;
169     }
170     else if (pClass == Double.TYPE) {
171       return Double.class;
172     }
173     else {
174       return pClass;
175     }
176   }
177 
178   //-------------------------------------
179   // Initializing the cached values
180   //-------------------------------------
181   static Byte [] createBytes ()
182   {
183     int len = BYTE_UPPER_BOUND - BYTE_LOWER_BOUND + 1;
184     Byte [] ret = new Byte [len];
185     byte val = (byte) BYTE_LOWER_BOUND;
186     for (int i = 0; i < len; i++, val++) {
187       ret [i] = new Byte (val);
188     }
189     return ret;
190   }
191 
192   //-------------------------------------
193   static Character [] createCharacters ()
194   {
195     int len = CHARACTER_UPPER_BOUND - CHARACTER_LOWER_BOUND + 1;
196     Character [] ret = new Character [len];
197     char val = (char) CHARACTER_LOWER_BOUND;
198     for (int i = 0; i < len; i++, val++) {
199       ret [i] = new Character (val);
200     }
201     return ret;
202   }
203 
204   //-------------------------------------
205   static Short [] createShorts ()
206   {
207     int len = SHORT_UPPER_BOUND - SHORT_LOWER_BOUND + 1;
208     Short [] ret = new Short [len];
209     short val = (short) SHORT_LOWER_BOUND;
210     for (int i = 0; i < len; i++, val++) {
211       ret [i] = new Short (val);
212     }
213     return ret;
214   }
215 
216   //-------------------------------------
217   static Integer [] createIntegers ()
218   {
219     int len = INTEGER_UPPER_BOUND - INTEGER_LOWER_BOUND + 1;
220     Integer [] ret = new Integer [len];
221     int val = (int) INTEGER_LOWER_BOUND;
222     for (int i = 0; i < len; i++, val++) {
223       ret [i] = new Integer (val);
224     }
225     return ret;
226   }
227 
228   //-------------------------------------
229   static Long [] createLongs ()
230   {
231     int len = LONG_UPPER_BOUND - LONG_LOWER_BOUND + 1;
232     Long [] ret = new Long [len];
233     long val = (long) LONG_LOWER_BOUND;
234     for (int i = 0; i < len; i++, val++) {
235       ret [i] = new Long (val);
236     }
237     return ret;
238   }
239 
240   //-------------------------------------
241 
242 }