KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > ZeroRankArray


1 /*
2  * ZeroRankArray.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: ZeroRankArray.java,v 1.4 2004/06/20 14:55:35 piso Exp $
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
22 package org.armedbear.lisp;
23
24 public final class ZeroRankArray extends AbstractArray
25 {
26     private final LispObject elementType;
27     private final boolean adjustable;
28
29     private LispObject data;
30
31     public ZeroRankArray(LispObject elementType, LispObject data,
32                          boolean adjustable)
33     {
34         this.elementType = elementType;
35         this.data = data;
36         this.adjustable = adjustable;
37     }
38
39     public LispObject typeOf()
40     {
41         if (adjustable)
42             return list3(Symbol.ARRAY, elementType, NIL);
43         else
44             return list3(Symbol.SIMPLE_ARRAY, elementType, NIL);
45     }
46
47     public LispClass classOf()
48     {
49         return BuiltInClass.ARRAY;
50     }
51
52     public LispObject typep(LispObject type) throws ConditionThrowable
53     {
54         if (type == Symbol.SIMPLE_ARRAY)
55             return adjustable ? NIL : T;
56         return super.typep(type);
57     }
58
59     public int getRank()
60     {
61         return 0;
62     }
63
64     public LispObject getDimensions()
65     {
66         return NIL;
67     }
68
69     public int getDimension(int n) throws ConditionThrowable
70     {
71         signal(new TypeError("Bad array dimension (" + n + ") for array of rank 0."));
72         // Not reached.
73
return -1;
74     }
75
76     public LispObject getElementType()
77     {
78         return elementType;
79     }
80
81     public int getTotalSize()
82     {
83         return 1;
84     }
85
86     public LispObject getRowMajor(int index) throws ConditionThrowable
87     {
88         if (index == 0)
89             return data;
90         else
91             return signal(new TypeError("Bad row major index " + index + "."));
92     }
93
94     public void setRowMajor(int index, LispObject newValue) throws ConditionThrowable
95     {
96         if (newValue.typep(elementType) == NIL)
97             signal(new TypeError(newValue, elementType));
98         if (index == 0)
99             data = newValue;
100         else
101             signal(new TypeError("Bad row major index " + index + "."));
102     }
103
104     public void fill(LispObject obj) throws ConditionThrowable
105     {
106         if (obj.typep(elementType) == NIL)
107             signal(new TypeError(obj, elementType));
108         data = obj;
109     }
110
111     public String JavaDoc writeToString() throws ConditionThrowable
112     {
113         if (_PRINT_READABLY_.symbolValue() != NIL || _PRINT_ARRAY_.symbolValue() != NIL) {
114             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("#0A");
115             sb.append(data.writeToString());
116             return sb.toString();
117         } else {
118             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
119             if (!adjustable)
120                 sb.append("SIMPLE-");
121             sb.append("ARRAY ");
122             sb.append(elementType.writeToString());
123             sb.append(" NIL");
124             return unreadableString(sb.toString());
125         }
126     }
127 }
128
Popular Tags