Differences between revisions 4 and 5
Revision 4 as of 2005-02-13 00:21:32
Size: 2827
Editor: ClarkUpdike
Comment:
Revision 5 as of 2005-02-13 00:38:41
Size: 2719
Editor: ClarkUpdike
Comment:
Deletions are marked like this. Additions are marked like this.
Line 41: Line 41:
  Sets: Sets were introduced in cpython 2.3, so this is a new implementation for jython 2.2. I'll assume that the specification derives from the [http://python.org/doc/2.3.3/lib/set-objects.html sets module] in cpython 2.3.[[BR]]

[http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html java.util.Collections]: Is there a need to provide a jython version of this class (especially the synchronized and unmodifiable wrapper methods)? If it is not implemented and the java.util.Collections wrappers are used on new collection objects, the returned objects will only proxy for the java.util interface and will be broken in jython.
  [http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html java.util.Collections]: Is there a need to provide a jython version of this class (especially the synchronized and unmodifiable wrapper methods)? If it is not implemented and the java.util.Collections wrappers are used on new collection objects, the returned objects will only proxy for the java.util interface and will be broken in jython.  There is also a Py`Immutable`Set in BrianZimmer's Sets module, but I don't think there is an immutable dictionary equivalent.

Background

Currently, the support for Java Collections integration is a one-way street. It's possible to make Collections object act as a PyObject but it's not possible to make a PyObject act as a Collection.

The integration of Collections into Jython happens through the CollectionProxy and CollectionProxy2 classes. They wrap the Collection instance with the appropriate proxy and delegate Jython calls to the Collection instance.

Going the other way fails. Take for example:

  •     >>> from java.util import ArrayList
        >>> a = ArrayList([1,2,3])
        Traceback (innermost last):
          File "<console>", line 1, in ?
        TypeError: java.util.ArrayList(): 1st arg can't be coerced to java.util.Collection or int

In this example the ArrayList constructor is expecting a java.util.Collection instance but since the PyList does not implement this interface the TypeError is thrown. Since the Collection framework is fundamental to Java since 1.2 the JythonDevelopmentTeam will address this issue. The work is currently being managed by ClarkUpdike.

Design

There are two different approaches:

  1. Subclass the Abstract classes available in java.util for the Collection framework.
  2. Continuing subclassing PyObject with the additional work of implementing the appropriate interface.

Approach 2 offers the best integration options. Jython is primarily an implementation of Python so implementing the data structures as they are in Python takes priority over the Java implementations. In addition, the keyword and index arguments for method calls are already done. The implementation of the interfaces will need only delegate to the appropriate PyObject instance's method for the same functionality.

Jython Class

Extends

Implements

PySequence

PyObject

List (Collection)

PyArray

PySequence

PyList

PySequence

PyString

PySequence

PyTuple

PySequence

PyXRange

PySequence

PySet

PyObject

Set

PyDictionary

PyObject

Map

Discussion

  • ClarkUpdike Feb 12 2005

    • [http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html java.util.Collections]: Is there a need to provide a jython version of this class (especially the synchronized and unmodifiable wrapper methods)? If it is not implemented and the java.util.Collections wrappers are used on new collection objects, the returned objects will only proxy for the java.util interface and will be broken in jython. There is also a PyImmutableSet in BrianZimmer's Sets module, but I don't think there is an immutable dictionary equivalent.

CollectionsIntegration (last edited 2008-11-15 09:15:59 by localhost)