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

Last change on this file was 11754, checked in by vvoutilainen, 16 years ago

Convert using ClassCastException? to checking instanceof.
Performance tests show this approach to be faster.
Patch by Douglas R. Miles. I modified the patch to
remove tabs, so indentation may be slightly off in places.
That's something that we need to handle separately, abcl
doesn't have a clear indentation policy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1/*
2 * SynonymStream.java
3 *
4 * Copyright (C) 2004 Peter Graves
5 * $Id: SynonymStream.java 11754 2009-04-12 10:53:39Z 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., 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
36public final class SynonymStream extends Stream
37{
38    private final Symbol symbol;
39
40    private SynonymStream(Symbol symbol)
41    {
42        this.symbol = symbol;
43    }
44
45    @Override
46    public boolean isInputStream() throws ConditionThrowable
47    {
48        return checkStream(symbol.symbolValue()).isInputStream();
49    }
50
51    @Override
52    public boolean isOutputStream() throws ConditionThrowable
53    {
54        return checkStream(symbol.symbolValue()).isOutputStream();
55    }
56
57    @Override
58    public boolean isCharacterInputStream() throws ConditionThrowable
59    {
60        return checkStream(symbol.symbolValue()).isCharacterInputStream();
61    }
62
63    @Override
64    public boolean isBinaryInputStream() throws ConditionThrowable
65    {
66        return checkStream(symbol.symbolValue()).isBinaryInputStream();
67    }
68
69    @Override
70    public boolean isCharacterOutputStream() throws ConditionThrowable
71    {
72        return checkStream(symbol.symbolValue()).isCharacterOutputStream();
73    }
74
75    @Override
76    public boolean isBinaryOutputStream() throws ConditionThrowable
77    {
78        return checkStream(symbol.symbolValue()).isBinaryOutputStream();
79    }
80
81    @Override
82    public LispObject typeOf()
83    {
84        return Symbol.SYNONYM_STREAM;
85    }
86
87    @Override
88    public LispObject classOf()
89    {
90        return BuiltInClass.SYNONYM_STREAM;
91    }
92
93    @Override
94    public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable
95    {
96        if (typeSpecifier == Symbol.SYNONYM_STREAM)
97            return T;
98        if (typeSpecifier == BuiltInClass.SYNONYM_STREAM)
99            return T;
100        return super.typep(typeSpecifier);
101    }
102
103    @Override
104    public LispObject getElementType() throws ConditionThrowable
105    {
106        return checkStream(symbol.symbolValue()).getElementType();
107    }
108
109    @Override
110    public LispObject listen() throws ConditionThrowable
111    {
112        return checkStream(symbol.symbolValue()).listen();
113    }
114
115    @Override
116    public LispObject fileLength() throws ConditionThrowable
117    {
118        return checkStream(symbol.symbolValue()).fileLength();
119    }
120
121    @Override
122    public LispObject fileStringLength(LispObject arg) throws ConditionThrowable
123    {
124        return checkStream(symbol.symbolValue()).fileStringLength(arg);
125    }
126
127    @Override
128    protected int _readChar() throws ConditionThrowable
129    {
130        return checkStream(symbol.symbolValue())._readChar();
131    }
132
133    @Override
134    protected void _unreadChar(int n) throws ConditionThrowable
135    {
136        checkStream(symbol.symbolValue())._unreadChar(n);
137    }
138
139    @Override
140    protected boolean _charReady() throws ConditionThrowable
141    {
142        return checkStream(symbol.symbolValue())._charReady();
143    }
144
145    @Override
146    public void _writeChar(char c) throws ConditionThrowable
147    {
148        checkStream(symbol.symbolValue())._writeChar(c);
149    }
150
151    @Override
152    public void _writeChars(char[] chars, int start, int end)
153        throws ConditionThrowable
154    {
155        checkStream(symbol.symbolValue())._writeChars(chars, start, end);
156    }
157
158    @Override
159    public void _writeString(String s) throws ConditionThrowable
160    {
161        checkStream(symbol.symbolValue())._writeString(s);
162    }
163
164    @Override
165    public void _writeLine(String s) throws ConditionThrowable
166    {
167        checkStream(symbol.symbolValue())._writeLine(s);
168    }
169
170    // Reads an 8-bit byte.
171    @Override
172    public int _readByte() throws ConditionThrowable
173    {
174        return checkStream(symbol.symbolValue())._readByte();
175    }
176
177    // Writes an 8-bit byte.
178    @Override
179    public void _writeByte(int n) throws ConditionThrowable
180    {
181        checkStream(symbol.symbolValue())._writeByte(n);
182    }
183
184    @Override
185    public void _finishOutput() throws ConditionThrowable
186    {
187        checkStream(symbol.symbolValue())._finishOutput();
188    }
189
190    @Override
191    public void _clearInput() throws ConditionThrowable
192    {
193        checkStream(symbol.symbolValue())._clearInput();
194    }
195
196    @Override
197    protected long _getFilePosition() throws ConditionThrowable
198    {
199        return checkStream(symbol.symbolValue())._getFilePosition();
200    }
201
202    @Override
203    protected boolean _setFilePosition(LispObject arg) throws ConditionThrowable
204    {
205        return checkStream(symbol.symbolValue())._setFilePosition(arg);
206    }
207
208    @Override
209    public void _close() throws ConditionThrowable
210    {
211        checkStream(symbol.symbolValue())._close();
212    }
213
214    @Override
215    public String writeToString() throws ConditionThrowable
216    {
217        StringBuffer sb = new StringBuffer("SYNONYM-STREAM ");
218        sb.append(symbol.writeToString());
219        return unreadableString(sb.toString());
220    }
221
222    // ### make-synonym-stream symbol => synonym-stream
223    private static final Primitive MAKE_SYNONYM_STREAM =
224        new Primitive("make-synonym-stream", "symbol")
225    {
226        @Override
227        public LispObject execute(LispObject arg) throws ConditionThrowable
228        {
229            return new SynonymStream(checkSymbol(arg));
230        }
231    };
232
233    // ### synonym-stream-symbol synonym-stream => symbol
234    private static final Primitive SYNONYM_STREAM_STREAMS =
235        new Primitive("synonym-stream-symbol", "synonym-stream")
236    {
237        @Override
238        public LispObject execute(LispObject arg) throws ConditionThrowable
239        {
240            if (arg instanceof SynonymStream) 
241                return ((SynonymStream)arg).symbol;
242            return error(new TypeError(arg, Symbol.SYNONYM_STREAM));
243        }
244    };
245}
Note: See TracBrowser for help on using the repository browser.