001: // Copyright (c) 2001, 2003 Per M.A. Bothner and Brainfood Inc.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.lists;
005:
006: /**
007: * A class that encapsulates primitive<->Object conversions.
008: * Applications can override the conversion if desired.
009: * For example charToObject could use an "intern" table.
010: */
011:
012: public class Convert {
013: public static Convert instance = new Convert();
014:
015: public static Convert getInstance() {
016: return instance;
017: }
018:
019: public static void setInstance(Convert value) {
020: instance = value;
021: };
022:
023: public Object booleanToObject(boolean value) {
024: return value ? Boolean.TRUE : Boolean.FALSE;
025: }
026:
027: public boolean objectToBoolean(Object obj) {
028: return !(obj instanceof Boolean)
029: || ((Boolean) obj).booleanValue();
030: }
031:
032: public static Object toObject(boolean value) {
033: return instance.booleanToObject(value);
034: }
035:
036: public static boolean toBoolean(Object obj) {
037: return instance.objectToBoolean(obj);
038: }
039:
040: public Object charToObject(char ch) {
041: return new Character(ch);
042: }
043:
044: public char objectToChar(Object obj) {
045: return ((Character) obj).charValue();
046: }
047:
048: public static Object toObject(char ch) {
049: return instance.charToObject(ch);
050: }
051:
052: public static char toChar(Object obj) {
053: return instance.objectToChar(obj);
054: }
055:
056: public Object byteToObject(byte value) {
057: return new Byte(value);
058: }
059:
060: public byte objectToByte(Object obj) {
061: return ((Number) obj).byteValue();
062: }
063:
064: public static Object toObject(byte value) {
065: return instance.byteToObject(value);
066: }
067:
068: public static byte toByte(Object obj) {
069: return instance.objectToByte(obj);
070: }
071:
072: public Object byteToObjectUnsigned(byte value) {
073: return new Integer(value & 0xFF);
074: }
075:
076: public byte objectToByteUnsigned(Object obj) {
077: return ((Number) obj).byteValue();
078: }
079:
080: public static Object toObjectUnsigned(byte value) {
081: return instance.byteToObjectUnsigned(value);
082: }
083:
084: public static byte toByteUnsigned(Object obj) {
085: return instance.objectToByteUnsigned(obj);
086: }
087:
088: public Object shortToObject(short value) {
089: return new Short(value);
090: }
091:
092: public short objectToShort(Object obj) {
093: return ((Number) obj).shortValue();
094: }
095:
096: public static Object toObject(short value) {
097: return instance.shortToObject(value);
098: }
099:
100: public static short toShort(Object obj) {
101: return instance.objectToShort(obj);
102: }
103:
104: public Object shortToObjectUnsigned(short value) {
105: return new Integer(value & 0xFFFF);
106: }
107:
108: public short objectToShortUnsigned(Object obj) {
109: return ((Number) obj).shortValue();
110: }
111:
112: public static Object toObjectUnsigned(short value) {
113: return instance.shortToObjectUnsigned(value);
114: }
115:
116: public static short toShortUnsigned(Object obj) {
117: return instance.objectToShortUnsigned(obj);
118: }
119:
120: public Object intToObject(int value) {
121: return new Integer(value);
122: }
123:
124: public int objectToInt(Object obj) {
125: return ((Number) obj).intValue();
126: }
127:
128: public static Object toObject(int value) {
129: return instance.intToObject(value);
130: }
131:
132: public static int toInt(Object obj) {
133: return instance.objectToInt(obj);
134: }
135:
136: public Object intToObjectUnsigned(int value) {
137: if (value >= 0)
138: return new Integer(value);
139: else
140: return new Long((long) value & 0xffffffffL);
141: }
142:
143: public int objectToIntUnsigned(Object obj) {
144: return ((Number) obj).intValue();
145: }
146:
147: public static Object toObjectUnsigned(int value) {
148: return instance.intToObjectUnsigned(value);
149: }
150:
151: public static int toIntUnsigned(Object obj) {
152: return instance.objectToIntUnsigned(obj);
153: }
154:
155: public Object longToObject(long value) {
156: return new Long(value);
157: }
158:
159: public long objectToLong(Object obj) {
160: return ((Number) obj).longValue();
161: }
162:
163: public static Object toObject(long value) {
164: return instance.longToObject(value);
165: }
166:
167: public static long toLong(Object obj) {
168: return instance.objectToLong(obj);
169: }
170:
171: public Object longToObjectUnsigned(long value) {
172: return new Long(value); // FIXME use BigInteger?
173: }
174:
175: public long objectToLongUnsigned(Object obj) {
176: return ((Number) obj).longValue();
177: }
178:
179: public static Object toObjectUnsigned(long value) {
180: return instance.longToObjectUnsigned(value);
181: }
182:
183: public static long toLongUnsigned(Object obj) {
184: return instance.objectToLongUnsigned(obj);
185: }
186:
187: public Object floatToObject(float value) {
188: return new Float(value);
189: }
190:
191: public float objectToFloat(Object obj) {
192: return ((Number) obj).floatValue();
193: }
194:
195: public static Object toObject(float value) {
196: return instance.floatToObject(value);
197: }
198:
199: public static float toFloat(Object obj) {
200: return instance.objectToFloat(obj);
201: }
202:
203: public Object doubleToObject(double value) {
204: return new Double(value);
205: }
206:
207: public double objectToDouble(Object obj) {
208: return ((Number) obj).doubleValue();
209: }
210:
211: public static Object toObject(double value) {
212: return instance.doubleToObject(value);
213: }
214:
215: public static double toDouble(Object obj) {
216: return instance.objectToDouble(obj);
217: }
218:
219: public static double parseDouble(String str) {
220: // We assume that if collections are available the Double.parseDouble
221: // is also available.
222: /* #ifdef JAVA2 */
223: return Double.parseDouble(str);
224: /* #endif */
225: /* #ifndef JAVA2 */
226: // return Double.valueOf(str).doubleValue();
227: /* #endif */
228: }
229: }
|