Procedure2.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package gnu.mapping;
  2. /* #ifdef use:java.lang.invoke */
  3. import java.lang.invoke.*;
  4. /* #else */
  5. // import gnu.mapping.CallContext.MethodHandle;
  6. /* #endif */
  7. /**
  8. * Abstract class for 2-argument Scheme procedures.
  9. * Extensions must provide apply2.
  10. * @author Per Bothner
  11. */
  12. public abstract class Procedure2 extends Procedure
  13. {
  14. public Procedure2() {
  15. super(false, Procedure2.applyToObject);
  16. }
  17. public Procedure2(String name) {
  18. super(false, Procedure2.applyToObject, name);
  19. }
  20. public int numArgs() { return 0x2002; }
  21. public Object apply0 () throws Throwable
  22. {
  23. throw new WrongArguments(this.getName(), 2, "(?)");
  24. }
  25. public Object apply1 (Object arg1) throws Throwable
  26. {
  27. throw new WrongArguments(this, 1);
  28. }
  29. public abstract Object apply2 (Object arg1,Object arg2) throws Throwable;
  30. public Object apply3 (Object arg1, Object arg2, Object arg3)
  31. {
  32. throw new WrongArguments(this, 3);
  33. }
  34. public Object apply4 (Object arg1, Object arg2, Object arg3, Object arg4)
  35. throws Throwable
  36. {
  37. throw new WrongArguments(this, 4);
  38. }
  39. public Object applyN (Object[] args) throws Throwable
  40. {
  41. if (args.length != 2)
  42. throw new WrongArguments(this, args.length);
  43. return apply2 (args[0], args[1]);
  44. }
  45. public static Object applyToObject(Procedure proc, CallContext ctx)
  46. throws Throwable {
  47. Object arg0 = ctx.getNextArg();
  48. Object arg1 = ctx.getNextArg();
  49. if (ctx.checkDone() == 0)
  50. return proc.apply2(arg0, arg1);
  51. return ctx;
  52. }
  53. public static final MethodHandle applyToObject
  54. = lookupApplyHandle(Procedure2.class, "applyToObject");
  55. }