Browse Source

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

Added test for it.
Abhinav Sarkar 11 years ago
parent
commit
f922b79d3e

+ 5
- 2
Lib/person.py View File

@@ -24,6 +24,9 @@ class Employee():
24 24
         if dependent in self._dependents:
25 25
             self._dependents.remove(dependent)
26 26
             dependent.set_employee(None)
27
+        
28
+    def set_dependents(self, dependents):
29
+        self._dependents = dependents
27 30
             
28 31
     def __eq__(self, other):
29 32
         if self == other:
@@ -34,7 +37,7 @@ class Employee():
34 37
         return False
35 38
             
36 39
     def __str__(self):
37
-        return "<Employee: %s %s>" % (self._first_name, self._last_name)
40
+        return "<Employee: %s %s (%s)>" % (self._first_name, self._last_name, self.__hash__())
38 41
     
39 42
     def __hash__(self):
40 43
         return 31 + hash(self.__class__) + hash(self._first_name) + hash(self._last_name)
@@ -60,7 +63,7 @@ class Dependent(object):
60 63
         return False
61 64
         
62 65
     def __str__(self):
63
-        return "<Dependent: %s %s>" % (self.first_name, self.last_name)
66
+        return "<Dependent: %s %s (%s)>" % (self.first_name, self.last_name, self.__hash__())
64 67
     
65 68
     def __hash__(self):
66 69
         return 31 + hash(self.__class__) + hash(self.first_name) + hash(self.last_name)

+ 9
- 7
src/example/java/net/abhinavsarkar/jywrapper/example/Employee.java View File

@@ -3,17 +3,19 @@ package net.abhinavsarkar.jywrapper.example;
3 3
 import java.util.Set;
4 4
 
5 5
 public interface Employee {
6
-	
6
+
7 7
 	public Employee initialize(String firstName, String lastName);
8
-	
8
+
9 9
 	public String getFirstName();
10
-	
10
+
11 11
 	public String getLastName();
12
-	
12
+
13 13
 	public Set<Dependent> getDependents();
14
-	
14
+
15
+	public void setDependents(Set<Dependent> dependents);
16
+
15 17
 	public void addDependent(Dependent dependent);
16
-	
18
+
17 19
 	public void removeDependent(Dependent dependent);
18
-	
20
+
19 21
 }

+ 17
- 0
src/example/java/net/abhinavsarkar/jywrapper/example/EmployeeTest.java View File

@@ -4,6 +4,9 @@ import static org.junit.Assert.assertEquals;
4 4
 import static org.junit.Assert.assertFalse;
5 5
 import static org.junit.Assert.assertTrue;
6 6
 
7
+import java.util.HashSet;
8
+import java.util.Set;
9
+
7 10
 import net.abhinavsarkar.jywrapper.JyWrapper;
8 11
 
9 12
 import org.junit.Test;
@@ -49,6 +52,20 @@ public class EmployeeTest {
49 52
 		assertTrue(employee.getDependents().contains(dependent));
50 53
 	}
51 54
 
55
+	@Test
56
+	public void testSetDependent() {
57
+		final Dependent dependent = Dependent_.initialize();
58
+		dependent.setFirstName("hawk");
59
+		dependent.setLastName("hawkster");
60
+		final Employee employee = Employee_.initialize("abhinav", "sarkar");
61
+		final Set<Dependent> dependents = new HashSet<Dependent>();
62
+		dependents.add(dependent);
63
+		employee.setDependents(dependents);
64
+		employee.removeDependent(dependent);
65
+
66
+		assertFalse(employee.getDependents().contains(dependent));
67
+	}
68
+
52 69
 	@Test
