001: // Copyright (c) 1999 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.mapping;
005:
006: /** Call a specified method in in a ApplyMethodContainer.
007: * We use an extra level of indirection, but we save by having
008: * to create fewer classes than in the one-class-per-procedure
009: * scheme, without having to use (slow) reflection.
010: *
011: * ApplyMethodProc is a generalization of expr.ModuleMethod.
012: */
013:
014: public class ApplyMethodProc extends ProcedureN {
015: ApplyMethodContainer module;
016: public final int selector;
017: private int numArgs;
018:
019: public ApplyMethodProc(ApplyMethodContainer module, int selector,
020: String name, int numArgs) {
021: this .module = module;
022: this .selector = selector;
023: this .numArgs = numArgs;
024: setName(name);
025: }
026:
027: public int numArgs() {
028: return numArgs;
029: }
030:
031: public Object apply0() {
032: return module.apply0(this );
033: }
034:
035: public Object apply1(Object arg1) {
036: return module.apply1(this , arg1);
037: }
038:
039: public Object apply2(Object arg1, Object arg2) {
040: return module.apply2(this , arg1, arg2);
041: }
042:
043: public Object apply3(Object arg1, Object arg2, Object arg3) {
044: return module.apply3(this , arg1, arg2, arg3);
045: }
046:
047: public Object apply4(Object arg1, Object arg2, Object arg3,
048: Object arg4) {
049: return module.apply4(this , arg1, arg2, arg3, arg4);
050: }
051:
052: public Object applyN(Object[] args) {
053: return module.applyN(this , args);
054: }
055:
056: /** Helper methods for default ApplyMethodContainer actions. */
057:
058: public static Object apply0Default(ApplyMethodProc method) {
059: return method.module.applyN(method, Values.noArgs);
060: }
061:
062: public static Object apply1Default(ApplyMethodProc method,
063: Object arg1) {
064: Object[] args = new Object[1];
065: args[0] = arg1;
066: return method.module.applyN(method, args);
067: }
068:
069: public static Object apply2Default(ApplyMethodProc method,
070: Object arg1, Object arg2) {
071: Object[] args = new Object[2];
072: args[0] = arg1;
073: args[1] = arg2;
074: return method.module.applyN(method, args);
075: }
076:
077: public static Object apply3Default(ApplyMethodProc method,
078: Object arg1, Object arg2, Object arg3) {
079: Object[] args = new Object[3];
080: args[0] = arg1;
081: args[1] = arg2;
082: args[2] = arg3;
083: return method.module.applyN(method, args);
084: }
085:
086: public static Object apply4Default(ApplyMethodProc method,
087: Object arg1, Object arg2, Object arg3, Object arg4) {
088: Object[] args = new Object[4];
089: args[0] = arg1;
090: args[1] = arg2;
091: args[2] = arg3;
092: args[3] = arg4;
093: return method.module.applyN(method, args);
094: }
095:
096: public static Object applyNDefault(ApplyMethodProc method,
097: Object[] args) {
098: int count = args.length;
099: int num = method.numArgs();
100: ApplyMethodContainer module = method.module;
101: if (count >= (num & 0xFFF) && (num < 0 || count <= (num >> 12))) {
102: switch (count) {
103: case 0:
104: return module.apply0(method);
105: case 1:
106: return module.apply1(method, args[0]);
107: case 2:
108: return module.apply2(method, args[0], args[1]);
109: case 3:
110: return module.apply3(method, args[0], args[1], args[2]);
111: case 4:
112: return module.apply4(method, args[0], args[1], args[2],
113: args[3]);
114: }
115: }
116: throw new WrongArguments(method, count);
117: }
118: }
|