01: /*
02: * JavaObject.java
03: *
04: * Copyright (C) 2002-2004 Peter Graves
05: * $Id: JavaObject.java,v 1.16 2004/06/04 16:18:23 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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 class JavaObject extends LispObject {
25: private final Object obj;
26:
27: public JavaObject(Object obj) {
28: this .obj = obj;
29: }
30:
31: public final Object getObject() {
32: return obj;
33: }
34:
35: public Object javaInstance() {
36: return obj;
37: }
38:
39: public Object javaInstance(Class c) {
40: return javaInstance();
41: }
42:
43: public static final Object getObject(LispObject o)
44: throws ConditionThrowable {
45: try {
46: return ((JavaObject) o).obj;
47: } catch (ClassCastException e) {
48: signal(new TypeError(o, "Java object"));
49: // Not reached.
50: return null;
51: }
52: }
53:
54: public final boolean equal(LispObject other) {
55: if (this == other)
56: return true;
57: if (other instanceof JavaObject)
58: return (obj == ((JavaObject) other).obj);
59: return false;
60: }
61:
62: public final boolean equalp(LispObject other) {
63: return equal(other);
64: }
65:
66: public int sxhash() {
67: return obj == null ? 0 : (obj.hashCode() & 0x7ffffff);
68: }
69:
70: public String writeToString() {
71: if (obj instanceof ConditionThrowable)
72: return obj.toString();
73: StringBuffer sb = new StringBuffer("#<JAVAOBJECT ");
74: if (obj == null)
75: sb.append("null");
76: else {
77: sb.append(obj.getClass().getName());
78: sb.append(" @ #x");
79: sb
80: .append(Integer.toHexString(System
81: .identityHashCode(obj)));
82: }
83: sb.append(">");
84: return sb.toString();
85: }
86: }
|