53 70
 	public void testRemoveDependent() {
54 71
 		final Dependent dependent = Dependent_.initialize();

+ 0
- 1
src/main/java/net/abhinavsarkar/jywrapper/JyWrapper.java View File

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

+ 78
- 2
src/main/java/net/abhinavsarkar/jywrapper/Util.java View File

@@ -5,14 +5,22 @@ import static net.abhinavsarkar.jywrapper.Messages._;
5 5
 import java.io.File;
6 6
 import java.io.IOException;
7 7
 import java.net.URI;
8
+import java.util.ArrayList;
9
+import java.util.List;
10
+import java.util.Map;
11
+import java.util.Set;
8 12
 
9 13
 import org.python.core.Py;
10 14
 import org.python.core.PyClass;
15
+import org.python.core.PyDictionary;
11 16
 import org.python.core.PyFunction;
17
+import org.python.core.PyList;
12 18
 import org.python.core.PyModule;
13 19
 import org.python.core.PyObject;
20
+import org.python.core.PySet;
14 21
 import org.python.core.PyString;
15 22
 import org.python.core.PyType;
23
+import org.python.core.adapter.PyObjectAdapter;
16 24
 
17 25
 final class Util {
18 26
 
@@ -40,16 +48,84 @@ final class Util {
40 48
 			return new PyObject[0];
41 49
 		}
42 50
 		if (args.length == 1) {
43
-			return new PyObject[] { Py.java2py(args[0]) };
51
+			return new PyObject[] { java2py(args[0]) };
44 52
 		}
45 53
 
46 54
 		final PyObject[] pyArgs = new PyObject[args.length];
47 55
 		for (int i = args.length; --i >= 0;) {
48
-			pyArgs[i] = Py.java2py(args[i]);
56
+			pyArgs[i] = java2py(args[i]);
49 57
 		}
50 58
 		return pyArgs;
51 59
 	}
52 60
 
61
+	private static abstract class InterfaceAdapter implements PyObjectAdapter {
62
+
63
+		private final Class<?> adaptedInterface;
64
+
65
+		public InterfaceAdapter(final Class<?> adaptedInterface) {
66
+			if (!adaptedInterface.isInterface()) {
67
+				throw new IllegalArgumentException(
68
+						_("JyWrapper.8", adaptedInterface.getName()));
69
+			}
70
+			this.adaptedInterface = adaptedInterface;
71
+		}
72
+
73
+		public boolean canAdapt(final Object object) {
74
+			return adaptedInterface.isAssignableFrom(object.getClass());
75
+		}
76
+
77
+	}
78
+
79
+	private static List<InterfaceAdapter> interfaceAdapters = new ArrayList<InterfaceAdapter>();
80
+
81
+	static {
82
+		interfaceAdapters.add(
83
+				new InterfaceAdapter(Set.class) {
84
+					public PyObject adapt(final Object object) {
85
+						final Set<?> set = (Set<?>) object;
86
+						final PySet pySet = new PySet();
87
+						for (final Object o : set) {
88
+							pySet.add(java2py(o));
89
+						}
90
+						return pySet;
91
+					}
92
+				});
93
+		interfaceAdapters.add(
94
+				new InterfaceAdapter(List.class) {
95
+					public PyObject adapt(final Object object) {
96
+						final List<?> list = (List<?>) object;
97
+						final PyList pyList = new PyList();
98
+						for (final Object o : list) {
99
+							pyList.add(java2py(o));
100
+						}
101
+						return pyList;
102
+					}
103
+				});
104
+		interfaceAdapters.add(
105
+				new InterfaceAdapter(Map.class) {
106
+					public PyObject adapt(final Object object) {
107
+						final Map<?, ?> map = (Map<?, ?>) object;
108
+						final PyDictionary pyDictionary = new PyDictionary();
109
+						for (final Map.Entry<?, ?> entry : map.entrySet()) {
110
+							pyDictionary.put(
111
+									java2py(entry.getKey()),
112
+									java2py(entry.getValue()));
113
+						}
114
+						return pyDictionary;
115
+					}
116
+				});
117
+	}
118
+
119
+	public static PyObject java2py(final Object object) {
120
+		for (final InterfaceAdapter interfaceAdapter : interfaceAdapters) {
121
+			if (interfaceAdapter.canAdapt(object)) {
122
+				return interfaceAdapter.adapt(object);
123
+			}
124
+		}
125
+
126
+		return Py.java2py(object);
127
+	}
128
+
53 129
 	static String getPyImportName(final PyObject pyObject) {
54 130
 		synchronized (pyObject) {
55 131
 			if (pyObject instanceof PyType) {

Loading…
Cancel
Save