001: /*
002: * LispCharacter.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: LispCharacter.java,v 1.7 2003/11/15 11:03:34 beedlem 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 LispCharacter extends LispObject {
025: private static final LispCharacter[] characters = new LispCharacter[128];
026:
027: static {
028: for (int i = characters.length; i-- > 0;)
029: characters[i] = new LispCharacter((char) i);
030: }
031:
032: private final char c;
033:
034: public static LispCharacter getInstance(char c) {
035: try {
036: return characters[c];
037: } catch (ArrayIndexOutOfBoundsException e) {
038: return new LispCharacter(c);
039: }
040: }
041:
042: private LispCharacter(char c) {
043: this .c = c;
044: }
045:
046: public LispObject typeOf() {
047: return Symbol.CHARACTER;
048: }
049:
050: public LispClass classOf() {
051: return BuiltInClass.CHARACTER;
052: }
053:
054: public LispObject typep(LispObject type) throws ConditionThrowable {
055: if (type == Symbol.CHARACTER)
056: return T;
057: if (type == BuiltInClass.CHARACTER)
058: return T;
059: if (type == Symbol.BASE_CHAR)
060: return T;
061: if (type == Symbol.STANDARD_CHAR)
062: return isStandardChar();
063: return super .typep(type);
064: }
065:
066: public LispObject isStandardChar() {
067: if (c >= ' ' && c < 127)
068: return T;
069: if (c == '\n')
070: return T;
071: return NIL;
072: }
073:
074: public boolean eql(LispObject obj) {
075: if (this == obj)
076: return true;
077: if (obj instanceof LispCharacter) {
078: if (c == ((LispCharacter) obj).c)
079: return true;
080: }
081: return false;
082: }
083:
084: public boolean equal(LispObject obj) {
085: if (this == obj)
086: return true;
087: if (obj instanceof LispCharacter) {
088: if (c == ((LispCharacter) obj).c)
089: return true;
090: }
091: return false;
092: }
093:
094: public boolean equalp(LispObject obj) {
095: if (this == obj)
096: return true;
097: if (obj instanceof LispCharacter) {
098: if (c == ((LispCharacter) obj).c)
099: return true;
100: return Utilities.toLowerCase(c) == Utilities
101: .toLowerCase(((LispCharacter) obj).c);
102: }
103: return false;
104: }
105:
106: public static char getValue(LispObject obj)
107: throws ConditionThrowable {
108: try {
109: return ((LispCharacter) obj).getValue();
110: } catch (ClassCastException e) {
111: throw new ConditionThrowable(
112: new TypeError(obj, "character"));
113: }
114: }
115:
116: public final char getValue() {
117: return c;
118: }
119:
120: public Object javaInstance() {
121: return new Character(c);
122: }
123:
124: public int hashCode() {
125: return c;
126: }
127:
128: public final String toString() {
129: StringBuffer sb = new StringBuffer();
130: if (_PRINT_ESCAPE_.symbolValueNoThrow() != NIL) {
131: sb.append("#\\");
132: switch (c) {
133: case 0:
134: sb.append("Null");
135: break;
136: case '\t':
137: sb.append("Tab");
138: break;
139: case '\n':
140: sb.append("Newline");
141: break;
142: case '\f':
143: sb.append("Page");
144: break;
145: case '\r':
146: sb.append("Return");
147: break;
148: case ' ':
149: sb.append("Space");
150: break;
151: case '\b':
152: sb.append("Backspace");
153: break;
154: default:
155: sb.append(c);
156: break;
157: }
158: } else {
159: sb.append(c);
160: }
161: return sb.toString();
162: }
163:
164: private static final Primitive1 CHARACTER = new Primitive1(
165: "character") {
166: public LispObject execute(LispObject arg)
167: throws ConditionThrowable {
168: if (arg instanceof LispCharacter)
169: return arg;
170: if (arg instanceof LispString) {
171: if (arg.length() == 1)
172: return ((LispString) arg).get(0);
173: } else if (arg instanceof Symbol) {
174: String name = arg.getName();
175: if (name.length() == 1)
176: return new LispCharacter(name.charAt(0));
177: }
178: throw new ConditionThrowable(new TypeError());
179: }
180: };
181:
182: // ### whitespacep
183: private static final Primitive1 WHITESPACEP = new Primitive1(
184: "whitespacep", PACKAGE_SYS, false) {
185: public LispObject execute(LispObject arg)
186: throws ConditionThrowable {
187: LispCharacter character = checkCharacter(arg);
188: return Character.isWhitespace(character.c) ? T : NIL;
189: }
190: };
191: }
|