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. package org.apache.commons.el;
  56. /**
  57. *
  58. * <p>This converts primitive values to their Object counterparts.
  59. * For bytes and chars, values from 0 to 255 are cached. For shorts,
  60. * ints, and longs, values -1000 to 1000 are cached.
  61. *
  62. * @author Nathan Abramson - Art Technology Group
  63. * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
  64. **/
  65. class PrimitiveObjects
  66. {
  67. //-------------------------------------
  68. // Constants
  69. //-------------------------------------
  70. static int BYTE_LOWER_BOUND = 0;
  71. static int BYTE_UPPER_BOUND = 255;
  72. static int CHARACTER_LOWER_BOUND = 0;
  73. static int CHARACTER_UPPER_BOUND = 255;
  74. static int SHORT_LOWER_BOUND = -1000;
  75. static int SHORT_UPPER_BOUND = 1000;
  76. static int INTEGER_LOWER_BOUND = -1000;
  77. static int INTEGER_UPPER_BOUND = 1000;
  78. static int LONG_LOWER_BOUND = -1000;
  79. static int LONG_UPPER_BOUND = 1000;
  80. //-------------------------------------
  81. // Member variables
  82. //-------------------------------------
  83. static Byte [] mBytes = createBytes ();
  84. static Character [] mCharacters = createCharacters ();
  85. static Short [] mShorts = createShorts ();
  86. static Integer [] mIntegers = createIntegers ();
  87. static Long [] mLongs = createLongs ();
  88. //-------------------------------------
  89. // Getting primitive values
  90. //-------------------------------------
  91. public static Boolean getBoolean (boolean pValue)
  92. {
  93. return
  94. pValue ?
  95. Boolean.TRUE :
  96. Boolean.FALSE;
  97. }
  98. //-------------------------------------
  99. public static Byte getByte (byte pValue)
  100. {
  101. if (pValue >= BYTE_LOWER_BOUND &&
  102. pValue <= BYTE_UPPER_BOUND) {
  103. return mBytes [((int) pValue) - BYTE_LOWER_BOUND];
  104. }
  105. else {
  106. return new Byte (pValue);
  107. }
  108. }
  109. //-------------------------------------
  110. public static Character getCharacter (char pValue)
  111. {
  112. if (pValue >= CHARACTER_LOWER_BOUND &&
  113. pValue <= CHARACTER_UPPER_BOUND) {
  114. return mCharacters [((int) pValue) - CHARACTER_LOWER_BOUND];
  115. }
  116. else {
  117. return new Character (pValue);
  118. }
  119. }
  120. //-------------------------------------
  121. public static Short getShort (short pValue)
  122. {
  123. if (pValue >= SHORT_LOWER_BOUND &&
  124. pValue <= SHORT_UPPER_BOUND) {
  125. return mShorts [((int) pValue) - SHORT_LOWER_BOUND];
  126. }
  127. else {
  128. return new Short (pValue);
  129. }
  130. }
  131. //-------------------------------------
  132. public static Integer getInteger (int pValue)
  133. {
  134. if (pValue >= INTEGER_LOWER_BOUND &&
  135. pValue <= INTEGER_UPPER_BOUND) {
  136. return mIntegers [((int) pValue) - INTEGER_LOWER_BOUND];
  137. }
  138. else {
  139. return new Integer (pValue);
  140. }
  141. }
  142. //-------------------------------------
  143. public static Long getLong (long pValue)
  144. {
  145. if (pValue >= LONG_LOWER_BOUND &&
  146. pValue <= LONG_UPPER_BOUND) {
  147. return mLongs [((int) pValue) - LONG_LOWER_BOUND];
  148. }
  149. else {
  150. return new Long (pValue);
  151. }
  152. }
  153. //-------------------------------------
  154. public static Float getFloat (float pValue)
  155. {
  156. return new Float (pValue);
  157. }
  158. //-------------------------------------
  159. public static Double getDouble (double pValue)
  160. {
  161. return new Double (pValue);
  162. }
  163. //-------------------------------------
  164. // Object class equivalents of primitive classes
  165. //-------------------------------------
  166. /**
  167. *
  168. * If the given class is a primitive class, returns the object
  169. * version of that class. Otherwise, the class is just returned.
  170. **/
  171. public static Class getPrimitiveObjectClass (Class pClass)
  172. {
  173. if (pClass == Boolean.TYPE) {
  174. return Boolean.class;
  175. }
  176. else if (pClass == Byte.TYPE) {
  177. return Byte.class;
  178. }
  179. else if (pClass == Short.TYPE) {
  180. return Short.class;
  181. }
  182. else if (pClass == Character.TYPE) {
  183. return Character.class;
  184. }
  185. else if (pClass == Integer.TYPE) {
  186. return Integer.class;
  187. }
  188. else if (pClass == Long.TYPE) {
  189. return Long.class;
  190. }
  191. else if (pClass == Float.TYPE) {
  192. return Float.class;
  193. }
  194. else if (pClass == Double.TYPE) {
  195. return Double.class;
  196. }
  197. else {
  198. return pClass;
  199. }
  200. }
  201. //-------------------------------------
  202. // Initializing the cached values
  203. //-------------------------------------
  204. static Byte [] createBytes ()
  205. {
  206. int len = BYTE_UPPER_BOUND - BYTE_LOWER_BOUND + 1;
  207. Byte [] ret = new Byte [len];
  208. byte val = (byte) BYTE_LOWER_BOUND;
  209. for (int i = 0; i < len; i++, val++) {
  210. ret [i] = new Byte (val);
  211. }
  212. return ret;
  213. }
  214. //-------------------------------------
  215. static Character [] createCharacters ()
  216. {
  217. int len = CHARACTER_UPPER_BOUND - CHARACTER_LOWER_BOUND + 1;
  218. Character [] ret = new Character [len];
  219. char val = (char) CHARACTER_LOWER_BOUND;
  220. for (int i = 0; i < len; i++, val++) {
  221. ret [i] = new Character (val);
  222. }
  223. return ret;
  224. }
  225. //-------------------------------------
  226. static Short [] createShorts ()
  227. {
  228. int len = SHORT_UPPER_BOUND - SHORT_LOWER_BOUND + 1;
  229. Short [] ret = new Short [len];
  230. short val = (short) SHORT_LOWER_BOUND;
  231. for (int i = 0; i < len; i++, val++) {
  232. ret [i] = new Short (val);
  233. }
  234. return ret;
  235. }
  236. //-------------------------------------
  237. static Integer [] createIntegers ()
  238. {
  239. int len = INTEGER_UPPER_BOUND - INTEGER_LOWER_BOUND + 1;
  240. Integer [] ret = new Integer [len];
  241. int val = (int) INTEGER_LOWER_BOUND;
  242. for (int i = 0; i < len; i++, val++) {
  243. ret [i] = new Integer (val);
  244. }
  245. return ret;
  246. }
  247. //-------------------------------------
  248. static Long [] createLongs ()
  249. {
  250. int len = LONG_UPPER_BOUND - LONG_LOWER_BOUND + 1;
  251. Long [] ret = new Long [len];
  252. long val = (long) LONG_LOWER_BOUND;
  253. for (int i = 0; i < len; i++, val++) {
  254. ret [i] = new Long (val);
  255. }
  256. return ret;
  257. }
  258. //-------------------------------------
  259. }