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.trinidad.util;
20  
21  /**
22   * This class has been moved from bali share.
23   * Class containing various integer utilities. It caches commonly
24   * used Integer objects and String representations of Integers to avoid
25   * expensive object creations.
26   * <p>
27   * IntegerUtils is used throughout most Bali projects, and clients are also
28   * encouraged to use it for increased performance.
29   * <p>
30   * @since Inspector 0.2
31   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-impl/src/main/java/oracle/adfinternal/view/faces/util/IntegerUtils.java#0 $) $Date: 10-nov-2005.18:49:09 $
32   */
33  public final class IntegerUtils
34  {
35    private IntegerUtils()
36    {
37    }
38  
39    /**
40     * @return a String value corresponding to the specified integer, possibly
41     * creating a new String object if a cached one does not exist.
42     */
43    public static String getString(
44      int intValue
45      )
46    {
47      int cacheIndex = intValue - _START_INTEGER;
48  
49      if ((cacheIndex >= _NUM_CACHED) || (cacheIndex < 0))
50      {
51        return String.valueOf(intValue);
52      }
53      else
54      {
55        String cachedValue = _sCachedStrings[cacheIndex];
56  
57        if (cachedValue == null)
58        {
59          cachedValue = String.valueOf(intValue);
60  
61          _sCachedStrings[cacheIndex] = cachedValue;
62        }
63  
64        return cachedValue;
65      }
66    }
67  
68    /**
69     * @return a String value corresponding to the specified long, possibly
70     * creating a new String object if a cached one does not exist.
71     */
72    public static String getString(long longValue)
73    {
74      if ((longValue >= _START_INTEGER) && (longValue <= _NUM_CACHED))
75        return getString((int) longValue);
76      return String.valueOf(longValue);
77    }
78  
79    /**
80     * @return a String value corresponding to the specified Integer, possibly
81     * creating a new String object if a cached one does not exist.
82     */
83    public static String getString(
84      Integer intObject
85      )
86    {
87      return getString(intObject.intValue());
88    }
89  
90    // index of first cached value
91    private static final int _START_INTEGER = -10;
92  
93    // number of cached values
94    private static final int _NUM_CACHED = 266;
95  
96    // cache of int Strings
97    private static String[] _sCachedStrings = new String[_NUM_CACHED];
98  }