PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sys/java/fanx/interop/Interop.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 265 lines | 111 code | 32 blank | 122 comment | 2 complexity | 0631b6fbaa7865376dbce8e6db8b01d8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2009, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 13 May 09 Brian Frank Creation
  7. //
  8. package fanx.interop;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.util.Enumeration;
  12. import java.util.HashMap;
  13. import java.util.Iterator;
  14. import fan.sys.*;
  15. import fanx.util.*;
  16. /**
  17. * Interop defines for converting between Fantom and Java for common types.
  18. */
  19. public class Interop
  20. {
  21. //////////////////////////////////////////////////////////////////////////
  22. // Reflection
  23. //////////////////////////////////////////////////////////////////////////
  24. /**
  25. * Get the java Class of the given object.
  26. */
  27. public static Class getClass(Object obj)
  28. {
  29. return obj.getClass();
  30. }
  31. /**
  32. * Convert java.lang.Class to sys::Type.
  33. */
  34. public static Type toFan(Class cls)
  35. {
  36. return FanUtil.toFanType(cls, true);
  37. }
  38. /**
  39. * Convert sys::Type to java.lang.Class. You
  40. * can also access this functionality using the
  41. * trap operator "->toClass" on Type.
  42. */
  43. public static Class toJava(Type type)
  44. {
  45. return type.toClass();
  46. }
  47. //////////////////////////////////////////////////////////////////////////
  48. // Exceptions
  49. //////////////////////////////////////////////////////////////////////////
  50. /**
  51. * Given a Java exception instance translate to a Fantom exception.
  52. * If the exception maps to a built-in Fantom exception then the
  53. * native Fantom type is used - for example NullPointerException will
  54. * return a NullErr. Otherwise the Java exception is wrapped
  55. * as a generic Err instance.
  56. */
  57. public static Err toFan(Throwable ex)
  58. {
  59. return Err.make(ex);
  60. }
  61. /**
  62. * Given a Fantom exception instance, get the underlying Java exception.
  63. */
  64. public static Throwable toJava(Err err)
  65. {
  66. return err.toJava();
  67. }
  68. //////////////////////////////////////////////////////////////////////////
  69. // IO
  70. //////////////////////////////////////////////////////////////////////////
  71. /**
  72. * Convert from java.io.InputStream to sys::InStream
  73. * with default buffer size of 4096.
  74. */
  75. public static InStream toFan(InputStream in)
  76. {
  77. return SysInStream.make(in, FanInt.Chunk);
  78. }
  79. /**
  80. * Convert from java.io.InputStream to sys::InStream
  81. * with the given buffer size.
  82. */
  83. public static InStream toFan(InputStream in, long bufSize)
  84. {
  85. return SysInStream.make(in, bufSize);
  86. }
  87. /**
  88. * Convert from java.io.OutputStream to sys::OutStream
  89. * with default buffer size of 4096.
  90. */
  91. public static OutStream toFan(OutputStream out)
  92. {
  93. return SysOutStream.make(out, FanInt.Chunk);
  94. }
  95. /**
  96. * Convert from java.io.OutputStream to sys::OutStream
  97. * with the given buffer size.
  98. */
  99. public static OutStream toFan(OutputStream out, long bufSize)
  100. {
  101. return SysOutStream.make(out, bufSize);
  102. }
  103. /**
  104. * Convert from java.io.File to sys::File.
  105. */
  106. public static File toFan(java.io.File file)
  107. {
  108. return new LocalFile(file);
  109. }
  110. /**
  111. * Convert from java.nio.ByteBuffer to sys::Buf.
  112. */
  113. public static Buf toFan(java.nio.ByteBuffer buf)
  114. {
  115. return new NioBuf(buf);
  116. }
  117. /**
  118. * Convert from sys::Buf to to java.nio.ByteBuffer
  119. *
  120. * Return a bytebuffer that shares the same storage
  121. * as this byte buffer, but independent pos and size.
  122. *
  123. * Changes made to the resulting ByteBuffer will affect
  124. * this ByteBuffer's content but not its pos or size.
  125. *
  126. * The ByteBuffer will be created with position equal
  127. * to this buf's pos and limit equal to this buf's limit,
  128. * and no mark set.
  129. *
  130. * Throws UnsupportedErr if this Buf doesn't use an
  131. ** underlying storage system supported by ByteBuffer.
  132. *
  133. * @see java.nio.ByteBuffer#duplicate
  134. * @see java.nio.ByteBuffer#wrap(byte[], int, int)
  135. */
  136. public static java.nio.ByteBuffer toJava(Buf buf)
  137. {
  138. return buf.toByteBuffer();
  139. }
  140. /**
  141. * Convert from sys::InStream to java.io.InputStream.
  142. */
  143. public static InputStream toJava(InStream in)
  144. {
  145. return SysInStream.java(in);
  146. }
  147. /**
  148. * Convert from sys::OutStream to java.io.OutputStream.
  149. */
  150. public static OutputStream toJava(OutStream out)
  151. {
  152. return SysOutStream.java(out);
  153. }
  154. /**
  155. * Convert from sys::File to java.io.File. Raise
  156. * cast exception if not a local file.
  157. */
  158. public static java.io.File toJava(File file)
  159. {
  160. return ((LocalFile)file).toJava();
  161. }
  162. //////////////////////////////////////////////////////////////////////////
  163. // Collections
  164. //////////////////////////////////////////////////////////////////////////
  165. /**
  166. * Convert a java.util.List to a sys::List with a type of Obj?[].
  167. */
  168. public static List toFan(java.util.List list)
  169. {
  170. return toFan(list.iterator(), Sys.ObjType.toNullable());
  171. }
  172. /**
  173. * Convert a java.util.List to a sys::List of the specified type.
  174. */
  175. public static List toFan(java.util.List list, Type of)
  176. {
  177. return toFan(list.iterator(), of);
  178. }
  179. /**
  180. * Convert a java.util.Enumeration to a sys::List with a type of Obj?[].
  181. */
  182. public static List toFan(Enumeration e)
  183. {
  184. return toFan(e, Sys.ObjType.toNullable());
  185. }
  186. /**
  187. * Convert a java.util.Enumeration to a sys::List of the specified type.
  188. */
  189. public static List toFan(Enumeration e, Type of)
  190. {
  191. List list = new List(of);
  192. while (e.hasMoreElements()) list.add(e.nextElement());
  193. return list;
  194. }
  195. /**
  196. * Convert a java.util.Iterator to a sys::List with a type of Obj?[].
  197. */
  198. public static List toFan(Iterator i)
  199. {
  200. return toFan(i, Sys.ObjType.toNullable());
  201. }
  202. /**
  203. * Convert a java.util.Iterator to a sys::List of the specified type.
  204. */
  205. public static List toFan(Iterator i, Type of)
  206. {
  207. List list = new List(of);
  208. while (i.hasNext()) list.add(i.next());
  209. return list;
  210. }
  211. /**
  212. * Convert a java.util.HashMap to a sys::Map with a type of Obj:Obj?.
  213. */
  214. public static Map toFan(HashMap map)
  215. {
  216. return new Map(new MapType(Sys.ObjType, Sys.ObjType.toNullable()), map);
  217. }
  218. /**
  219. * Convert a java.util.HashMap to a sys::Map with the specified map type.
  220. */
  221. public static Map toFan(HashMap map, Type type)
  222. {
  223. return new Map((MapType)type, map);
  224. }
  225. /**
  226. * Convert a sys::Map to a java.util.HashMap. If the fan
  227. ** map is not read/write, then ReadonlyErr is thrown.
  228. */
  229. public static HashMap toJava(Map map)
  230. {
  231. return map.toJava();
  232. }
  233. }