KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Mailbox.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: Mailbox.java,v 1.3 2004/09/13 18:10:30 asimon 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 import java.util.LinkedList JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 public final class Mailbox extends LispObject
27 {
28
29     private LinkedList JavaDoc box = new LinkedList JavaDoc();
30
31     private void send (LispObject o)
32     {
33         synchronized(this) {
34             box.add(o);
35             notifyAll();
36         }
37     }
38
39     private LispObject read ()
40     {
41         while (box.isEmpty())
42             synchronized(this) {
43                 try {
44                     wait();
45                 } catch(InterruptedException JavaDoc e) {
46                     throw new RuntimeException JavaDoc(e);
47                 }
48             }
49         return (LispObject) box.removeFirst();
50     }
51
52     private LispObject peek ()
53     {
54         synchronized(this) {
55             try {
56                 return (LispObject) box.getFirst();
57             } catch(NoSuchElementException JavaDoc e) {
58                 return NIL;
59             }
60         }
61     }
62
63     private LispObject empty ()
64     {
65         return box.isEmpty() ? T : NIL;
66     }
67
68     public String JavaDoc toString()
69     {
70         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("#<MAILBOX @ #x");
71         sb.append(Integer.toHexString(hashCode()));
72         sb.append(">");
73         return sb.toString();
74     }
75
76
77     // ### make-mailbox
78
private static final Primitive0 MAKE_MAILBOX =
79         new Primitive0("make-mailbox", PACKAGE_EXT, true, "")
80     {
81         public LispObject execute() throws ConditionThrowable
82         {
83             return new Mailbox();
84         }
85     };
86
87     // ### mailbox-send mailbox object
88
private static final Primitive2 MAILBOX_SEND =
89         new Primitive2("mailbox-send", PACKAGE_EXT, true, "mailbox object")
90     {
91         public LispObject execute(LispObject first, LispObject second) throws ConditionThrowable
92         {
93             if (first instanceof Mailbox) {
94                 Mailbox mbox = (Mailbox) first;
95                 mbox.send(second);
96                 return T;
97             } else
98                 return signal(new TypeError(first, "Mailbox"));
99         }
100     };
101
102     // ### mailbox-read mailbox
103
private static final Primitive1 MAILBOX_READ =
104         new Primitive1("mailbox-read", PACKAGE_EXT, true, "mailbox")
105     {
106         public LispObject execute(LispObject arg) throws ConditionThrowable
107         {
108             if (arg instanceof Mailbox) {
109                 Mailbox mbox = (Mailbox) arg;
110                 return mbox.read();
111             } else
112                 return signal(new TypeError(arg, "Mailbox"));
113         }
114     };
115
116     // ### mailbox-peek mailbox
117
private static final Primitive1 MAILBOX_PEEK =
118         new Primitive1("mailbox-peek", PACKAGE_EXT, true, "mailbox")
119     {
120         public LispObject execute(LispObject arg) throws ConditionThrowable
121         {
122             if (arg instanceof Mailbox) {
123                 Mailbox mbox = (Mailbox) arg;
124                 return mbox.peek();
125             } else
126                 return signal(new TypeError(arg, "Mailbox"));
127         }
128     };
129
130     // ### mailbox-empty-p mailbox
131
private static final Primitive1 MAILBOX_EMPTY_P =
132         new Primitive1("mailbox-empty-p", PACKAGE_EXT, true, "mailbox")
133     {
134         public LispObject execute(LispObject arg) throws ConditionThrowable
135         {
136             if (arg instanceof Mailbox) {
137                 Mailbox mbox = (Mailbox) arg;
138                 return mbox.empty();
139             } else
140                 return signal(new TypeError(arg, "Mailbox"));
141         }
142     };
143 }
144
145
Popular Tags