16
16
17
17
import java .lang .ref .WeakReference ;
18
18
import java .lang .reflect .InvocationTargetException ;
19
+ import java .util .function .Supplier ;
19
20
20
21
import org .ros2 .rcljava .common .JNIUtils ;
21
22
import org .ros2 .rcljava .consumers .Consumer ;
@@ -53,48 +54,28 @@ public class EventHandlerImpl<
53
54
/**
54
55
* Constructor.
55
56
*
56
- * @param parentType The <code>Class</code> type of the parent.
57
- * It can be either a @{link org.ros2.rcljava.Publisher} or a
58
- * @{link org.ros2.rcljava.Subscription} class.
59
57
* @param parentReference A {@link java.lang.ref.WeakReference} to the
60
58
* @{link org.ros2.rcljava.Publisher} or @{link org.ros2.rcljava.Subscription}
61
59
* that created this event handler.
62
60
* @param handle A pointer to the underlying ROS 2 event structure, as an integer.
63
61
* Must not be zero.
64
- * @param eventStatusType The <code>Class</code> of the messages that this
65
- * subscription will receive. We need this because of Java's type erasure,
66
- * which doesn't allow us to use the generic parameter of
67
- * @{link org.ros2.rcljava.Subscription} directly.
62
+ * Ownership of the passed `handle` is taken, and this object is responsible
63
+ * of disposing it. That can be done using @{link EventHandler#dispose()}.
64
+ * @param eventStatusFactory Factory of an event status.
68
65
* @param callback The callback function that will be called when the event
69
66
* is triggered.
70
67
*/
71
68
public EventHandlerImpl (
72
- final Class <ParentT > parentType ,
73
69
final WeakReference <ParentT > parentReference ,
74
70
final long handle ,
75
- final Class <T > eventStatusType ,
71
+ final Supplier <T > eventStatusFactory ,
76
72
final Consumer <T > callback ) {
77
- this .parentType = parentType ;
78
73
this .parentReference = parentReference ;
79
74
this .handle = handle ;
80
- this .eventStatusType = eventStatusType ;
75
+ this .eventStatusFactory = eventStatusFactory ;
81
76
this .callback = callback ;
82
77
}
83
78
84
- /**
85
- * {@inheritDoc}
86
- */
87
- public final Class <T > getEventStatusType () {
88
- return this .eventStatusType ;
89
- }
90
-
91
- /**
92
- * {@inheritDoc}
93
- */
94
- public final Class <ParentT > getParentType () {
95
- return this .parentType ;
96
- }
97
-
98
79
/**
99
80
* {@inheritDoc}
100
81
*/
@@ -120,8 +101,10 @@ public final long getHandle() {
120
101
/**
121
102
* {@inheritDoc}
122
103
*/
123
- public final void dispose () {
124
- nativeDispose (this .handle );
104
+ public synchronized final void dispose () {
105
+ if (this .handle != 0 ) {
106
+ nativeDispose (this .handle );
107
+ }
125
108
this .handle = 0 ;
126
109
}
127
110
@@ -137,28 +120,16 @@ public final void dispose() {
137
120
/**
138
121
* {@inheritDoc}
139
122
*/
140
- public final void executeCallback () {
141
- T eventStatus = null ;
142
- try {
143
- eventStatus = this .eventStatusType .getDeclaredConstructor ().newInstance ();
144
- } catch (NoSuchMethodException nme ) {
145
- nme .printStackTrace ();
146
- } catch (InvocationTargetException ite ) {
147
- ite .printStackTrace ();
148
- } catch (InstantiationException ie ) {
149
- ie .printStackTrace ();
150
- } catch (IllegalAccessException iae ) {
151
- iae .printStackTrace ();
152
- }
123
+ public synchronized final void executeCallback () {
124
+ T eventStatus = eventStatusFactory .get ();
153
125
long nativeEventStatusHandle = eventStatus .allocateRCLStatusEvent ();
154
126
nativeTake (this .handle , nativeEventStatusHandle );
155
127
eventStatus .fromRCLEvent (nativeEventStatusHandle );
156
128
eventStatus .deallocateRCLStatusEvent (nativeEventStatusHandle );
157
129
callback .accept (eventStatus );
158
130
}
159
131
160
- private final Class <T > eventStatusType ;
161
- private final Class <ParentT > parentType ;
132
+ private final Supplier <T > eventStatusFactory ;
162
133
private final WeakReference <ParentT > parentReference ;
163
134
private long handle ;
164
135
private final Consumer <T > callback ;
0 commit comments