001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package com.mysql.jdbc;
026:
027: /**
028: * Represents various constants used in the driver.
029: *
030: * @author Mark Matthews
031: *
032: * @version $Id: Constants.java 6433 2007-05-18 18:38:56Z mmatthews $
033: */
034: public class Constants {
035: /**
036: * Avoids allocation of empty byte[] when representing 0-length strings.
037: */
038: public final static byte[] EMPTY_BYTE_ARRAY = new byte[0];
039:
040: /**
041: * I18N'd representation of the abbreviation for "ms"
042: */
043: public final static String MILLIS_I18N = Messages
044: .getString("Milliseconds");
045:
046: public final static byte[] SLASH_STAR_SPACE_AS_BYTES = new byte[] {
047: (byte) '/', (byte) '*', (byte) ' ' };
048:
049: public final static byte[] SPACE_STAR_SLASH_SPACE_AS_BYTES = new byte[] {
050: (byte) ' ', (byte) '*', (byte) '/', (byte) ' ' };
051:
052: /*
053: * We're still stuck on JDK-1.4.2, but want the Number.valueOf() methods
054: */
055: private static final Character[] CHARACTER_CACHE = new Character[128];
056:
057: private static final int BYTE_CACHE_OFFSET = 128;
058:
059: private static final Byte[] BYTE_CACHE = new Byte[256];
060:
061: private static final int INTEGER_CACHE_OFFSET = 128;
062:
063: private static final Integer[] INTEGER_CACHE = new Integer[256];
064:
065: private static final int SHORT_CACHE_OFFSET = 128;
066:
067: private static final Short[] SHORT_CACHE = new Short[256];
068:
069: private static final Long[] LONG_CACHE = new Long[256];
070:
071: private static final int LONG_CACHE_OFFSET = 128;
072:
073: static {
074: for (int i = 0; i < CHARACTER_CACHE.length; i++) {
075: CHARACTER_CACHE[i] = new Character((char) i);
076: }
077:
078: for (int i = 0; i < INTEGER_CACHE.length; i++) {
079: INTEGER_CACHE[i] = new Integer(i - 128);
080: }
081:
082: for (int i = 0; i < SHORT_CACHE.length; i++) {
083: SHORT_CACHE[i] = new Short((short) (i - 128));
084: }
085:
086: for (int i = 0; i < LONG_CACHE.length; i++) {
087: LONG_CACHE[i] = new Long(i - 128);
088: }
089:
090: for (int i = 0; i < BYTE_CACHE.length; i++)
091: BYTE_CACHE[i] = new Byte((byte) (i - BYTE_CACHE_OFFSET));
092: }
093:
094: /** Same behavior as JDK-1.5's Constants.characterValueOf(int) */
095:
096: public static Character characterValueOf(char c) {
097: if (c <= 127) {
098: return CHARACTER_CACHE[c];
099: }
100:
101: return new Character(c);
102: }
103:
104: /** Same behavior as JDK-1.5's Byte.valueOf(int) */
105:
106: public static final Byte byteValueOf(byte b) {
107: return BYTE_CACHE[b + BYTE_CACHE_OFFSET];
108: }
109:
110: /** Same behavior as JDK-1.5's Integer.valueOf(int) */
111:
112: public static final Integer integerValueOf(int i) {
113: if (i >= -128 && i <= 127) {
114: return INTEGER_CACHE[i + INTEGER_CACHE_OFFSET];
115: }
116:
117: return new Integer(i);
118: }
119:
120: /** Same behavior as JDK-1.5's Constants.shortValueOf(int) */
121:
122: public static Short shortValueOf(short s) {
123:
124: if (s >= -128 && s <= 127) {
125: return SHORT_CACHE[s + SHORT_CACHE_OFFSET];
126: }
127:
128: return new Short(s);
129: }
130:
131: /** Same behavior as JDK-1.5's Long.valueOf(int) */
132:
133: public static final Long longValueOf(long l) {
134: if (l >= -128 && l <= 127) {
135: return LONG_CACHE[(int) l + LONG_CACHE_OFFSET];
136: }
137:
138: return new Long(l);
139: }
140:
141: /**
142: * Prevents instantiation
143: */
144: private Constants() {
145: }
146: }
|