KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JavaObject.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: JavaObject.java,v 1.16 2004/06/04 16:18:23 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 class JavaObject extends LispObject
25 {
26     private final Object JavaDoc obj;
27
28     public JavaObject(Object JavaDoc obj)
29     {
30         this.obj = obj;
31     }
32
33     public final Object JavaDoc getObject()
34     {
35         return obj;
36     }
37
38     public Object JavaDoc javaInstance()
39     {
40         return obj;
41     }
42
43     public Object JavaDoc javaInstance(Class JavaDoc c)
44     {
45         return javaInstance();
46     }
47
48     public static final Object JavaDoc getObject(LispObject o) throws ConditionThrowable
49     {
50         try {
51         return ((JavaObject)o).obj;
52         }
53         catch (ClassCastException JavaDoc e) {
54             signal (new TypeError(o, "Java object"));
55             // Not reached.
56
return null;
57         }
58     }
59
60     public final boolean equal(LispObject other)
61     {
62         if (this == other)
63             return true;
64         if (other instanceof JavaObject)
65             return (obj == ((JavaObject)other).obj);
66         return false;
67     }
68
69     public final boolean equalp(LispObject other)
70     {
71         return equal(other);
72     }
73
74     public int sxhash()
75     {
76         return obj == null ? 0 : (obj.hashCode() & 0x7ffffff);
77     }
78
79     public String JavaDoc writeToString()
80     {
81         if (obj instanceof ConditionThrowable)
82             return obj.toString();
83         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("#<JAVAOBJECT ");
84         if (obj == null)
85             sb.append("null");
86         else {
87             sb.append(obj.getClass().getName());
88             sb.append(" @ #x");
89             sb.append(Integer.toHexString(System.identityHashCode(obj)));
90         }
91         sb.append(">");
92         return sb.toString();
93     }
94 }
95
Popular Tags