001: /*
002: * JHandler.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: JHandler.java,v 1.1 2003/11/15 11:03:31 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: import java.util.HashMap;
025: import java.util.Map;
026: import java.util.WeakHashMap;
027:
028: public final class JHandler extends Lisp {
029: private static final Map table = new WeakHashMap();
030:
031: public static void callLisp(String s, Object o) {
032: callLisp(s, o, "");
033: }
034:
035: public static void callLisp(String s, Object o, String s1) {
036: callLisp(s, o, s1, new int[] {});
037: }
038:
039: public static void callLisp(String s, Object o, String s1, int ai[]) {
040: callLisp(s, o, new String[] { s1 }, ai);
041: }
042:
043: public static void callLisp(String s, Object o, String as[]) {
044: callLisp(s, o, as, new int[] {});
045: }
046:
047: public static void callLisp(String s, Object o, String as[],
048: int ai[]) {
049: if (table.containsKey(o)) {
050: Map entryTable = (Map) table.get(o);
051: if (entryTable.containsKey(s)) {
052: Function f = ((Entry) entryTable.get(s)).getHandler();
053: LispObject data = ((Entry) entryTable.get(s)).getData();
054: Fixnum count = ((Entry) entryTable.get(s)).getCount();
055: Fixnum[] lispAi = new Fixnum[ai.length];
056: for (int i = 0; i < ai.length; i++) {
057: lispAi[i] = new Fixnum(ai[i]);
058: }
059: LispObject lispAiVector = new Vector(lispAi);
060: LispString[] lispAs = new LispString[as.length];
061: for (int i = 0; i < as.length; i++) {
062: lispAs[i] = new LispString(as[i]);
063: }
064: LispObject lispAsVector = new Vector(lispAs);
065: LispObject[] args = new LispObject[] //FIXME: count -> seq_num
066: { data, new JavaObject(o), lispAiVector, lispAsVector,
067: Keyword.internKeyword(s), count };
068: try {
069: f.execute(args);
070: } catch (ConditionThrowable t) {
071: t.printStackTrace();
072: }
073: }
074: }
075: }
076:
077: // jregister-handler1 object event handler data count
078: private static final Primitive _JREGISTER_HANDLER = new Primitive(
079: "%jregister-handler", PACKAGE_JAVA) {
080: public LispObject execute(LispObject[] args)
081: throws ConditionThrowable {
082: if (args.length != 5)
083: throw new ConditionThrowable(
084: new WrongNumberOfArgumentsException(this ));
085: Map entryTable = null;
086: Object object = args[0].javaInstance();
087: String event = ((Symbol) args[1]).getName();
088: if (!table.containsKey(object)) {
089: entryTable = new HashMap();
090: table.put(object, entryTable);
091: } else {
092: entryTable = (Map) table.get(object);
093: }
094: Entry entry = new Entry((Function) args[2], event,
095: entryTable);
096: if (args[3] != NIL)
097: entry.addData(args[3]);
098: if (args[4] != NIL)
099: entry.addCount(((Fixnum) args[4]).getValue());
100: entryTable.put(event, entry);
101: return T;
102: }
103: };
104: }
105:
106: class Entry {
107: public Entry(Function handler, String event, Map entryTable) {
108: this .entryTable = entryTable;
109: this .event = event;
110: this .handler = handler;
111: }
112:
113: public Function getHandler() {
114: return handler;
115: }
116:
117: public void addData(LispObject data) {
118: this .data = data;
119: }
120:
121: public LispObject getData() {
122: return data;
123: }
124:
125: public void addCount(int count) {
126: this .count = count;
127: }
128:
129: public Fixnum getCount() {
130: if (count == 0)
131: entryTable.remove(event);
132: return (new Fixnum(count--));
133: }
134:
135: Function handler;
136: LispObject data;
137: int count = -1;
138: Map entryTable;
139: String event;
140: }
|