KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > util > ReuseFactory


1 /*
2
3    Derby - Class com.ihost.cs.ReuseFactory
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.util;
23
24 /**
25     Factory methods for reusable objects. So far, the objects allocated
26     by this factory are all immutable. Any immutable object can be re-used.
27
28     All the methods in this class are static.
29 */

30 public class ReuseFactory {
31
32     /** Private constructor so no instances can be made */
33     private ReuseFactory() {
34     }
35
36     private static final Integer JavaDoc[] staticInts =
37         {new Integer JavaDoc(0), new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3),
38          new Integer JavaDoc(4), new Integer JavaDoc(5), new Integer JavaDoc(6), new Integer JavaDoc(7),
39          new Integer JavaDoc(8), new Integer JavaDoc(9), new Integer JavaDoc(10), new Integer JavaDoc(11),
40          new Integer JavaDoc(12), new Integer JavaDoc(13), new Integer JavaDoc(14), new Integer JavaDoc(15),
41          new Integer JavaDoc(16), new Integer JavaDoc(17), new Integer JavaDoc(18)};
42     private static final Integer JavaDoc FIFTY_TWO = new Integer JavaDoc(52);
43     private static final Integer JavaDoc TWENTY_THREE = new Integer JavaDoc(23);
44     private static final Integer JavaDoc MAXINT = new Integer JavaDoc(Integer.MAX_VALUE);
45     private static final Integer JavaDoc MINUS_ONE = new Integer JavaDoc(-1);
46
47     public static Integer JavaDoc getInteger(int i)
48     {
49         if (i >= 0 && i < staticInts.length)
50         {
51             return staticInts[i];
52         }
53         else
54         {
55             // Look for other common values
56
switch (i)
57             {
58               case 23:
59                 return TWENTY_THREE; // precision of Int
60

61               case 52:
62                 return FIFTY_TWO; // precision of Double
63

64               case Integer.MAX_VALUE:
65                 return MAXINT;
66
67               case -1:
68                 return MINUS_ONE;
69
70               default:
71                 return new Integer JavaDoc(i);
72             }
73         }
74     }
75
76     private static final Short JavaDoc[] staticShorts =
77         {new Short JavaDoc((short) 0), new Short JavaDoc((short) 1), new Short JavaDoc((short) 2),
78          new Short JavaDoc((short) 3), new Short JavaDoc((short) 4), new Short JavaDoc((short) 5),
79          new Short JavaDoc((short) 6), new Short JavaDoc((short) 7), new Short JavaDoc((short) 8),
80          new Short JavaDoc((short) 9), new Short JavaDoc((short) 10)};
81
82     public static Short JavaDoc getShort(short i)
83     {
84         if (i >= 0 && i < staticShorts.length)
85             return staticShorts[i];
86         else
87             return new Short JavaDoc(i);
88     }
89
90     private static final Byte JavaDoc[] staticBytes =
91         {new Byte JavaDoc((byte) 0), new Byte JavaDoc((byte) 1), new Byte JavaDoc((byte) 2),
92          new Byte JavaDoc((byte) 3), new Byte JavaDoc((byte) 4), new Byte JavaDoc((byte) 5),
93          new Byte JavaDoc((byte) 6), new Byte JavaDoc((byte) 7), new Byte JavaDoc((byte) 8),
94          new Byte JavaDoc((byte) 9), new Byte JavaDoc((byte) 10)};
95
96     public static Byte JavaDoc getByte(byte i)
97     {
98         if (i >= 0 && i < staticBytes.length)
99             return staticBytes[i];
100         else
101             return new Byte JavaDoc(i);
102     }
103
104     private static final Long JavaDoc[] staticLongs =
105         {new Long JavaDoc(0), new Long JavaDoc(1), new Long JavaDoc(2),
106          new Long JavaDoc(3), new Long JavaDoc(4), new Long JavaDoc(5),
107          new Long JavaDoc(6), new Long JavaDoc(7), new Long JavaDoc(8),
108          new Long JavaDoc(9), new Long JavaDoc(10)};
109
110     public static Long JavaDoc getLong(long i)
111     {
112         if (i >= 0 && i < staticLongs.length)
113             return staticLongs[(int) i];
114         else
115             return new Long JavaDoc(i);
116     }
117
118     private static final Boolean JavaDoc staticFalse = new Boolean JavaDoc( false);
119     private static final Boolean JavaDoc staticTrue = new Boolean JavaDoc( true);
120
121     public static Boolean JavaDoc getBoolean( boolean b)
122     {
123         return b ? staticTrue : staticFalse;
124     }
125 }
126
Popular Tags