source: branches/0.15.x/abcl/src/org/armedbear/lisp/JHandler.java

Last change on this file was 11714, checked in by ehuelsmann, 16 years ago

Use the Fixnum factory instead of creating new Fixnums all over the place.

Patch by: Douglas Miles (logicmoo at gmail.com)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/*
2 * JHandler.java
3 *
4 * Copyright (C) 2003-2005 Andras Simon, Peter Graves
5 * $Id: JHandler.java 11714 2009-03-23 20:05:37Z ehuelsmann $
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 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36import java.util.HashMap;
37import java.util.Map;
38import java.util.WeakHashMap;
39
40public final class JHandler extends Lisp
41{
42    private static final Map<Object,Map<String,Entry>> table =
43       new WeakHashMap<Object,Map<String,Entry>>();
44
45    public static void callLisp (String s, Object o)
46    {
47        callLisp(s, o, "");
48    }
49
50    public static void callLisp (String s, Object o, String s1)
51    {
52        callLisp(s, o, s1, new int[] {});
53    }
54
55    public static void callLisp (String s, Object o, String s1, int ai[]) {
56        callLisp(s, o, new String[] { s1 }, ai);
57    }
58
59    public static void callLisp (String s, Object o, String as[])
60    {
61        callLisp(s, o, as, new int[] {});
62    }
63
64    public static void callLisp (String s, Object o, String as[], int ai[])
65    {
66        if (table.containsKey(o)) {
67            Map<String,Entry> entryTable =  (Map<String,Entry>)table.get(o);
68            if (entryTable.containsKey(s)) {
69                Function f = ((Entry)entryTable.get(s)).getHandler();
70                LispObject data = ((Entry)entryTable.get(s)).getData();
71                Fixnum count = ((Entry)entryTable.get(s)).getCount();
72                Fixnum[] lispAi = new Fixnum[ai.length];
73                for (int i = 0; i < ai.length; i++) {
74                    lispAi[i] = Fixnum.getInstance(ai[i]);
75                }
76                LispObject lispAiVector = new SimpleVector(lispAi);
77                SimpleString[] lispAs = new SimpleString[as.length];
78                for (int i = 0; i < as.length; i++) {
79                    lispAs[i] = new SimpleString(as[i]);
80                }
81                LispObject lispAsVector = new SimpleVector(lispAs);
82                LispObject[] args = new LispObject[] //FIXME: count -> seq_num
83                { data, new JavaObject(o), lispAiVector, lispAsVector, Keyword.internKeyword(s), count };
84                try {
85                    f.execute(args);
86                }
87                catch (ConditionThrowable t) {
88                    t.printStackTrace();
89                }
90            }
91        }
92    }
93
94    // jregister-handler1 object event handler data count
95    private static final Primitive _JREGISTER_HANDLER =
96        new Primitive("%jregister-handler", PACKAGE_JAVA)
97    {
98        @Override
99        public LispObject execute(LispObject[] args) throws ConditionThrowable
100        {
101            if (args.length != 5)
102                return error(new WrongNumberOfArgumentsException(this));
103            Map<String,Entry> entryTable = null;
104            Object object = args[0].javaInstance();
105            String event = ((Symbol)args[1]).getName();
106            if (!table.containsKey(object)) {
107                entryTable = new HashMap<String,Entry>();
108                table.put(object,entryTable);
109            } else {
110                entryTable = (Map<String,Entry>)table.get(object);
111            }
112            Entry entry = new Entry((Function) args[2], args[3], event, entryTable);
113            if (args[4] != NIL)
114                entry.addCount(((Fixnum)args[4]).value);
115            entryTable.put(event,entry);
116            return T;
117        }
118    };
119
120    private static class Entry
121    {
122        Function handler;
123        LispObject data;
124        int count = -1;
125        Map<String,Entry> entryTable;
126        String event;
127
128        public Entry (Function handler, LispObject data, String event,
129                      Map<String,Entry> entryTable)
130        {
131            this.entryTable = entryTable;
132            this.event = event;
133            this.handler = handler;
134            this.data = data;
135        }
136
137        public Function getHandler ()
138        {
139            return handler;
140        }
141
142        public void addData (LispObject data)
143        {
144            this.data = data;
145        }
146
147        public LispObject getData ()
148        {
149            return data;
150        }
151
152        public void addCount (int count)
153        {
154            this.count = count;
155        }
156
157        public Fixnum getCount ()
158        {
159            if (count == 0)
160                entryTable.remove(event);
161            return (Fixnum.getInstance (count--));
162        }
163    }
164}
Note: See TracBrowser for help on using the repository browser.