Added adapters for adapting Java Set, List and Map types to corresponding Python types while converting the args.

Added test for it.
This commit is contained in:
Abhinav Sarkar 2009-11-02 23:35:56 +05:30
parent 906e5222f3
commit f922b79d3e
5 changed files with 109 additions and 12 deletions

View File

@ -24,6 +24,9 @@ class Employee():
if dependent in self._dependents:
self._dependents.remove(dependent)
dependent.set_employee(None)
def set_dependents(self, dependents):
self._dependents = dependents
def __eq__(self, other):
if self == other:
@ -34,7 +37,7 @@ class Employee():
return False
def __str__(self):
return "<Employee: %s %s>" % (self._first_name, self._last_name)
return "<Employee: %s %s (%s)>" % (self._first_name, self._last_name, self.__hash__())
def __hash__(self):
return 31 + hash(self.__class__) + hash(self._first_name) + hash(self._last_name)
@ -60,7 +63,7 @@ class Dependent(object):
return False
def __str__(self):
return "<Dependent: %s %s>" % (self.first_name, self.last_name)
return "<Dependent: %s %s (%s)>" % (self.first_name, self.last_name, self.__hash__())
def __hash__(self):
return 31 + hash(self.__class__) + hash(self.first_name) + hash(self.last_name)

View File

@ -3,17 +3,19 @@ package net.abhinavsarkar.jywrapper.example;
import java.util.Set;
public interface Employee {
public Employee initialize(String firstName, String lastName);
public String getFirstName();
public String getLastName();
public Set<Dependent> getDependents();
public void setDependents(Set<Dependent> dependents);
public void addDependent(Dependent dependent);
public void removeDependent(Dependent dependent);
}

View File

@ -4,6 +4,9 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.HashSet;
import java.util.Set;
import net.abhinavsarkar.jywrapper.JyWrapper;
import org.junit.Test;
@ -49,6 +52,20 @@ public class EmployeeTest {
assertTrue(employee.getDependents().contains(dependent));
}
@Test
public void testSetDependent() {
final Dependent dependent = Dependent_.initialize();
dependent.setFirstName("hawk");
dependent.setLastName("hawkster");
final Employee employee = Employee_.initialize("abhinav", "sarkar");
final Set<Dependent> dependents = new HashSet<Dependent>();
dependents.add(dependent);
employee.setDependents(dependents);
employee.removeDependent(dependent);
assertFalse(employee.getDependents().contains(dependent));
}
@Test
public void testRemoveDependent() {
final Dependent dependent = Dependent_.initialize();

View File

@ -100,7 +100,6 @@ public final class JyWrapper {
}
}
public static void removeFromPythonPath(final URI path) {
final PySystemState pySystemState = Py.getSystemState();
final PyString resolvedPath = Util.resolvePath(path);

View File

@ -5,14 +5,22 @@ import static net.abhinavsarkar.jywrapper.Messages._;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.python.core.Py;
import org.python.core.PyClass;
import org.python.core.PyDictionary;
import org.python.core.PyFunction;
import org.python.core.PyList;
import org.python.core.PyModule;
import org.python.core.PyObject;
import org.python.core.PySet;
import org.python.core.PyString;
import org.python.core.PyType;
import org.python.core.adapter.PyObjectAdapter;
final class Util {
@ -40,16 +48,84 @@ final class Util {
return new PyObject[0];
}
if (args.length == 1) {
return new PyObject[] { Py.java2py(args[0]) };
return new PyObject[] { java2py(args[0]) };
}
final PyObject[] pyArgs = new PyObject[args.length];
for (int i = args.length; --i >= 0;) {
pyArgs[i] = Py.java2py(args[i]);
pyArgs[i] = java2py(args[i]);
}
return pyArgs;
}
private static abstract class InterfaceAdapter implements PyObjectAdapter {
private final Class<?> adaptedInterface;
public InterfaceAdapter(final Class<?> adaptedInterface) {
if (!adaptedInterface.isInterface()) {
throw new IllegalArgumentException(
_("JyWrapper.8", adaptedInterface.getName()));
}
this.adaptedInterface = adaptedInterface;
}
public boolean canAdapt(final Object object) {
return adaptedInterface.isAssignableFrom(object.getClass());
}
}
private static List<InterfaceAdapter> interfaceAdapters = new ArrayList<InterfaceAdapter>();
static {
interfaceAdapters.add(
new InterfaceAdapter(Set.class) {
public PyObject adapt(final Object object) {
final Set<?> set = (Set<?>) object;
final PySet pySet = new PySet();
for (final Object o : set) {
pySet.add(java2py(o));
}
return pySet;
}
});
interfaceAdapters.add(
new InterfaceAdapter(List.class) {
public PyObject adapt(final Object object) {
final List<?> list = (List<?>) object;
final PyList pyList = new PyList();
for (final Object o : list) {
pyList.add(java2py(o));
}
return pyList;
}
});
interfaceAdapters.add(
new InterfaceAdapter(Map.class) {
public PyObject adapt(final Object object) {
final Map<?, ?> map = (Map<?, ?>) object;
final PyDictionary pyDictionary = new PyDictionary();
for (final Map.Entry<?, ?> entry : map.entrySet()) {
pyDictionary.put(
java2py(entry.getKey()),
java2py(entry.getValue()));
}
return pyDictionary;
}
});
}
public static PyObject java2py(final Object object) {
for (final InterfaceAdapter interfaceAdapter : interfaceAdapters) {
if (interfaceAdapter.canAdapt(object)) {
return interfaceAdapter.adapt(object);
}
}
return Py.java2py(object);
}
static String getPyImportName(final PyObject pyObject) {
synchronized (pyObject) {
if (pyObject instanceof PyType) {