source: branches/0.19.x/abcl/src/org/armedbear/lisp/Condition.java

Last change on this file was 12512, checked in by vvoutilainen, 15 years ago

Make Condition.writeToString() final, add documentation to
Condition.getMessage(), fix remaining overrides of Condition.writeToString()
to override Condition.getMessage() instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1/*
2 * Condition.java
3 *
4 * Copyright (C) 2003-2007 Peter Graves
5 * $Id: Condition.java 12512 2010-02-28 15:54:17Z vvoutilainen $
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 static org.armedbear.lisp.Lisp.*;
37
38public class Condition extends StandardObject
39{
40  protected String message;
41
42  public Condition()
43  {
44    super(StandardClass.CONDITION);
45    Debug.assertTrue(slots.length == 2);
46    setFormatArguments(NIL);
47  }
48
49  protected Condition(LispClass cls)
50  {
51    super(cls);
52    Debug.assertTrue(slots.length >= 2);
53    setFormatArguments(NIL);
54  }
55
56  public Condition(LispClass cls, int length)
57  {
58    super(cls, length);
59  }
60
61  public Condition(LispObject initArgs)
62  {
63    super(StandardClass.CONDITION);
64    Debug.assertTrue(slots.length == 2);
65    initialize(initArgs);
66  }
67
68  protected void initialize(LispObject initArgs)
69  {
70    LispObject control = null;
71    LispObject arguments = null;
72    LispObject first, second;
73    while (initArgs instanceof Cons)
74      {
75        first = initArgs.car();
76        initArgs = initArgs.cdr();
77        second = initArgs.car();
78        initArgs = initArgs.cdr();
79        if (first == Keyword.FORMAT_CONTROL)
80          {
81            if (control == null)
82              control = second;
83          }
84        else if (first == Keyword.FORMAT_ARGUMENTS)
85          {
86            if (arguments == null)
87              arguments = second;
88          }
89      }
90    if (control != null)
91      setFormatControl(control);
92    if (arguments == null)
93      arguments = NIL;
94    setFormatArguments(arguments);
95  }
96
97  public Condition(String message)
98  {
99    super(StandardClass.CONDITION);
100    Debug.assertTrue(slots.length == 2);
101    setFormatControl(message);
102    setFormatArguments(NIL);
103  }
104
105  public final LispObject getFormatControl()
106  {
107    return getInstanceSlotValue(Symbol.FORMAT_CONTROL);
108  }
109
110  public final void setFormatControl(LispObject formatControl)
111
112  {
113    setInstanceSlotValue(Symbol.FORMAT_CONTROL, formatControl);
114  }
115
116  public final void setFormatControl(String s)
117  {
118    setFormatControl(new SimpleString(s));
119  }
120
121  public final LispObject getFormatArguments()
122  {
123    return getInstanceSlotValue(Symbol.FORMAT_ARGUMENTS);
124  }
125
126  public final void setFormatArguments(LispObject formatArguments)
127
128  {
129    setInstanceSlotValue(Symbol.FORMAT_ARGUMENTS, formatArguments);
130  }
131
132  /**
133   * Extending classes should override this method if they want to
134   * customize how they will be printed.
135   */
136  public String getMessage()
137  {
138    return getFormatControl().toString();
139  }
140
141  @Override
142  public LispObject typeOf()
143  {
144    LispClass c = getLispClass();
145    if (c != null)
146      return c.getName();
147    return Symbol.CONDITION;
148  }
149
150  @Override
151  public LispObject classOf()
152  {
153    LispClass c = getLispClass();
154    if (c != null)
155      return c;
156    return StandardClass.CONDITION;
157  }
158
159  @Override
160  public LispObject typep(LispObject type)
161  {
162    if (type == Symbol.CONDITION)
163      return T;
164    if (type == StandardClass.CONDITION)
165      return T;
166    return super.typep(type);
167  }
168
169  public String getConditionReport()
170  {
171    String s = getMessage();
172    if (s != null)
173      return s;
174    LispObject formatControl = getFormatControl();
175    if (formatControl != NIL)
176      {
177        return format(formatControl, getFormatArguments());
178      }
179    return unreadableString(typeOf().writeToString());
180  }
181
182  @Override
183  public final String writeToString()
184  {
185    final LispThread thread = LispThread.currentThread();
186    if (Symbol.PRINT_ESCAPE.symbolValue(thread) == NIL)
187      {
188        String s = getMessage();
189        if (s != null)
190          return s;
191        LispObject formatControl = getFormatControl();
192        if (formatControl instanceof Function)
193          {
194            StringOutputStream stream = new StringOutputStream();
195            Symbol.APPLY.execute(formatControl, stream, getFormatArguments());
196            return stream.getString().getStringValue();
197          }
198        if (formatControl instanceof AbstractString)
199          {
200            LispObject f = Symbol.FORMAT.getSymbolFunction();
201            if (f == null || f instanceof Autoload)
202              return format(formatControl, getFormatArguments());
203            return Symbol.APPLY.execute(f, NIL, formatControl, getFormatArguments()).getStringValue();
204          }
205      }
206    final int maxLevel;
207    LispObject printLevel = Symbol.PRINT_LEVEL.symbolValue(thread);
208    if (printLevel instanceof Fixnum)
209      maxLevel = ((Fixnum)printLevel).value;
210    else
211      maxLevel = Integer.MAX_VALUE;
212    LispObject currentPrintLevel =
213      _CURRENT_PRINT_LEVEL_.symbolValue(thread);
214    int currentLevel = ((Fixnum)currentPrintLevel).value;
215    if (currentLevel >= maxLevel)
216      return "#";
217    return unreadableString(typeOf().writeToString());
218  }
219}
Note: See TracBrowser for help on using the repository browser.