001: /*
002: * ZeroRankArray.java
003: *
004: * Copyright (C) 2004 Peter Graves
005: * $Id: ZeroRankArray.java,v 1.4 2004/06/20 14:55:35 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public final class ZeroRankArray extends AbstractArray {
025: private final LispObject elementType;
026: private final boolean adjustable;
027:
028: private LispObject data;
029:
030: public ZeroRankArray(LispObject elementType, LispObject data,
031: boolean adjustable) {
032: this .elementType = elementType;
033: this .data = data;
034: this .adjustable = adjustable;
035: }
036:
037: public LispObject typeOf() {
038: if (adjustable)
039: return list3(Symbol.ARRAY, elementType, NIL);
040: else
041: return list3(Symbol.SIMPLE_ARRAY, elementType, NIL);
042: }
043:
044: public LispClass classOf() {
045: return BuiltInClass.ARRAY;
046: }
047:
048: public LispObject typep(LispObject type) throws ConditionThrowable {
049: if (type == Symbol.SIMPLE_ARRAY)
050: return adjustable ? NIL : T;
051: return super .typep(type);
052: }
053:
054: public int getRank() {
055: return 0;
056: }
057:
058: public LispObject getDimensions() {
059: return NIL;
060: }
061:
062: public int getDimension(int n) throws ConditionThrowable {
063: signal(new TypeError("Bad array dimension (" + n
064: + ") for array of rank 0."));
065: // Not reached.
066: return -1;
067: }
068:
069: public LispObject getElementType() {
070: return elementType;
071: }
072:
073: public int getTotalSize() {
074: return 1;
075: }
076:
077: public LispObject getRowMajor(int index) throws ConditionThrowable {
078: if (index == 0)
079: return data;
080: else
081: return signal(new TypeError("Bad row major index " + index
082: + "."));
083: }
084:
085: public void setRowMajor(int index, LispObject newValue)
086: throws ConditionThrowable {
087: if (newValue.typep(elementType) == NIL)
088: signal(new TypeError(newValue, elementType));
089: if (index == 0)
090: data = newValue;
091: else
092: signal(new TypeError("Bad row major index " + index + "."));
093: }
094:
095: public void fill(LispObject obj) throws ConditionThrowable {
096: if (obj.typep(elementType) == NIL)
097: signal(new TypeError(obj, elementType));
098: data = obj;
099: }
100:
101: public String writeToString() throws ConditionThrowable {
102: if (_PRINT_READABLY_.symbolValue() != NIL
103: || _PRINT_ARRAY_.symbolValue() != NIL) {
104: StringBuffer sb = new StringBuffer("#0A");
105: sb.append(data.writeToString());
106: return sb.toString();
107: } else {
108: StringBuffer sb = new StringBuffer();
109: if (!adjustable)
110: sb.append("SIMPLE-");
111: sb.append("ARRAY ");
112: sb.append(elementType.writeToString());
113: sb.append(" NIL");
114: return unreadableString(sb.toString());
115: }
116: }
117: }
|