01: package org.jicengine.operation;
02:
03: import java.util.List;
04: import java.util.ArrayList;
05:
06: /**
07: * <p>
08: * Either a method or constructor invocation.
09: * </p>
10: * <p>
11: * Copyright (C) 2004 Timo Laitinen
12: * </p>
13: * @author .timo
14: */
15:
16: public abstract class InvocationOperation implements Operation {
17:
18: Operation actor;
19: Operation[] parameters;
20:
21: String signature;
22:
23: public InvocationOperation(String signature, Operation actor,
24: Operation[] parameters) {
25: this .actor = actor;
26: this .parameters = parameters;
27: this .signature = signature;
28: }
29:
30: public boolean needsParameters() {
31: return this .parameters.length > 0;
32: }
33:
34: public boolean needsParameter(String name) {
35: if (this .actor.needsParameter(name)) {
36: return true;
37: }
38:
39: for (int i = 0; i < this .parameters.length; i++) {
40: if (this .parameters[i].needsParameter(name)) {
41: return true;
42: }
43: }
44:
45: return false;
46: }
47:
48: public Object execute(Context context) throws OperationException {
49: Object actorObject = this .actor.execute(context);
50: Object[] arguments = evaluateParameters(this .parameters,
51: context);
52: return execute(actorObject, arguments);
53: }
54:
55: public String toString() {
56: return this .signature;
57: }
58:
59: protected abstract Object execute(Object actor, Object[] arguments)
60: throws OperationException;
61:
62: public static Object[] evaluateParameters(Operation[] parameters,
63: Context context) throws OperationException {
64: Object[] params = new Object[parameters.length];
65: for (int i = 0; i < parameters.length; i++) {
66: params[i] = parameters[i].execute(context);
67: }
68: return params;
69: }
70:
71: }
|