Coverage Report - org.apache.commons.el.PrimitiveObjects
 
Classes in this File Line Coverage Branch Coverage Complexity
PrimitiveObjects
58%
47/81
18%
12/64
3.643
 
 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  0
 class PrimitiveObjects
 30  
 {
 31  
   //-------------------------------------
 32  
   // Constants
 33  
   //-------------------------------------
 34  
 
 35  1
   static int BYTE_LOWER_BOUND = 0;
 36  1
   static int BYTE_UPPER_BOUND = 255;
 37  1
   static int CHARACTER_LOWER_BOUND = 0;
 38  1
   static int CHARACTER_UPPER_BOUND = 255;
 39  1
   static int SHORT_LOWER_BOUND = -1000;
 40  1
   static int SHORT_UPPER_BOUND = 1000;
 41  1
   static int INTEGER_LOWER_BOUND = -1000;
 42  1
   static int INTEGER_UPPER_BOUND = 1000;
 43  1
   static int LONG_LOWER_BOUND = -1000;
 44  1
   static int LONG_UPPER_BOUND = 1000;
 45  
 
 46  
   //-------------------------------------
 47  
   // Member variables
 48  
   //-------------------------------------
 49  
 
 50  1
   static Byte [] mBytes = createBytes ();
 51  1
   static Character [] mCharacters = createCharacters ();
 52  1
   static Short [] mShorts = createShorts ();
 53  1
   static Integer [] mIntegers = createIntegers ();
 54  1
   static Long [] mLongs = createLongs ();
 55  
 
 56  
   //-------------------------------------
 57  
   // Getting primitive values
 58  
   //-------------------------------------
 59  
   public static Boolean getBoolean (boolean pValue)
 60  
   {
 61  0
     return
 62  
       pValue ?
 63  
       Boolean.TRUE :
 64  
       Boolean.FALSE;
 65  
   }
 66  
 
 67  
   //-------------------------------------
 68  
   public static Byte getByte (byte pValue)
 69  
   {
 70  0
     if (pValue >= BYTE_LOWER_BOUND &&
 71  
         pValue <= BYTE_UPPER_BOUND) {
 72  0
       return mBytes [((int) pValue) - BYTE_LOWER_BOUND];
 73  
     }
 74  
     else {
 75  0
       return new Byte (pValue);
 76  
     }
 77  
   }
 78  
 
 79  
   //-------------------------------------
 80  
   public static Character getCharacter (char pValue)
 81  
   {
 82  0
     if (pValue >= CHARACTER_LOWER_BOUND &&
 83  
         pValue <= CHARACTER_UPPER_BOUND) {
 84  0
       return mCharacters [((int) pValue) - CHARACTER_LOWER_BOUND];
 85  
     }
 86  
     else {
 87  0
       return new Character (pValue);
 88  
     }
 89  
   }
 90  
 
 91  
   //-------------------------------------
 92  
   public static Short getShort (short pValue)
 93  
   {
 94  0
     if (pValue >= SHORT_LOWER_BOUND &&
 95  
         pValue <= SHORT_UPPER_BOUND) {
 96  0
       return mShorts [((int) pValue) - SHORT_LOWER_BOUND];
 97  
     }
 98  
     else {
 99  0
       return new Short (pValue);
 100  
     }
 101  
   }
 102  
 
 103  
   //-------------------------------------
 104  
   public static Integer getInteger (int pValue)
 105  
   {
 106  0
     if (pValue >= INTEGER_LOWER_BOUND &&
 107  
         pValue <= INTEGER_UPPER_BOUND) {
 108  0
       return mIntegers [((int) pValue) - INTEGER_LOWER_BOUND];
 109  
     }
 110  
     else {
 111  0
       return new Integer (pValue);
 112  
     }
 113  
   }
 114  
 
 115  
   //-------------------------------------
 116  
   public static Long getLong (long pValue)
 117  
   {
 118  2
     if (pValue >= LONG_LOWER_BOUND &&
 119  
         pValue <= LONG_UPPER_BOUND) {
 120  2
       return mLongs [((int) pValue) - LONG_LOWER_BOUND];
 121  
     }
 122  
     else {
 123  0
       return new Long (pValue);
 124  
     }
 125  
   }
 126  
 
 127  
   //-------------------------------------
 128  
   public static Float getFloat (float pValue)
 129  
   {
 130  0
     return new Float (pValue);
 131  
   }
 132  
 
 133  
   //-------------------------------------
 134  
   public static Double getDouble (double pValue)
 135  
   {
 136  0
     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  0
     if (pClass == Boolean.TYPE) {
 150  0
       return Boolean.class;
 151  
     }
 152  0
     else if (pClass == Byte.TYPE) {
 153  0
       return Byte.class;
 154  
     }
 155  0
     else if (pClass == Short.TYPE) {
 156  0
       return Short.class;
 157  
     }
 158  0
     else if (pClass == Character.TYPE) {
 159  0
       return Character.class;
 160  
     }
 161  0
     else if (pClass == Integer.TYPE) {
 162  0
       return Integer.class;
 163  
     }
 164  0
     else if (pClass == Long.TYPE) {
 165  0
       return Long.class;
 166  
     }
 167  0
     else if (pClass == Float.TYPE) {
 168  0
       return Float.class;
 169  
     }
 170  0
     else if (pClass == Double.TYPE) {
 171  0
       return Double.class;
 172  
     }
 173  
     else {
 174  0
       return pClass;
 175  
     }
 176  
   }
 177  
 
 178  
   //-------------------------------------
 179  
   // Initializing the cached values
 180  
   //-------------------------------------
 181  
   static Byte [] createBytes ()
 182  
   {
 183  1
     int len = BYTE_UPPER_BOUND - BYTE_LOWER_BOUND + 1;
 184  1
     Byte [] ret = new Byte [len];
 185  1
     byte val = (byte) BYTE_LOWER_BOUND;
 186  257
     for (int i = 0; i < len; i++, val++) {
 187  256
       ret [i] = new Byte (val);
 188  
     }
 189  1
     return ret;
 190  
   }
 191  
 
 192  
   //-------------------------------------
 193  
   static Character [] createCharacters ()
 194  
   {
 195  1
     int len = CHARACTER_UPPER_BOUND - CHARACTER_LOWER_BOUND + 1;
 196  1
     Character [] ret = new Character [len];
 197  1
     char val = (char) CHARACTER_LOWER_BOUND;
 198  257
     for (int i = 0; i < len; i++, val++) {
 199  256
       ret [i] = new Character (val);
 200  
     }
 201  1
     return ret;
 202  
   }
 203  
 
 204  
   //-------------------------------------
 205  
   static Short [] createShorts ()
 206  
   {
 207  1
     int len = SHORT_UPPER_BOUND - SHORT_LOWER_BOUND + 1;
 208  1
     Short [] ret = new Short [len];
 209  1
     short val = (short) SHORT_LOWER_BOUND;
 210  2002
     for (int i = 0; i < len; i++, val++) {
 211  2001
       ret [i] = new Short (val);
 212  
     }
 213  1
     return ret;
 214  
   }
 215  
 
 216  
   //-------------------------------------
 217  
   static Integer [] createIntegers ()
 218  
   {
 219  1
     int len = INTEGER_UPPER_BOUND - INTEGER_LOWER_BOUND + 1;
 220  1
     Integer [] ret = new Integer [len];
 221  1
     int val = (int) INTEGER_LOWER_BOUND;
 222  2002
     for (int i = 0; i < len; i++, val++) {
 223  2001
       ret [i] = new Integer (val);
 224  
     }
 225  1
     return ret;
 226  
   }
 227  
 
 228  
   //-------------------------------------
 229  
   static Long [] createLongs ()
 230  
   {
 231  1
     int len = LONG_UPPER_BOUND - LONG_LOWER_BOUND + 1;
 232  1
     Long [] ret = new Long [len];
 233  1
     long val = (long) LONG_LOWER_BOUND;
 234  2002
     for (int i = 0; i < len; i++, val++) {
 235  2001
       ret [i] = new Long (val);
 236  
     }
 237  1
     return ret;
 238  
   }
 239  
 
 240  
   //-------------------------------------
 241  
 
 242  
 }