001: /* Copyright 2006 Wolfgang S. Kechel
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015: package org.hecl;
016:
017: import java.util.Enumeration;
018: import java.util.Hashtable;
019:
020: /**
021: * The <code>Operator</code> class implements a number of features
022: * that are used in the creation of groups of commands, such as those
023: * found in InterpCmds, ListCmds, and so on.
024: *
025: * @author <a href="mailto:davidw@dedasys.com">David N. Welton</a>
026: * @version 1.0
027: */
028: public abstract class Operator implements Command {
029:
030: /**
031: * <code>cmdcode</code> - the int that corresponds to the command
032: * to be executed.
033: *
034: */
035: protected int cmdcode;
036:
037: /**
038: * <code>minargs</code> - the minimum number of arguments this
039: * command accepts.
040: *
041: */
042: protected int minargs;
043:
044: /**
045: * <code>maxargs</code> - the maximum number of arguments this
046: * command accepts. A value of 0 means unlimited arguments.
047: *
048: */
049: protected int maxargs;
050:
051: /**
052: * Creates a new <code>Operator</code> instance.
053: *
054: * @param cmdcode an <code>int</code> value corresponding to the
055: * command code number found in the class implementing the
056: * command.
057: * @param minargs an <code>int</code> value - the minimum number
058: * of arguments to the command.
059: * @param maxargs an <code>int</code> value - the maximum number
060: * of arguments to the command, or -1 if unlimited.
061: */
062: protected Operator(int cmdcode, int minargs, int maxargs) {
063: this .cmdcode = cmdcode;
064: this .minargs = minargs;
065: this .maxargs = maxargs;
066: }
067:
068: /**
069: * The <code>cmdCode</code> method dispatches to the actual code
070: * via the operate method and cmdcode argument.
071: *
072: * @param interp an <code>Interp</code> value
073: * @param argv a <code>Thing[]</code> value
074: * @return The computed <code>Thing</code>, or null when no value has been
075: * computed.
076: * @exception HeclException if an error occurs
077: */
078: public Thing cmdCode(Interp interp, Thing[] argv)
079: throws HeclException {
080: Interp.checkArgCount(argv, this .minargs, this .maxargs);
081: return operate(cmdcode, interp, argv);
082: }
083:
084: /**
085: * The <code>operate</code> method dispatches to the actual code.
086: *
087: * @param cmdcode an <code>int</code> value
088: * @param interp an <code>Interp</code> value
089: * @param argv a <code>Thing[]</code> value
090: * @return a <code>Thing</code> value, or null when no value has been
091: * @exception HeclException if an error occurs
092: */
093: public abstract Thing operate(int cmdcode, Interp interp,
094: Thing[] argv) throws HeclException;
095:
096: /**
097: * The <code>load</code> method loads the commands in a class that
098: * extends Operator.
099: *
100: * @param ip an <code>Interp</code> value
101: * @exception HeclException if an error occurs
102: */
103: protected static void load(Interp ip, Hashtable cmdtable)
104: throws HeclException {
105: // System.err.println("-->Operator.load, ip="+ip+", cmdtable="+cmdtable);
106: Enumeration e = cmdtable.keys();
107: while (e.hasMoreElements()) {
108: String k = (String) e.nextElement();
109: // System.err.println("cmd="+k);
110: ip.addCommand(k, (Command) cmdtable.get(k));
111: }
112: // System.err.println("<<--Operator.load");
113: }
114:
115: /**
116: * The <code>unload</code> method unloads the commands in a class
117: * that extends Operator.
118: *
119: * @param ip an <code>Interp</code> value
120: * @exception HeclException if an error occurs
121: */
122: protected static void unload(Interp ip, Hashtable cmdtable)
123: throws HeclException {
124: Enumeration e = cmdtable.keys();
125: while (e.hasMoreElements()) {
126: ip.removeCommand((String) e.nextElement());
127: }
128: }
129: }
|