001: /* Copyright 2006 David N. Welton
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:
016: package org.hecl;
017:
018: import java.util.Enumeration;
019: import java.util.Hashtable;
020: import java.util.Vector;
021:
022: /**
023: * The <code>HashCmds</code> class takes care of loading and
024: * implementing the Hecl commands that deal with hash tables, which
025: * are in turn implemented in the HashThing class.
026: *
027: * @author <a href="mailto:davidw@dedasys.com">David N. Welton</a>
028: * @version 1.0
029: */
030: class HashCmds extends Operator {
031: public static final int HASH = 0;
032: public static final int HGET = 1;
033: public static final int HSET = 2;
034: public static final int HKEYS = 3;
035: public static final int HCLEAR = 4;
036: public static final int HREMOVE = 5;
037: public static final int HCONTAINS = 6;
038:
039: public Thing operate(int cmd, Interp interp, Thing[] argv)
040: throws HeclException {
041: Hashtable hash = cmd != 0 ? HashThing.get(argv[1]) : null;
042: Thing result = null;
043:
044: switch (cmd) {
045: case HASH:
046: result = HashThing.create(HashThing.get(argv[1]));
047: break;
048:
049: case HGET:
050: result = (Thing) hash.get(argv[2].toString());
051: break;
052:
053: case HSET:
054: result = argv[3];
055: hash.put(argv[2].toString(), result);
056: break;
057:
058: case HKEYS:
059: Vector v = new Vector(hash.size());
060: Enumeration e = hash.keys();
061: while (e.hasMoreElements()) {
062: v.addElement(new Thing((String) e.nextElement()));
063: }
064: return ListThing.create(v);
065:
066: case HCLEAR:
067: hash.clear();
068: result = argv[1];
069: break;
070:
071: case HREMOVE:
072: Object o = hash.remove(argv[2].toString());
073: result = o != null ? (Thing) o : new Thing("");
074: break;
075:
076: case HCONTAINS:
077: result = new Thing(
078: hash.containsKey(argv[2].toString()) ? IntThing.ONE
079: : IntThing.ZERO);
080: break;
081:
082: default:
083: throw new HeclException("Unknown hash command '"
084: + argv[0].toString() + "' with code '" + cmd + "'.");
085: }
086: return result;
087: }
088:
089: public static void load(Interp ip) throws HeclException {
090: Operator.load(ip, cmdtable);
091: }
092:
093: public static void unload(Interp ip) throws HeclException {
094: Operator.unload(ip, cmdtable);
095: }
096:
097: protected HashCmds(int cmdcode, int minargs, int maxargs) {
098: super (cmdcode, minargs, maxargs);
099: }
100:
101: private static Hashtable cmdtable = new Hashtable();
102:
103: static {
104: cmdtable.put("hash", new HashCmds(HASH, 1, 1));
105: cmdtable.put("hget", new HashCmds(HGET, 2, 2));
106: cmdtable.put("hset", new HashCmds(HSET, 3, 3));
107: cmdtable.put("hkeys", new HashCmds(HKEYS, 1, 1));
108: cmdtable.put("hclear", new HashCmds(HCLEAR, 1, 1));
109: cmdtable.put("hremove", new HashCmds(HREMOVE, 2, 2));
110: cmdtable.put("hcontains", new HashCmds(HCONTAINS, 2, 2));
111: }
112: }
|