source: branches/0.17.x/abcl/src/org/armedbear/lisp/ZeroRankArray.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/*
2 * ZeroRankArray.java
3 *
4 * Copyright (C) 2004-2005 Peter Graves
5 * $Id: ZeroRankArray.java 12254 2009-11-06 20:07:54Z 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
34package org.armedbear.lisp;
35
36public final class ZeroRankArray extends AbstractArray
37{
38    private final LispObject elementType;
39    private final boolean adjustable;
40
41    private LispObject data;
42
43    public ZeroRankArray(LispObject elementType, LispObject data,
44                         boolean adjustable)
45    {
46        this.elementType = elementType;
47        this.data = data;
48        this.adjustable = adjustable;
49    }
50
51    @Override
52    public LispObject typeOf()
53    {
54        if (adjustable)
55            return list(Symbol.ARRAY, elementType, NIL);
56        else
57            return list(Symbol.SIMPLE_ARRAY, elementType, NIL);
58    }
59
60    @Override
61    public LispObject classOf()
62    {
63        return BuiltInClass.ARRAY;
64    }
65
66    @Override
67    public LispObject typep(LispObject type)
68    {
69        if (type == Symbol.SIMPLE_ARRAY)
70            return adjustable ? NIL : T;
71        return super.typep(type);
72    }
73
74    @Override
75    public int getRank()
76    {
77        return 0;
78    }
79
80    @Override
81    public LispObject getDimensions()
82    {
83        return NIL;
84    }
85
86    @Override
87    public int getDimension(int n)
88    {
89        error(new TypeError("Bad array dimension (" + n + ") for array of rank 0."));
90        // Not reached.
91        return -1;
92    }
93
94    @Override
95    public LispObject getElementType()
96    {
97        return elementType;
98    }
99
100    @Override
101    public int getTotalSize()
102    {
103        return 1;
104    }
105
106    @Override
107    public LispObject AREF(int index)
108    {
109        if (index == 0)
110            return data;
111        else
112            return error(new TypeError("Bad row major index " + index + "."));
113    }
114
115    @Override
116    public void aset(int index, LispObject obj)
117    {
118        if (obj.typep(elementType) == NIL)
119            error(new TypeError(obj, elementType));
120        if (index == 0)
121            data = obj;
122        else
123            error(new TypeError("Bad row major index " + index + "."));
124    }
125
126    @Override
127    public void fill(LispObject obj)
128    {
129        if (obj.typep(elementType) == NIL)
130            error(new TypeError(obj, elementType));
131        data = obj;
132    }
133
134    @Override
135    public String writeToString()
136    {
137        final LispThread thread = LispThread.currentThread();
138        boolean printReadably = (Symbol.PRINT_READABLY.symbolValue(thread) != NIL);
139        if (printReadably) {
140            if (elementType != T) {
141                error(new PrintNotReadable(list(Keyword.OBJECT, this)));
142                // Not reached.
143                return null;
144            }
145        }
146        if (printReadably || Symbol.PRINT_ARRAY.symbolValue(thread) != NIL) {
147            StringBuffer sb = new StringBuffer("#0A");
148            if (data == this && Symbol.PRINT_CIRCLE.symbolValue(thread) != NIL) {
149                StringOutputStream stream = new StringOutputStream();
150                thread.execute(Symbol.OUTPUT_OBJECT.getSymbolFunction(),
151                               data, stream);
152                sb.append(stream.getString().getStringValue());
153            } else
154                sb.append(data.writeToString());
155            return sb.toString();
156        }
157        StringBuffer sb = new StringBuffer();
158        if (!adjustable)
159            sb.append("SIMPLE-");
160        sb.append("ARRAY ");
161        sb.append(elementType.writeToString());
162        sb.append(" NIL");
163        return unreadableString(sb.toString());
164    }
165
166  @Override
167  public AbstractArray adjustArray(int[] dims,
168                                              LispObject initialElement,
169                                              LispObject initialContents)
170    {
171      if (isAdjustable()) {
172          // initial element doesn't matter:
173          // we're not creating new elements
174          if (initialContents != null)
175              data = initialContents;
176          return this;
177      } else {
178          return new ZeroRankArray(elementType,
179                  initialContents != null ? initialContents :
180                      initialElement != null ? initialElement : data, false);
181      }
182  }
183
184  @Override
185  public AbstractArray adjustArray(int[] dims,
186                                              AbstractArray displacedTo,
187                                              int displacement)
188    {
189      error(new TypeError("Displacement not supported for array of rank 0."));
190      return null;
191  }
192}
Note: See TracBrowser for help on using the repository browser.