1 21 22 package org.armedbear.lisp; 23 import java.util.LinkedList ; 24 import java.util.NoSuchElementException ; 25 26 public final class Mailbox extends LispObject 27 { 28 29 private LinkedList box = new LinkedList (); 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 e) { 46 throw new RuntimeException (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 e) { 58 return NIL; 59 } 60 } 61 } 62 63 private LispObject empty () 64 { 65 return box.isEmpty() ? T : NIL; 66 } 67 68 public String toString() 69 { 70 StringBuffer sb = new StringBuffer ("#<MAILBOX @ #x"); 71 sb.append(Integer.toHexString(hashCode())); 72 sb.append(">"); 73 return sb.toString(); 74 } 75 76 77 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 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 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 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 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 |