From: <cg...@us...> - 2007-07-15 08:26:12
|
Revision: 3305 http://svn.sourceforge.net/jython/?rev=3305&view=rev Author: cgroves Date: 2007-07-15 01:26:10 -0700 (Sun, 15 Jul 2007) Log Message: ----------- Remove support for ancient JVMs in IdImpl1 and move IdImpl2 into IdImpl as the default Modified Paths: -------------- branches/2.3/src/org/python/core/IdImpl.java branches/2.3/src/org/python/core/Py.java Removed Paths: ------------- branches/2.3/src/org/python/core/IdImpl1.java branches/2.3/src/org/python/core/IdImpl2.java Modified: branches/2.3/src/org/python/core/IdImpl.java =================================================================== --- branches/2.3/src/org/python/core/IdImpl.java 2007-07-15 08:23:52 UTC (rev 3304) +++ branches/2.3/src/org/python/core/IdImpl.java 2007-07-15 08:26:10 UTC (rev 3305) @@ -1,26 +1,91 @@ package org.python.core; -public abstract class IdImpl { +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; +import java.util.HashMap; - public static IdImpl getInstance() { - if (System.getProperty("java.version").compareTo("1.2") >= 0) { - try { - return (IdImpl) Class.forName("org.python.core.IdImpl2") - .newInstance(); - } catch (Throwable e) { - return null; +public class IdImpl { + + public static class WeakIdentityMap { + + private ReferenceQueue refqueue = new ReferenceQueue(); + private HashMap hashmap = new HashMap(); + + private void cleanup() { + Object k; + while ((k = this.refqueue.poll()) != null) { + this.hashmap.remove(k); } - } else { - return new IdImpl1(); } + + private class WeakIdKey extends WeakReference { + private int hashcode; + + WeakIdKey(Object obj) { + super(obj,WeakIdentityMap.this.refqueue); + this.hashcode = System.identityHashCode(obj); + } + + public int hashCode() { + return this.hashcode; + } + + public boolean equals(Object other) { + Object obj = this.get(); + if (obj != null) { + return obj == ((WeakIdKey)other).get(); + } else { + return this == other; + } + } + } + + public int _internal_map_size() { + return this.hashmap.size(); + } + + public void put(Object key,Object val) { + cleanup(); + this.hashmap.put(new WeakIdKey(key),val); + } + + public Object get(Object key) { + cleanup(); + return this.hashmap.get(new WeakIdKey(key)); + } + public void remove(Object key) { + cleanup(); + this.hashmap.remove(new WeakIdKey(key)); + } + } - public abstract long id(PyObject o); + private WeakIdentityMap id_map = new WeakIdentityMap(); + private long sequential_id = 0; - public abstract String idstr(PyObject o); + public long id(PyObject o) { + if (o instanceof PyJavaInstance) { + return java_obj_id(((PyJavaInstance)o).javaProxy); + } else { + return java_obj_id(o); + } + } - // o should not be an instance of a subclass of PyObject - public abstract long java_obj_id(Object o); + // XXX maybe should display both this id and identityHashCode + // XXX preserve the old "at ###" style? + public String idstr(PyObject o) { + return Long.toString(id(o)); + } + public synchronized long java_obj_id(Object o) { + Long cand = (Long)this.id_map.get(o); + if (cand == null) { + this.sequential_id++; + long new_id = this.sequential_id; + this.id_map.put(o,new Long(new_id)); + return new_id; + } + return cand.longValue(); + } } Deleted: branches/2.3/src/org/python/core/IdImpl1.java =================================================================== --- branches/2.3/src/org/python/core/IdImpl1.java 2007-07-15 08:23:52 UTC (rev 3304) +++ branches/2.3/src/org/python/core/IdImpl1.java 2007-07-15 08:26:10 UTC (rev 3305) @@ -1,26 +0,0 @@ -package org.python.core; - -/** - * - * @deprecated Java1 no longer supported. - * - */ -public class IdImpl1 extends IdImpl { - - public long id(PyObject o) { - if (o instanceof PyJavaInstance) { - return System.identityHashCode(((PyJavaInstance) o).javaProxy); - } else { - return System.identityHashCode(o); - } - } - - public String idstr(PyObject o) { - return Long.toString(id(o)); - } - - public long java_obj_id(Object o) { - return System.identityHashCode(o); - } - -} Deleted: branches/2.3/src/org/python/core/IdImpl2.java =================================================================== --- branches/2.3/src/org/python/core/IdImpl2.java 2007-07-15 08:23:52 UTC (rev 3304) +++ branches/2.3/src/org/python/core/IdImpl2.java 2007-07-15 08:26:10 UTC (rev 3305) @@ -1,92 +0,0 @@ -package org.python.core; - -import java.lang.ref.WeakReference; -import java.lang.ref.ReferenceQueue; -import java.util.HashMap; - -public class IdImpl2 extends IdImpl { - - public static class WeakIdentityMap { - - private ReferenceQueue refqueue = new ReferenceQueue(); - private HashMap hashmap = new HashMap(); - - private void cleanup() { - Object k; - while ((k = this.refqueue.poll()) != null) { - this.hashmap.remove(k); - } - } - - private class WeakIdKey extends WeakReference { - private int hashcode; - - WeakIdKey(Object obj) { - super(obj,WeakIdentityMap.this.refqueue); - this.hashcode = System.identityHashCode(obj); - } - - public int hashCode() { - return this.hashcode; - } - - public boolean equals(Object other) { - Object obj = this.get(); - if (obj != null) { - return obj == ((WeakIdKey)other).get(); - } else { - return this == other; - } - } - } - - public int _internal_map_size() { - return this.hashmap.size(); - } - - public void put(Object key,Object val) { - cleanup(); - this.hashmap.put(new WeakIdKey(key),val); - } - - public Object get(Object key) { - cleanup(); - return this.hashmap.get(new WeakIdKey(key)); - } - - public void remove(Object key) { - cleanup(); - this.hashmap.remove(new WeakIdKey(key)); - } - - } - - private WeakIdentityMap id_map = new WeakIdentityMap(); - private long sequential_id = 0; - - public long id(PyObject o) { - if (o instanceof PyJavaInstance) { - return java_obj_id(((PyJavaInstance)o).javaProxy); - } else { - return java_obj_id(o); - } - } - - // XXX maybe should display both this id and identityHashCode - // XXX preserve the old "at ###" style? - public String idstr(PyObject o) { - return Long.toString(id(o)); - } - - public synchronized long java_obj_id(Object o) { - Long cand = (Long)this.id_map.get(o); - if (cand == null) { - this.sequential_id++; - long new_id = this.sequential_id; - this.id_map.put(o,new Long(new_id)); - return new_id; - } - return cand.longValue(); - } - -} Modified: branches/2.3/src/org/python/core/Py.java =================================================================== --- branches/2.3/src/org/python/core/Py.java 2007-07-15 08:23:52 UTC (rev 3304) +++ branches/2.3/src/org/python/core/Py.java 2007-07-15 08:26:10 UTC (rev 3305) @@ -1828,7 +1828,7 @@ } } - private static IdImpl idimpl = IdImpl.getInstance(); + private static IdImpl idimpl = new IdImpl(); public static long id(PyObject o) { return idimpl.id(o); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |