/* Copyright 2006 Wolfgang S. Kechel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.hecl;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * The Operator class implements a number of features
 * that are used in the creation of groups of commands, such as those
 * found in InterpCmds, ListCmds, and so on.
 *
 * @author David N. Welton
 * @version 1.0
 */
public abstract class Operator implements Command {

    /**
     * cmdcode - the int that corresponds to the command
     * to be executed.
     *
     */
    protected int cmdcode;

    /**
     * minargs - the minimum number of arguments this
     * command accepts.
     *
     */
    protected int minargs;

    /**
     * maxargs - the maximum number of arguments this
     * command accepts.  A value of 0 means unlimited arguments.
     *
     */
    protected int maxargs;

    /**
     * Creates a new Operator instance.
     *
     * @param cmdcode an int value corresponding to the
     * command code number found in the class implementing the
     * command.
     * @param minargs an int value - the minimum number
     * of arguments to the command.
     * @param maxargs an int value - the maximum number
     * of arguments to the command, or -1 if unlimited.
     */
    protected Operator(int cmdcode, int minargs, int maxargs) {
	this.cmdcode = cmdcode;
	this.minargs = minargs;
	this.maxargs = maxargs;
    }

    /**
     * The cmdCode method dispatches to the actual code
     * via the operate method and cmdcode argument.
     *
     * @param interp an Interp value
     * @param argv a Thing[] value
     * @return The computed Thing, or null when no value has been
     * computed.
     * @exception HeclException if an error occurs
     */
    @Override
    public Thing cmdCode(Interp interp, Thing[] argv) throws HeclException {
	Interp.checkArgCount(argv,this.minargs,this.maxargs);
	return operate(cmdcode, interp, argv);
    }

    /**
     * The operate method dispatches to the actual code.
     *
     * @param cmdcode an int value
     * @param interp an Interp value
     * @param argv a Thing[] value
     * @return a Thing value, or null when no value has been
     * @exception HeclException if an error occurs
     */
    public abstract Thing operate(int cmdcode,Interp interp,Thing[] argv)
	throws HeclException;


    /**
     * The load method loads the commands in a class that
     * extends Operator.
     *
     * @param ip an Interp value
     * @param cmdtable
     * @exception HeclException if an error occurs
     */
    protected static void load(Interp ip,Hashtable cmdtable) throws HeclException {
	//	System.err.println("-->Operator.load, ip="+ip+", cmdtable="+cmdtable);
	Enumeration e = cmdtable.keys();
	while(e.hasMoreElements()) {
	    String k = (String)e.nextElement();
	    //	    System.err.println("cmd="+k);
	    ip.addCommand(k, (Command)cmdtable.get(k));
	}
	//	System.err.println("<<--Operator.load");
    }


    /**
     * The unload method unloads the commands in a class
     * that extends Operator.
     *
     * @param ip an Interp value
     * @param cmdtable
     * @exception HeclException if an error occurs
     */
    protected static void unload(Interp ip,Hashtable cmdtable) throws HeclException {
	Enumeration e = cmdtable.keys();
	while(e.hasMoreElements()) {
	    ip.removeCommand((String)e.nextElement());
	}
    }
}

 

Ads help maintain this website.