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