View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.myfaces.shared_orchestra.util;
    20
    21  /**
    22   *
    23   * <p>This converts primitive values to their Object counterparts.
    24   * For bytes and chars, values from 0 to 255 are cached.  For shorts,
    25   * ints, and longs, values -1000 to 1000 are cached.
    26   *
    27   * This is a copy from commons-el PrimitiveObjects class.
    28   *
    29   * @author Nathan Abramson - Art Technology Group
    30   * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author$
    31   **/
    32
    33  class _PrimitiveObjects
    34  {
    35    //-------------------------------------
    36    // Constants
    37    //-------------------------------------
    38
    39    static int BYTE_LOWER_BOUND = 0;
    40    static int BYTE_UPPER_BOUND = 255;
    41    static int CHARACTER_LOWER_BOUND = 0;
    42    static int CHARACTER_UPPER_BOUND = 255;
    43    static int SHORT_LOWER_BOUND = -1000;
    44    static int SHORT_UPPER_BOUND = 1000;
    45    static int INTEGER_LOWER_BOUND = -1000;
    46    static int INTEGER_UPPER_BOUND = 1000;
    47    static int LONG_LOWER_BOUND = -1000;
    48    static int LONG_UPPER_BOUND = 1000;
    49
    50    //-------------------------------------
    51    // Member variables
    52    //-------------------------------------
    53
    54    static Byte [] mBytes = createBytes ();
    55    static Character [] mCharacters = createCharacters ();
    56    static Short [] mShorts = createShorts ();
    57    static Integer [] mIntegers = createIntegers ();
    58    static Long [] mLongs = createLongs ();
    59
    60    //-------------------------------------
    61    // Getting primitive values
    62    //-------------------------------------
    63    public static Boolean getBoolean (boolean pValue)
    64    {
    65      return
    66        pValue ?
    67        Boolean.TRUE :
    68        Boolean.FALSE;
    69    }
    70
    71    //-------------------------------------
    72    public static Byte getByte (byte pValue)
    73    {
    74      if (pValue >= BYTE_LOWER_BOUND &&
    75      pValue <= BYTE_UPPER_BOUND) {
    76        return mBytes [((int) pValue) - BYTE_LOWER_BOUND];
    77      }
    78      else {
    79        return new Byte (pValue);
    80      }
    81    }
    82
    83    //-------------------------------------
    84    public static Character getCharacter (char pValue)
    85    {
    86      if (pValue >= CHARACTER_LOWER_BOUND &&
    87      pValue <= CHARACTER_UPPER_BOUND) {
    88        return mCharacters [((int) pValue) - CHARACTER_LOWER_BOUND];
    89      }
    90      else {
    91        return new Character (pValue);
    92      }
    93    }
    94
    95    //-------------------------------------
    96    public static Short getShort (short pValue)
    97    {
    98      if (pValue >= SHORT_LOWER_BOUND &&
    99      pValue <= SHORT_UPPER_BOUND) {
    100       return mShorts [((int) pValue) - SHORT_LOWER_BOUND];
    101     }
    102     else {
    103       return new Short (pValue);
    104     }
    105   }
    106
    107   //-------------------------------------
    108   public static Integer getInteger (int pValue)
    109   {
    110     if (pValue >= INTEGER_LOWER_BOUND &&
    111     pValue <= INTEGER_UPPER_BOUND) {
    112       return mIntegers [((int) pValue) - INTEGER_LOWER_BOUND];
    113     }
    114     else {
    115       return new Integer (pValue);
    116     }
    117   }
    118
    119   //-------------------------------------
    120   public static Long getLong (long pValue)
    121   {
    122     if (pValue >= LONG_LOWER_BOUND &&
    123     pValue <= LONG_UPPER_BOUND) {
    124       return mLongs [((int) pValue) - LONG_LOWER_BOUND];
    125     }
    126     else {
    127       return new Long (pValue);
    128     }
    129   }
    130
    131   //-------------------------------------
    132   public static Float getFloat (float pValue)
    133   {
    134     return new Float (pValue);
    135   }
    136
    137   //-------------------------------------
    138   public static Double getDouble (double pValue)
    139   {
    140     return new Double (pValue);
    141   }
    142
    143   //-------------------------------------
    144   // Object class equivalents of primitive classes
    145   //-------------------------------------
    146   /**
    147    *
    148    * If the given class is a primitive class, returns the object
    149    * version of that class.  Otherwise, the class is just returned.
    150    **/
    151   public static Class getPrimitiveObjectClass (Class pClass)
    152   {
    153     if (pClass == Boolean.TYPE) {
    154       return Boolean.class;
    155     }
    156     else if (pClass == Byte.TYPE) {
    157       return Byte.class;
    158     }
    159     else if (pClass == Short.TYPE) {
    160       return Short.class;
    161     }
    162     else if (pClass == Character.TYPE) {
    163       return Character.class;
    164     }
    165     else if (pClass == Integer.TYPE) {
    166       return Integer.class;
    167     }
    168     else if (pClass == Long.TYPE) {
    169       return Long.class;
    170     }
    171     else if (pClass == Float.TYPE) {
    172       return Float.class;
    173     }
    174     else if (pClass == Double.TYPE) {
    175       return Double.class;
    176     }
    177     else {
    178       return pClass;
    179     }
    180   }
    181
    182   //-------------------------------------
    183   // Initializing the cached values
    184   //-------------------------------------
    185   static Byte [] createBytes ()
    186   {
    187     int len = BYTE_UPPER_BOUND - BYTE_LOWER_BOUND + 1;
    188     Byte [] ret = new Byte [len];
    189     byte val = (byte) BYTE_LOWER_BOUND;
    190     for (int i = 0; i < len; i++, val++) {
    191       ret [i] = new Byte (val);
    192     }
    193     return ret;
    194   }
    195
    196   //-------------------------------------
    197   static Character [] createCharacters ()
    198   {
    199     int len = CHARACTER_UPPER_BOUND - CHARACTER_LOWER_BOUND + 1;
    200     Character [] ret = new Character [len];
    201     char val = (char) CHARACTER_LOWER_BOUND;
    202     for (int i = 0; i < len; i++, val++) {
    203       ret [i] = new Character (val);
    204     }
    205     return ret;
    206   }
    207
    208   //-------------------------------------
    209   static Short [] createShorts ()
    210   {
    211     int len = SHORT_UPPER_BOUND - SHORT_LOWER_BOUND + 1;
    212     Short [] ret = new Short [len];
    213     short val = (short) SHORT_LOWER_BOUND;
    214     for (int i = 0; i < len; i++, val++) {
    215       ret [i] = new Short (val);
    216     }
    217     return ret;
    218   }
    219
    220   //-------------------------------------
    221   static Integer [] createIntegers ()
    222   {
    223     int len = INTEGER_UPPER_BOUND - INTEGER_LOWER_BOUND + 1;
    224     Integer [] ret = new Integer [len];
    225     int val = (int) INTEGER_LOWER_BOUND;
    226     for (int i = 0; i < len; i++, val++) {
    227       ret [i] = new Integer (val);
    228     }
    229     return ret;
    230   }
    231
    232   //-------------------------------------
    233   static Long [] createLongs ()
    234   {
    235     int len = LONG_UPPER_BOUND - LONG_LOWER_BOUND + 1;
    236     Long [] ret = new Long [len];
    237     long val = (long) LONG_LOWER_BOUND;
    238     for (int i = 0; i < len; i++, val++) {
    239       ret [i] = new Long (val);
    240     }
    241     return ret;
    242   }
    243
    244   //-------------------------------------
    245
    246 }