KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Condition.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: Condition.java,v 1.25 2004/08/18 17:14:38 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 public class Condition extends StandardObject
25 {
26     private LispObject formatControl = NIL;
27     private LispObject formatArguments = NIL;
28
29     private String JavaDoc message = null;
30
31     public Condition()
32     {
33         message = null;
34     }
35
36     public Condition(LispClass cls, SimpleVector slots)
37     {
38         super(cls, slots);
39         message = null;
40     }
41
42     public Condition(LispObject initArgs) throws ConditionThrowable
43     {
44         super(BuiltInClass.CONDITION, null);
45         LispObject formatControl = NIL;
46         LispObject formatArguments = NIL;
47         LispObject first, second;
48         while (initArgs instanceof Cons) {
49             first = initArgs.car();
50             initArgs = initArgs.cdr();
51             second = initArgs.car();
52             initArgs = initArgs.cdr();
53             if (first == Keyword.FORMAT_CONTROL)
54                 formatControl = second;
55             else if (first == Keyword.FORMAT_ARGUMENTS)
56                 formatArguments = second;
57         }
58         setFormatControl(formatControl);
59         setFormatArguments(formatArguments);
60     }
61
62     public Condition(String JavaDoc message)
63     {
64         this.message = message;
65     }
66
67     public final LispObject getFormatControl()
68     {
69         return formatControl;
70     }
71
72     public final void setFormatControl(LispObject formatControl)
73     {
74         this.formatControl = formatControl;
75     }
76
77     public final LispObject getFormatArguments()
78     {
79         return formatArguments;
80     }
81
82     public final void setFormatArguments(LispObject formatArguments)
83     {
84         this.formatArguments = formatArguments;
85     }
86
87     public String JavaDoc getMessage()
88     {
89         return message;
90     }
91
92     public LispObject typeOf()
93     {
94         LispClass c = getLispClass();
95         if (c != null)
96             return c.getSymbol();
97         return Symbol.CONDITION;
98     }
99
100     public LispClass classOf()
101     {
102         LispClass c = getLispClass();
103         if (c != null)
104             return c;
105         return BuiltInClass.CONDITION;
106     }
107
108     public LispObject typep(LispObject type) throws ConditionThrowable
109     {
110         if (type == Symbol.CONDITION)
111             return T;
112         if (type == BuiltInClass.CONDITION)
113             return T;
114         return super.typep(type);
115     }
116
117     public String JavaDoc getConditionReport() throws ConditionThrowable
118     {
119         String JavaDoc s = getMessage();
120         if (s != null)
121             return s;
122         if (formatControl != NIL) {
123             try {
124                 return format(formatControl, formatArguments);
125             }
126             catch (Throwable JavaDoc t) {}
127         }
128         return unreadableString(typeOf().writeToString());
129     }
130
131     public String JavaDoc writeToString() throws ConditionThrowable
132     {
133         if (_PRINT_ESCAPE_.symbolValue() == NIL) {
134             String JavaDoc s = getMessage();
135             if (s != null)
136                 return s;
137             if (formatControl != NIL)
138                 return format(formatControl, formatArguments);
139         }
140         return unreadableString(typeOf().writeToString());
141     }
142
143     // ### condition-report
144
private static final Primitive1 CONDITION_REPORT =
145         new Primitive1("condition-report", PACKAGE_SYS, false, "condition")
146     {
147         public LispObject execute(LispObject arg) throws ConditionThrowable
148         {
149             try {
150                 String JavaDoc s = ((Condition)arg).getMessage();
151                 return s != null ? new SimpleString(s) : NIL;
152             }
153             catch (ClassCastException JavaDoc e) {
154                 return signal(new TypeError(arg, Symbol.CONDITION));
155             }
156         }
157     };
158 }
159
Popular Tags