KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > JHandler


1 /*
2  * JHandler.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: JHandler.java,v 1.8 2004/02/24 01:54:40 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.lisp;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.WeakHashMap JavaDoc;
27
28 public final class JHandler extends Lisp
29 {
30     private static final Map JavaDoc table = new WeakHashMap JavaDoc();
31
32     public static void callLisp (String JavaDoc s, Object JavaDoc o)
33     {
34         callLisp(s, o, "");
35     }
36
37     public static void callLisp (String JavaDoc s, Object JavaDoc o, String JavaDoc s1)
38     {
39         callLisp(s, o, s1, new int[] {});
40     }
41
42     public static void callLisp (String JavaDoc s, Object JavaDoc o, String JavaDoc s1, int ai[]) {
43         callLisp(s, o, new String JavaDoc[] { s1 }, ai);
44     }
45
46     public static void callLisp (String JavaDoc s, Object JavaDoc o, String JavaDoc as[])
47     {
48         callLisp(s, o, as, new int[] {});
49     }
50
51     public static void callLisp (String JavaDoc s, Object JavaDoc o, String JavaDoc as[], int ai[])
52     {
53         if (table.containsKey(o)) {
54             Map JavaDoc entryTable = (Map JavaDoc)table.get(o);
55             if (entryTable.containsKey(s)) {
56                 Function f = ((Entry)entryTable.get(s)).getHandler();
57                 LispObject data = ((Entry)entryTable.get(s)).getData();
58                 Fixnum count = ((Entry)entryTable.get(s)).getCount();
59                 Fixnum[] lispAi = new Fixnum[ai.length];
60                 for (int i = 0; i < ai.length; i++) {
61                     lispAi[i] = new Fixnum(ai[i]);
62                 }
63                 LispObject lispAiVector = new SimpleVector(lispAi);
64                 SimpleString[] lispAs = new SimpleString[as.length];
65                 for (int i = 0; i < as.length; i++) {
66                     lispAs[i] = new SimpleString(as[i]);
67                 }
68                 LispObject lispAsVector = new SimpleVector(lispAs);
69                 LispObject[] args = new LispObject[] //FIXME: count -> seq_num
70
{ data, new JavaObject(o), lispAiVector, lispAsVector, Keyword.internKeyword(s), count };
71                 try {
72                     f.execute(args);
73                 }
74                 catch (ConditionThrowable t) {
75                     t.printStackTrace();
76                 }
77             }
78         }
79     }
80
81     // jregister-handler1 object event handler data count
82
private static final Primitive _JREGISTER_HANDLER =
83         new Primitive("%jregister-handler", PACKAGE_JAVA)
84     {
85         public LispObject execute(LispObject[] args) throws ConditionThrowable
86         {
87             if (args.length != 5)
88                 return signal(new WrongNumberOfArgumentsException(this));
89             Map JavaDoc entryTable = null;
90             Object JavaDoc object = args[0].javaInstance();
91             String JavaDoc event = ((Symbol)args[1]).getName();
92             if (!table.containsKey(object)) {
93                 entryTable = new HashMap JavaDoc();
94                 table.put(object,entryTable);
95             } else {
96                 entryTable = (Map JavaDoc)table.get(object);
97             }
98             Entry entry = new Entry((Function) args[2], args[3], event, entryTable);
99             if (args[4] != NIL)
100                 entry.addCount(((Fixnum)args[4]).getValue());
101             entryTable.put(event,entry);
102             return T;
103         }
104     };
105
106     private static class Entry
107     {
108         Function handler;
109         LispObject data;
110         int count = -1;
111         Map JavaDoc entryTable;
112         String JavaDoc event;
113
114         public Entry (Function handler, LispObject data, String JavaDoc event, Map JavaDoc entryTable)
115         {
116             this.entryTable = entryTable;
117             this.event = event;
118             this.handler = handler;
119             this.data = data;
120         }
121
122         public Function getHandler ()
123         {
124             return handler;
125         }
126
127         public void addData (LispObject data)
128         {
129             this.data = data;
130         }
131
132         public LispObject getData ()
133         {
134             return data;
135         }
136
137         public void addCount (int count)
138         {
139             this.count = count;
140         }
141
142         public Fixnum getCount ()
143         {
144             if (count == 0)
145                 entryTable.remove(event);
146             return (new Fixnum (count--));
147         }
148     }
149 }
150
Popular Tags