1 | /* |
---|
2 | * NilVector.java |
---|
3 | * |
---|
4 | * Copyright (C) 2004-2005 Peter Graves |
---|
5 | * $Id: NilVector.java 13442 2011-08-05 21:51:28Z ehuelsmann $ |
---|
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 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public final class NilVector extends AbstractString |
---|
39 | { |
---|
40 | private int capacity; |
---|
41 | |
---|
42 | public NilVector(int capacity) |
---|
43 | { |
---|
44 | this.capacity = capacity; |
---|
45 | } |
---|
46 | |
---|
47 | @Override |
---|
48 | public char[] chars() |
---|
49 | { |
---|
50 | if (capacity != 0) |
---|
51 | accessError(); |
---|
52 | return new char[0]; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public char[] getStringChars() |
---|
57 | { |
---|
58 | if (capacity != 0) |
---|
59 | accessError(); |
---|
60 | return new char[0]; |
---|
61 | } |
---|
62 | |
---|
63 | @Override |
---|
64 | public String getStringValue() |
---|
65 | { |
---|
66 | if (capacity != 0) |
---|
67 | accessError(); |
---|
68 | return ""; |
---|
69 | } |
---|
70 | |
---|
71 | @Override |
---|
72 | public LispObject typeOf() |
---|
73 | { |
---|
74 | return list(Symbol.NIL_VECTOR, Fixnum.getInstance(capacity)); |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public LispObject classOf() |
---|
79 | { |
---|
80 | return BuiltInClass.NIL_VECTOR; |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | public LispObject typep(LispObject type) |
---|
85 | { |
---|
86 | if (type == Symbol.NIL_VECTOR) |
---|
87 | return T; |
---|
88 | if (type == Symbol.SIMPLE_STRING) |
---|
89 | return T; |
---|
90 | if (type == Symbol.SIMPLE_ARRAY) |
---|
91 | return T; |
---|
92 | if (type == BuiltInClass.NIL_VECTOR) |
---|
93 | return T; |
---|
94 | if (type == BuiltInClass.SIMPLE_STRING) |
---|
95 | return T; |
---|
96 | if (type == BuiltInClass.SIMPLE_ARRAY) |
---|
97 | return T; |
---|
98 | return super.typep(type); |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public LispObject SIMPLE_STRING_P() |
---|
103 | { |
---|
104 | return T; |
---|
105 | } |
---|
106 | |
---|
107 | @Override |
---|
108 | public boolean equal(LispObject obj) |
---|
109 | { |
---|
110 | if (obj instanceof NilVector) { |
---|
111 | if (capacity != ((NilVector)obj).capacity) |
---|
112 | return false; |
---|
113 | if (capacity != 0) { |
---|
114 | accessError(); |
---|
115 | // Not reached. |
---|
116 | return false; |
---|
117 | } |
---|
118 | return true; |
---|
119 | } |
---|
120 | if (obj instanceof AbstractString) { |
---|
121 | if (capacity != obj.length()) |
---|
122 | return false; |
---|
123 | if (capacity != 0) { |
---|
124 | accessError(); |
---|
125 | // Not reached. |
---|
126 | return false; |
---|
127 | } |
---|
128 | return true; |
---|
129 | } |
---|
130 | return false; |
---|
131 | } |
---|
132 | |
---|
133 | public String getValue() |
---|
134 | { |
---|
135 | if (capacity == 0) |
---|
136 | return ""; |
---|
137 | accessError(); |
---|
138 | // Not reached. |
---|
139 | return null; |
---|
140 | } |
---|
141 | |
---|
142 | @Override |
---|
143 | public int length() |
---|
144 | { |
---|
145 | return capacity; |
---|
146 | } |
---|
147 | |
---|
148 | @Override |
---|
149 | public int capacity() |
---|
150 | { |
---|
151 | return capacity; |
---|
152 | } |
---|
153 | |
---|
154 | @Override |
---|
155 | public LispObject getElementType() |
---|
156 | { |
---|
157 | return NIL; |
---|
158 | } |
---|
159 | |
---|
160 | @Override |
---|
161 | public LispObject CHAR(int index) |
---|
162 | { |
---|
163 | return accessError(); |
---|
164 | } |
---|
165 | |
---|
166 | @Override |
---|
167 | public LispObject SCHAR(int index) |
---|
168 | { |
---|
169 | return accessError(); |
---|
170 | } |
---|
171 | |
---|
172 | @Override |
---|
173 | public LispObject AREF(int index) |
---|
174 | { |
---|
175 | return accessError(); |
---|
176 | } |
---|
177 | |
---|
178 | @Override |
---|
179 | public void aset(int index, LispObject newValue) |
---|
180 | { |
---|
181 | storeError(newValue); |
---|
182 | } |
---|
183 | |
---|
184 | @Override |
---|
185 | public char charAt(int index) |
---|
186 | { |
---|
187 | accessError(); |
---|
188 | // Not reached. |
---|
189 | return 0; |
---|
190 | } |
---|
191 | |
---|
192 | @Override |
---|
193 | public void setCharAt(int index, char c) |
---|
194 | { |
---|
195 | storeError(LispCharacter.getInstance(c)); |
---|
196 | } |
---|
197 | |
---|
198 | @Override |
---|
199 | public LispObject subseq(int start, int end) |
---|
200 | { |
---|
201 | if (capacity == 0 && start == 0 && end == 0) |
---|
202 | return this; |
---|
203 | return accessError(); |
---|
204 | } |
---|
205 | |
---|
206 | @Override |
---|
207 | public void fill(LispObject obj) |
---|
208 | { |
---|
209 | storeError(obj); |
---|
210 | } |
---|
211 | |
---|
212 | @Override |
---|
213 | public void fill(char c) |
---|
214 | { |
---|
215 | storeError(LispCharacter.getInstance(c)); |
---|
216 | } |
---|
217 | |
---|
218 | @Override |
---|
219 | public void shrink(int n) |
---|
220 | { |
---|
221 | } |
---|
222 | |
---|
223 | @Override |
---|
224 | public LispObject reverse() |
---|
225 | { |
---|
226 | return accessError(); |
---|
227 | } |
---|
228 | |
---|
229 | public LispObject accessError() |
---|
230 | { |
---|
231 | return error(new TypeError("Attempt to access an array of element type NIL.")); |
---|
232 | } |
---|
233 | |
---|
234 | private void storeError(LispObject obj) |
---|
235 | { |
---|
236 | error(new TypeError(String.valueOf(obj) + " is not of type NIL.")); |
---|
237 | } |
---|
238 | |
---|
239 | @Override |
---|
240 | public int sxhash() |
---|
241 | { |
---|
242 | return 0; |
---|
243 | } |
---|
244 | |
---|
245 | @Override |
---|
246 | public AbstractVector adjustArray(int newCapacity, |
---|
247 | LispObject initialElement, |
---|
248 | LispObject initialContents) |
---|
249 | |
---|
250 | { |
---|
251 | accessError(); |
---|
252 | // Not reached. |
---|
253 | return null; |
---|
254 | } |
---|
255 | |
---|
256 | @Override |
---|
257 | public AbstractVector adjustArray(int size, AbstractArray displacedTo, |
---|
258 | int displacement) |
---|
259 | |
---|
260 | { |
---|
261 | accessError(); |
---|
262 | // Not reached. |
---|
263 | return null; |
---|
264 | } |
---|
265 | } |
---|