1 | /* |
2 | |
3 | Derby - Class com.ihost.cs.ReuseFactory |
4 | |
5 | Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.util; |
22 | |
23 | /** |
24 | Factory methods for reusable objects. So far, the objects allocated |
25 | by this factory are all immutable. Any immutable object can be re-used. |
26 | |
27 | All the methods in this class are static. |
28 | */ |
29 | public class ReuseFactory { |
30 | |
31 | /** Private constructor so no instances can be made */ |
32 | private ReuseFactory() { |
33 | } |
34 | |
35 | private static final Integer[] staticInts = |
36 | {new Integer(0), new Integer(1), new Integer(2), new Integer(3), |
37 | new Integer(4), new Integer(5), new Integer(6), new Integer(7), |
38 | new Integer(8), new Integer(9), new Integer(10), new Integer(11), |
39 | new Integer(12), new Integer(13), new Integer(14), new Integer(15), |
40 | new Integer(16), new Integer(17), new Integer(18)}; |
41 | private static final Integer FIFTY_TWO = new Integer(52); |
42 | private static final Integer TWENTY_THREE = new Integer(23); |
43 | private static final Integer MAXINT = new Integer(Integer.MAX_VALUE); |
44 | private static final Integer MINUS_ONE = new Integer(-1); |
45 | |
46 | public static Integer getInteger(int i) |
47 | { |
48 | if (i >= 0 && i < staticInts.length) |
49 | { |
50 | return staticInts[i]; |
51 | } |
52 | else |
53 | { |
54 | // Look for other common values |
55 | switch (i) |
56 | { |
57 | case 23: |
58 | return TWENTY_THREE; // precision of Int |
59 | |
60 | case 52: |
61 | return FIFTY_TWO; // precision of Double |
62 | |
63 | case Integer.MAX_VALUE: |
64 | return MAXINT; |
65 | |
66 | case -1: |
67 | return MINUS_ONE; |
68 | |
69 | default: |
70 | return new Integer(i); |
71 | } |
72 | } |
73 | } |
74 | |
75 | private static final Short[] staticShorts = |
76 | {new Short((short) 0), new Short((short) 1), new Short((short) 2), |
77 | new Short((short) 3), new Short((short) 4), new Short((short) 5), |
78 | new Short((short) 6), new Short((short) 7), new Short((short) 8), |
79 | new Short((short) 9), new Short((short) 10)}; |
80 | |
81 | public static Short getShort(short i) |
82 | { |
83 | if (i >= 0 && i < staticShorts.length) |
84 | return staticShorts[i]; |
85 | else |
86 | return new Short(i); |
87 | } |
88 | |
89 | private static final Byte[] staticBytes = |
90 | {new Byte((byte) 0), new Byte((byte) 1), new Byte((byte) 2), |
91 | new Byte((byte) 3), new Byte((byte) 4), new Byte((byte) 5), |
92 | new Byte((byte) 6), new Byte((byte) 7), new Byte((byte) 8), |
93 | new Byte((byte) 9), new Byte((byte) 10)}; |
94 | |
95 | public static Byte getByte(byte i) |
96 | { |
97 | if (i >= 0 && i < staticBytes.length) |
98 | return staticBytes[i]; |
99 | else |
100 | return new Byte(i); |
101 | } |
102 | |
103 | private static final Long[] staticLongs = |
104 | {new Long(0), new Long(1), new Long(2), |
105 | new Long(3), new Long(4), new Long(5), |
106 | new Long(6), new Long(7), new Long(8), |
107 | new Long(9), new Long(10)}; |
108 | |
109 | public static Long getLong(long i) |
110 | { |
111 | if (i >= 0 && i < staticLongs.length) |
112 | return staticLongs[(int) i]; |
113 | else |
114 | return new Long(i); |
115 | } |
116 | |
117 | private static final Boolean staticFalse = new Boolean( false); |
118 | private static final Boolean staticTrue = new Boolean( true); |
119 | |
120 | public static Boolean getBoolean( boolean b) |
121 | { |
122 | return b ? staticTrue : staticFalse; |
123 | } |
124 | } |