001: /*
002:
003: Derby - Class com.ihost.cs.ReuseFactory
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.util;
023:
024: /**
025: Factory methods for reusable objects. So far, the objects allocated
026: by this factory are all immutable. Any immutable object can be re-used.
027:
028: All the methods in this class are static.
029: */
030: public class ReuseFactory {
031:
032: /** Private constructor so no instances can be made */
033: private ReuseFactory() {
034: }
035:
036: private static final Integer[] staticInts = { new Integer(0),
037: new Integer(1), new Integer(2), new Integer(3),
038: new Integer(4), new Integer(5), new Integer(6),
039: new Integer(7), new Integer(8), new Integer(9),
040: new Integer(10), new Integer(11), new Integer(12),
041: new Integer(13), new Integer(14), new Integer(15),
042: new Integer(16), new Integer(17), new Integer(18) };
043: private static final Integer FIFTY_TWO = new Integer(52);
044: private static final Integer TWENTY_THREE = new Integer(23);
045: private static final Integer MAXINT = new Integer(Integer.MAX_VALUE);
046: private static final Integer MINUS_ONE = new Integer(-1);
047:
048: public static Integer getInteger(int i) {
049: if (i >= 0 && i < staticInts.length) {
050: return staticInts[i];
051: } else {
052: // Look for other common values
053: switch (i) {
054: case 23:
055: return TWENTY_THREE; // precision of Int
056:
057: case 52:
058: return FIFTY_TWO; // precision of Double
059:
060: case Integer.MAX_VALUE:
061: return MAXINT;
062:
063: case -1:
064: return MINUS_ONE;
065:
066: default:
067: return new Integer(i);
068: }
069: }
070: }
071:
072: private static final Short[] staticShorts = { new Short((short) 0),
073: new Short((short) 1), new Short((short) 2),
074: new Short((short) 3), new Short((short) 4),
075: new Short((short) 5), new Short((short) 6),
076: new Short((short) 7), new Short((short) 8),
077: new Short((short) 9), new Short((short) 10) };
078:
079: public static Short getShort(short i) {
080: if (i >= 0 && i < staticShorts.length)
081: return staticShorts[i];
082: else
083: return new Short(i);
084: }
085:
086: private static final Byte[] staticBytes = { new Byte((byte) 0),
087: new Byte((byte) 1), new Byte((byte) 2), new Byte((byte) 3),
088: new Byte((byte) 4), new Byte((byte) 5), new Byte((byte) 6),
089: new Byte((byte) 7), new Byte((byte) 8), new Byte((byte) 9),
090: new Byte((byte) 10) };
091:
092: public static Byte getByte(byte i) {
093: if (i >= 0 && i < staticBytes.length)
094: return staticBytes[i];
095: else
096: return new Byte(i);
097: }
098:
099: private static final Long[] staticLongs = { new Long(0),
100: new Long(1), new Long(2), new Long(3), new Long(4),
101: new Long(5), new Long(6), new Long(7), new Long(8),
102: new Long(9), new Long(10) };
103:
104: public static Long getLong(long i) {
105: if (i >= 0 && i < staticLongs.length)
106: return staticLongs[(int) i];
107: else
108: return new Long(i);
109: }
110:
111: private static final Boolean staticFalse = new Boolean(false);
112: private static final Boolean staticTrue = new Boolean(true);
113:
114: public static Boolean getBoolean(boolean b) {
115: return b ? staticTrue : staticFalse;
116: }
117: }
|