Convert.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright (c) 2001, 2003 Per M.A. Bothner and Brainfood Inc.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.lists;
  4. /**
  5. * A class that encapsulates conversions between primitive and Object.
  6. * Applications can override the conversion if desired.
  7. * For example charToObject could use an "intern" table.
  8. */
  9. public class Convert
  10. {
  11. public static Convert instance = new Convert();
  12. public static Convert getInstance() { return instance; }
  13. public static void setInstance(Convert value) { instance = value; };
  14. public Object booleanToObject(boolean value)
  15. {
  16. return value ? Boolean.TRUE : Boolean.FALSE;
  17. }
  18. public boolean objectToBoolean(Object obj)
  19. {
  20. return ! (obj instanceof Boolean) || ((Boolean) obj).booleanValue();
  21. }
  22. public static Object toObject(boolean value)
  23. {
  24. return instance.booleanToObject(value);
  25. }
  26. public static boolean toBoolean(Object obj)
  27. {
  28. return instance.objectToBoolean(obj);
  29. }
  30. public Object charToObject(char ch)
  31. {
  32. return new Character(ch);
  33. }
  34. public char objectToChar(Object obj)
  35. {
  36. return ((Character) obj).charValue();
  37. }
  38. public static Object toObject(char ch)
  39. {
  40. return instance.charToObject(ch);
  41. }
  42. public static char toChar(Object obj)
  43. {
  44. return instance.objectToChar(obj);
  45. }
  46. public Object byteToObject(byte value)
  47. {
  48. return new Byte(value);
  49. }
  50. public byte objectToByte(Object obj)
  51. {
  52. return ((Number) obj).byteValue();
  53. }
  54. public static Object toObject(byte value)
  55. {
  56. return instance.byteToObject(value);
  57. }
  58. public static byte toByte(Object obj)
  59. {
  60. return instance.objectToByte(obj);
  61. }
  62. public Object byteToObjectUnsigned(byte value)
  63. {
  64. return new Integer(value & 0xFF);
  65. }
  66. public byte objectToByteUnsigned(Object obj)
  67. {
  68. return ((Number) obj).byteValue();
  69. }
  70. public static Object toObjectUnsigned(byte value)
  71. {
  72. return instance.byteToObjectUnsigned(value);
  73. }
  74. public static byte toByteUnsigned(Object obj)
  75. {
  76. return instance.objectToByteUnsigned(obj);
  77. }
  78. public Object shortToObject(short value)
  79. {
  80. return new Short(value);
  81. }
  82. public short objectToShort(Object obj)
  83. {
  84. return ((Number) obj).shortValue();
  85. }
  86. public static Object toObject(short value)
  87. {
  88. return instance.shortToObject(value);
  89. }
  90. public static short toShort(Object obj)
  91. {
  92. return instance.objectToShort(obj);
  93. }
  94. public Object shortToObjectUnsigned(short value)
  95. {
  96. return new Integer(value & 0xFFFF);
  97. }
  98. public short objectToShortUnsigned(Object obj)
  99. {
  100. return ((Number) obj).shortValue();
  101. }
  102. public static Object toObjectUnsigned(short value)
  103. {
  104. return instance.shortToObjectUnsigned(value);
  105. }
  106. public static short toShortUnsigned(Object obj)
  107. {
  108. return instance.objectToShortUnsigned(obj);
  109. }
  110. public Object intToObject(int value)
  111. {
  112. return new Integer(value);
  113. }
  114. public int objectToInt(Object obj)
  115. {
  116. return ((Number) obj).intValue();
  117. }
  118. public static Object toObject(int value)
  119. {
  120. return instance.intToObject(value);
  121. }
  122. public static int toInt(Object obj)
  123. {
  124. return instance.objectToInt(obj);
  125. }
  126. public Object intToObjectUnsigned(int value)
  127. {
  128. if (value >= 0)
  129. return new Integer(value);
  130. else
  131. return new Long((long) value & 0xffffffffL);
  132. }
  133. public int objectToIntUnsigned(Object obj)
  134. {
  135. return ((Number) obj).intValue();
  136. }
  137. public static Object toObjectUnsigned(int value)
  138. {
  139. return instance.intToObjectUnsigned(value);
  140. }
  141. public static int toIntUnsigned(Object obj)
  142. {
  143. return instance.objectToIntUnsigned(obj);
  144. }
  145. public Object longToObject(long value)
  146. {
  147. return new Long(value);
  148. }
  149. public long objectToLong(Object obj)
  150. {
  151. return ((Number) obj).longValue();
  152. }
  153. public static Object toObject(long value)
  154. {
  155. return instance.longToObject(value);
  156. }
  157. public static long toLong(Object obj)
  158. {
  159. return instance.objectToLong(obj);
  160. }
  161. public Object longToObjectUnsigned(long value)
  162. {
  163. return new Long(value); // FIXME use BigInteger?
  164. }
  165. public long objectToLongUnsigned(Object obj)
  166. {
  167. return ((Number) obj).longValue();
  168. }
  169. public static Object toObjectUnsigned(long value)
  170. {
  171. return instance.longToObjectUnsigned(value);
  172. }
  173. public static long toLongUnsigned(Object obj)
  174. {
  175. return instance.objectToLongUnsigned(obj);
  176. }
  177. public Object floatToObject(float value)
  178. {
  179. return new Float(value);
  180. }
  181. public float objectToFloat(Object obj)
  182. {
  183. return ((Number) obj).floatValue();
  184. }
  185. public static Object toObject(float value)
  186. {
  187. return instance.floatToObject(value);
  188. }
  189. public static float toFloat(Object obj)
  190. {
  191. return instance.objectToFloat(obj);
  192. }
  193. public Object doubleToObject(double value)
  194. {
  195. return new Double(value);
  196. }
  197. public double objectToDouble(Object obj)
  198. {
  199. return ((Number) obj).doubleValue();
  200. }
  201. public static Object toObject(double value)
  202. {
  203. return instance.doubleToObject(value);
  204. }
  205. public static double toDouble(Object obj)
  206. {
  207. return instance.objectToDouble(obj);
  208. }
  209. public static double parseDouble(String str)
  210. {
  211. // We assume that if collections are available the Double.parseDouble
  212. // is also available.
  213. /* #ifdef JAVA2 */
  214. return Double.parseDouble(str);
  215. /* #endif */
  216. /* #ifndef JAVA2 */
  217. // return Double.valueOf(str).doubleValue();
  218. /* #endif */
  219. }
  220. }