PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/orc/values/sites/compatibility/Args.java

https://github.com/laurenyew/cOrcS
Java | 321 lines | 186 code | 32 blank | 103 comment | 75 complexity | ebf2744dd5fc23f0e4ef61fadeffc029 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. //
  2. // Args.java -- Java class Args
  3. // Project OrcScala
  4. //
  5. // $Id: Args.java 2280 2010-12-19 16:47:03Z jthywissen $
  6. //
  7. // Copyright (c) 2009 The University of Texas at Austin. All rights reserved.
  8. //
  9. // Use and redistribution of this file is governed by the license terms in
  10. // the LICENSE file found in the project's top-level directory and also found at
  11. // URL: http://orc.csres.utexas.edu/license.shtml .
  12. //
  13. package orc.values.sites.compatibility;
  14. import java.io.Serializable;
  15. import java.math.BigDecimal;
  16. import java.math.BigInteger;
  17. import java.util.List;
  18. import orc.error.runtime.ArgumentTypeMismatchException;
  19. import orc.error.runtime.ArityMismatchException;
  20. import orc.error.runtime.InsufficientArgsException;
  21. import orc.error.runtime.JavaException;
  22. import orc.values.Field;
  23. /**
  24. * Container for arguments to a site.
  25. *
  26. * @author dkitchin
  27. */
  28. @SuppressWarnings("serial")
  29. public class Args implements Serializable {
  30. Object[] values;
  31. public Args(@SuppressWarnings("hiding") final List<Object> values) {
  32. this.values = new Object[values.size()];
  33. this.values = values.toArray(this.values);
  34. }
  35. /**
  36. * @return number of arguments
  37. */
  38. public int size() {
  39. return values.length;
  40. }
  41. /**
  42. * Helper function to, assuming the argument is an Orc field, retrieve the
  43. * field's name
  44. *
  45. * @return
  46. * @throws ArityMismatchException
  47. * @throws ArgumentTypeMismatchException
  48. */
  49. public String fieldName() throws ArityMismatchException, ArgumentTypeMismatchException {
  50. if (values.length != 1) {
  51. throw new ArityMismatchException(1, values.length);
  52. }
  53. final Object v = values[0];
  54. if (v == null) {
  55. throw new ArgumentTypeMismatchException(0, "message", "null");
  56. }
  57. if (v instanceof Field) {
  58. return ((Field) v).field();
  59. } else {
  60. throw new ArgumentTypeMismatchException(0, "message", (v != null ? v.getClass().toString() : "null"));
  61. }
  62. }
  63. /**
  64. * Helper function to retrieve the nth element as an object (starting from
  65. * 0), with error checking
  66. *
  67. * @param n
  68. * @return
  69. * @throws InsufficientArgsException
  70. */
  71. public Object getArg(final int n) throws InsufficientArgsException {
  72. try {
  73. return values[n];
  74. } catch (final ArrayIndexOutOfBoundsException e) {
  75. throw new InsufficientArgsException(n, values.length);
  76. }
  77. }
  78. /**
  79. * Return the entire tuple as an object array. Please don't mutate the
  80. * array.
  81. *
  82. * @return
  83. */
  84. public Object[] asArray() {
  85. return values;
  86. }
  87. /**
  88. * Helper function for integers
  89. *
  90. * @param n
  91. * @return
  92. * @throws ArgumentTypeMismatchException
  93. * @throws InsufficientArgsException
  94. */
  95. public int intArg(final int n) throws ArgumentTypeMismatchException, InsufficientArgsException {
  96. final Object a = getArg(n);
  97. if (a == null) {
  98. throw new ArgumentTypeMismatchException(n, "int", "null");
  99. }
  100. try {
  101. return ((Number) a).intValue();
  102. } catch (final ClassCastException e) {
  103. throw new ArgumentTypeMismatchException(n, "int", (a != null ? a.getClass().toString() : "null"));
  104. }
  105. }
  106. /**
  107. * Helper function for longs
  108. *
  109. * @param n
  110. * @return
  111. * @throws ArgumentTypeMismatchException
  112. * @throws InsufficientArgsException
  113. */
  114. public long longArg(final int n) throws ArgumentTypeMismatchException, InsufficientArgsException {
  115. final Object a = getArg(n);
  116. if (a == null) {
  117. throw new ArgumentTypeMismatchException(n, "long", "null");
  118. }
  119. try {
  120. return ((Number) a).longValue();
  121. } catch (final ClassCastException e) {
  122. throw new ArgumentTypeMismatchException(n, "long", (a != null ? a.getClass().toString() : "null"));
  123. }
  124. }
  125. /**
  126. * @param n
  127. * @return
  128. * @throws ArgumentTypeMismatchException
  129. * @throws InsufficientArgsException
  130. */
  131. public Number numberArg(final int n) throws ArgumentTypeMismatchException, InsufficientArgsException {
  132. final Object a = getArg(n);
  133. if (a == null) {
  134. throw new ArgumentTypeMismatchException(n, "Number", "null");
  135. }
  136. try {
  137. return (Number) a;
  138. } catch (final ClassCastException e) {
  139. throw new ArgumentTypeMismatchException(n, "Number", (a != null ? a.getClass().toString() : "null"));
  140. }
  141. }
  142. /**
  143. * Helper function for booleans
  144. *
  145. * @param n
  146. * @return
  147. * @throws ArgumentTypeMismatchException
  148. * @throws InsufficientArgsException
  149. */
  150. public boolean boolArg(final int n) throws ArgumentTypeMismatchException, InsufficientArgsException {
  151. final Object a = getArg(n);
  152. if (a == null) {
  153. throw new ArgumentTypeMismatchException(n, "boolean", "null");
  154. }
  155. try {
  156. return ((Boolean) a).booleanValue();
  157. } catch (final ClassCastException e) {
  158. throw new ArgumentTypeMismatchException(n, "boolean", (a != null ? a.getClass().toString() : "null"));
  159. }
  160. }
  161. /**
  162. * Helper function for strings. Note that this requires a strict String
  163. * type. If you don't care whether the argument is really a string, use
  164. * valArg(n).toString().
  165. *
  166. * @param n
  167. * @return
  168. * @throws ArgumentTypeMismatchException
  169. * @throws InsufficientArgsException
  170. */
  171. public String stringArg(final int n) throws ArgumentTypeMismatchException, InsufficientArgsException {
  172. final Object a = getArg(n);
  173. if (a == null) {
  174. throw new ArgumentTypeMismatchException(n, "String", "null");
  175. }
  176. try {
  177. return (String) a;
  178. } catch (final ClassCastException e) {
  179. throw new ArgumentTypeMismatchException(n, "String", (a != null ? a.getClass().toString() : "null"));
  180. }
  181. }
  182. /** A unary operator on numbers */
  183. public interface NumericUnaryOperator<T> {
  184. public T apply(BigInteger a);
  185. public T apply(BigDecimal a);
  186. public T apply(int a);
  187. public T apply(long a);
  188. public T apply(byte a);
  189. public T apply(short a);
  190. public T apply(double a);
  191. public T apply(float a);
  192. }
  193. /** A binary operator on numbers */
  194. public interface NumericBinaryOperator<T> {
  195. public T apply(BigInteger a, BigInteger b);
  196. public T apply(BigDecimal a, BigDecimal b);
  197. public T apply(int a, int b);
  198. public T apply(long a, long b);
  199. public T apply(byte a, byte b);
  200. public T apply(short a, short b);
  201. public T apply(double a, double b);
  202. public T apply(float a, float b);
  203. }
  204. /**
  205. * Dispatch a binary operator based on the widest type of two numbers.
  206. *
  207. * @param <T>
  208. * @param a
  209. * @param b
  210. * @param op
  211. * @return
  212. * @throws JavaException
  213. */
  214. public static <T> T applyNumericOperator(final Number a, final Number b, final NumericBinaryOperator<T> op) throws JavaException {
  215. try {
  216. if (a instanceof BigDecimal) {
  217. if (b instanceof BigDecimal) {
  218. return op.apply((BigDecimal) a, (BigDecimal) b);
  219. } else {
  220. return op.apply((BigDecimal) a, BigDecimal.valueOf(b.doubleValue()));
  221. }
  222. } else if (b instanceof BigDecimal) {
  223. if (a instanceof BigDecimal) {
  224. return op.apply((BigDecimal) a, (BigDecimal) b);
  225. } else {
  226. return op.apply(BigDecimal.valueOf(a.doubleValue()), (BigDecimal) b);
  227. }
  228. } else if (a instanceof Double || b instanceof Double) {
  229. return op.apply(a.doubleValue(), b.doubleValue());
  230. } else if (a instanceof Float || b instanceof Float) {
  231. return op.apply(a.floatValue(), b.floatValue());
  232. } else if (a instanceof BigInteger) {
  233. if (b instanceof BigInteger) {
  234. return op.apply((BigInteger) a, (BigInteger) b);
  235. } else {
  236. return op.apply((BigInteger) a, BigInteger.valueOf(b.longValue()));
  237. }
  238. } else if (b instanceof BigInteger) {
  239. if (a instanceof BigInteger) {
  240. return op.apply((BigInteger) a, (BigInteger) b);
  241. } else {
  242. return op.apply(BigInteger.valueOf(a.longValue()), (BigInteger) b);
  243. }
  244. } else if (a instanceof Long || b instanceof Long) {
  245. return op.apply(a.longValue(), b.longValue());
  246. } else if (a instanceof Integer || b instanceof Integer) {
  247. return op.apply(a.intValue(), b.intValue());
  248. } else if (a instanceof Short || b instanceof Short) {
  249. return op.apply(a.shortValue(), b.shortValue());
  250. } else if (a instanceof Byte || b instanceof Byte) {
  251. return op.apply(a.byteValue(), b.byteValue());
  252. } else {
  253. throw new JavaException(new IllegalArgumentException("Unexpected Number type in (" + (a != null ? a.getClass().toString() : "null") + ", " + (b != null ? b.getClass().toString() : "null") + ")"));
  254. }
  255. } catch (final ArithmeticException e) {
  256. throw new JavaException(e);
  257. }
  258. }
  259. /**
  260. * Dispatch a unary operator based on the type of a number.
  261. *
  262. * @param <T>
  263. * @param a
  264. * @param op
  265. * @return
  266. * @throws JavaException
  267. */
  268. public static <T> T applyNumericOperator(final Number a, final NumericUnaryOperator<T> op) throws JavaException {
  269. if (a instanceof BigDecimal) {
  270. return op.apply((BigDecimal) a);
  271. } else if (a instanceof Double) {
  272. return op.apply(a.doubleValue());
  273. } else if (a instanceof Float) {
  274. return op.apply(a.floatValue());
  275. } else if (a instanceof BigInteger) {
  276. return op.apply((BigInteger) a);
  277. } else if (a instanceof Long) {
  278. return op.apply(a.longValue());
  279. } else if (a instanceof Integer) {
  280. return op.apply(a.intValue());
  281. } else if (a instanceof Short) {
  282. return op.apply(a.shortValue());
  283. } else if (a instanceof Byte) {
  284. return op.apply(a.byteValue());
  285. } else {
  286. throw new JavaException(new IllegalArgumentException("Unexpected Number type in (" + (a != null ? a.getClass().toString() : "null") + ")"));
  287. }
  288. }
  289. }