Skip to content

Commit c012ba8

Browse files
jacobperronpluris
andauthored
Use array instead of List for ROS message types (#55)
* change logic(list -> array) in msg.java.em * change logic(list -> array) in msg.cpp.em * Support getting and setting as Lists in addition to arrays Signed-off-by: Jacob Perron <[email protected]> * Update rcljava to work with new methods for list types Signed-off-by: Jacob Perron <[email protected]> * Update tests to work with new methods for list types Signed-off-by: Jacob Perron <[email protected]> * Fixes to template files * Avoid nullptr access when converting from Java arrays to C arrays * Make sure Java arrays are initialized Signed-off-by: Jacob Perron <[email protected]> * Minor refactor Signed-off-by: Jacob Perron <[email protected]> * A note about performance to *AsList docblocks Signed-off-by: Jacob Perron <[email protected]> Co-authored-by: pluris <[email protected]>
1 parent 2c00872 commit c012ba8

File tree

11 files changed

+611
-428
lines changed

11 files changed

+611
-428
lines changed

rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ public rcl_interfaces.msg.ListParametersResult listParameters(
781781
rcl_interfaces.msg.ListParametersResult result =
782782
new rcl_interfaces.msg.ListParametersResult();
783783

784+
List<String> resultNames = new ArrayList<String>();
785+
List<String> resultPrefixes = new ArrayList<String>();
784786
synchronized (parametersMutex) {
785787
for (Map.Entry<String, ParameterAndDescriptor> entry : this.parameters.entrySet()) {
786788
boolean getAll =
@@ -805,16 +807,18 @@ public rcl_interfaces.msg.ListParametersResult listParameters(
805807
}
806808

807809
if (getAll || prefixMatches) {
808-
result.getNames().add(entry.getKey());
810+
resultNames.add(entry.getKey());
809811
int lastSeparator = entry.getKey().lastIndexOf(separator);
810812
if (-1 != lastSeparator) {
811813
String prefix = entry.getKey().substring(0, lastSeparator);
812-
if (!result.getPrefixes().contains(prefix)) {
813-
result.getPrefixes().add(prefix);
814+
if (!resultPrefixes.contains(prefix)) {
815+
resultPrefixes.add(prefix);
814816
}
815817
}
816818
}
817819
}
820+
result.setNames(resultNames);
821+
result.setPrefixes(resultPrefixes);
818822
return result;
819823
}
820824
}
@@ -853,7 +857,7 @@ public final Collection<EndpointInfo> getPublishersInfo(final String topicName)
853857

854858
private native static final void nativeGetPublishersInfo(
855859
final long handle, final String topicName, ArrayList<EndpointInfo> endpointInfo);
856-
860+
857861
public final Collection<EndpointInfo> getSubscriptionsInfo(final String topicName) {
858862
ArrayList<EndpointInfo> returnValue = new ArrayList();
859863
nativeGetSubscriptionsInfo(this.handle, topicName, returnValue);

rcljava/src/main/java/org/ros2/rcljava/parameters/ParameterVariant.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,38 +100,38 @@ public ParameterVariant(final String name, final String stringValue) {
100100
this.value.setType(ParameterType.PARAMETER_STRING.getValue());
101101
}
102102

103-
public ParameterVariant(final String name, final Byte[] byteArrayValue) {
103+
public ParameterVariant(final String name, final byte[] byteArrayValue) {
104104
this.name = name;
105105
this.value = new rcl_interfaces.msg.ParameterValue();
106-
this.value.setByteArrayValue(Arrays.asList(byteArrayValue));
106+
this.value.setByteArrayValue(byteArrayValue);
107107
this.value.setType(ParameterType.PARAMETER_BYTE_ARRAY.getValue());
108108
}
109109

110-
public ParameterVariant(final String name, final Boolean[] boolArrayValue) {
110+
public ParameterVariant(final String name, final boolean[] boolArrayValue) {
111111
this.name = name;
112112
this.value = new rcl_interfaces.msg.ParameterValue();
113-
this.value.setBoolArrayValue(Arrays.asList(boolArrayValue));
113+
this.value.setBoolArrayValue(boolArrayValue);
114114
this.value.setType(ParameterType.PARAMETER_BOOL_ARRAY.getValue());
115115
}
116116

117-
public ParameterVariant(final String name, final Long[] integerArrayValue) {
117+
public ParameterVariant(final String name, final long[] integerArrayValue) {
118118
this.name = name;
119119
this.value = new rcl_interfaces.msg.ParameterValue();
120-
this.value.setIntegerArrayValue(Arrays.asList(integerArrayValue));
120+
this.value.setIntegerArrayValue(integerArrayValue);
121121
this.value.setType(ParameterType.PARAMETER_INTEGER_ARRAY.getValue());
122122
}
123123

124-
public ParameterVariant(final String name, final Double[] doubleArrayValue) {
124+
public ParameterVariant(final String name, final double[] doubleArrayValue) {
125125
this.name = name;
126126
this.value = new rcl_interfaces.msg.ParameterValue();
127-
this.value.setDoubleArrayValue(Arrays.asList(doubleArrayValue));
127+
this.value.setDoubleArrayValue(doubleArrayValue);
128128
this.value.setType(ParameterType.PARAMETER_DOUBLE_ARRAY.getValue());
129129
}
130130

131131
public ParameterVariant(final String name, final String[] stringArrayValue) {
132132
this.name = name;
133133
this.value = new rcl_interfaces.msg.ParameterValue();
134-
this.value.setStringArrayValue(Arrays.asList(stringArrayValue));
134+
this.value.setStringArrayValue(stringArrayValue);
135135
this.value.setType(ParameterType.PARAMETER_STRING_ARRAY.getValue());
136136
}
137137

@@ -203,39 +203,39 @@ public final boolean asBool() {
203203
return this.value.getBoolValue();
204204
}
205205

206-
public final Byte[] asByteArray() {
206+
public final byte[] asByteArray() {
207207
if (getType() != ParameterType.PARAMETER_BYTE_ARRAY) {
208208
throw new IllegalArgumentException("Invalid type");
209209
}
210-
return this.value.getByteArrayValue().toArray(new Byte[0]);
210+
return this.value.getByteArrayValue();
211211
}
212212

213-
public final Boolean[] asBooleanArray() {
213+
public final boolean[] asBooleanArray() {
214214
if (getType() != ParameterType.PARAMETER_BOOL_ARRAY) {
215215
throw new IllegalArgumentException("Invalid type");
216216
}
217-
return this.value.getBoolArrayValue().toArray(new Boolean[0]);
217+
return this.value.getBoolArrayValue();
218218
}
219219

220-
public final Long[] asIntegerArray() {
220+
public final long[] asIntegerArray() {
221221
if (getType() != ParameterType.PARAMETER_INTEGER_ARRAY) {
222222
throw new IllegalArgumentException("Invalid type");
223223
}
224-
return this.value.getIntegerArrayValue().toArray(new Long[0]);
224+
return this.value.getIntegerArrayValue();
225225
}
226226

227-
public final Double[] asDoubleArray() {
227+
public final double[] asDoubleArray() {
228228
if (getType() != ParameterType.PARAMETER_DOUBLE_ARRAY) {
229229
throw new IllegalArgumentException("Invalid type");
230230
}
231-
return this.value.getDoubleArrayValue().toArray(new Double[0]);
231+
return this.value.getDoubleArrayValue();
232232
}
233233

234234
public final String[] asStringArray() {
235235
if (getType() != ParameterType.PARAMETER_STRING_ARRAY) {
236236
throw new IllegalArgumentException("Invalid type");
237237
}
238-
return this.value.getStringArrayValue().toArray(new String[0]);
238+
return this.value.getStringArrayValue();
239239
}
240240

241241
public final rcl_interfaces.msg.Parameter toParameter() {
@@ -256,15 +256,15 @@ public static ParameterVariant fromParameter(final rcl_interfaces.msg.Parameter
256256
case rcl_interfaces.msg.ParameterType.PARAMETER_STRING:
257257
return new ParameterVariant(parameter.getName(), parameter.getValue().getStringValue());
258258
case rcl_interfaces.msg.ParameterType.PARAMETER_BYTE_ARRAY:
259-
return new ParameterVariant(parameter.getName(), parameter.getValue().getByteArrayValue().toArray(new Byte[0]));
259+
return new ParameterVariant(parameter.getName(), parameter.getValue().getByteArrayValue());
260260
case rcl_interfaces.msg.ParameterType.PARAMETER_BOOL_ARRAY:
261-
return new ParameterVariant(parameter.getName(), parameter.getValue().getBoolArrayValue().toArray(new Boolean[0]));
261+
return new ParameterVariant(parameter.getName(), parameter.getValue().getBoolArrayValue());
262262
case rcl_interfaces.msg.ParameterType.PARAMETER_INTEGER_ARRAY:
263-
return new ParameterVariant(parameter.getName(), parameter.getValue().getIntegerArrayValue().toArray(new Long[0]));
263+
return new ParameterVariant(parameter.getName(), parameter.getValue().getIntegerArrayValue());
264264
case rcl_interfaces.msg.ParameterType.PARAMETER_DOUBLE_ARRAY:
265-
return new ParameterVariant(parameter.getName(), parameter.getValue().getDoubleArrayValue().toArray(new Double[0]));
265+
return new ParameterVariant(parameter.getName(), parameter.getValue().getDoubleArrayValue());
266266
case rcl_interfaces.msg.ParameterType.PARAMETER_STRING_ARRAY:
267-
return new ParameterVariant(parameter.getName(), parameter.getValue().getStringArrayValue().toArray(new String[0]));
267+
return new ParameterVariant(parameter.getName(), parameter.getValue().getStringArrayValue());
268268
case rcl_interfaces.msg.ParameterType.PARAMETER_NOT_SET:
269269
throw new IllegalArgumentException("Type from ParameterValue is not set");
270270
default:

rcljava/src/main/java/org/ros2/rcljava/parameters/client/AsyncParametersClientImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ public Future<List<ParameterVariant>> getParameters(
114114
request, new Consumer<Future<rcl_interfaces.srv.GetParameters_Response>>() {
115115
public void accept(final Future<rcl_interfaces.srv.GetParameters_Response> future) {
116116
List<ParameterVariant> parameterVariants = new ArrayList<ParameterVariant>();
117-
List<rcl_interfaces.msg.ParameterValue> pvalues = null;
117+
rcl_interfaces.msg.ParameterValue[] pvalues = new rcl_interfaces.msg.ParameterValue[0];
118118
try {
119119
pvalues = future.get().getValues();
120120
} catch (Exception e) {
121121
// TODO(esteve): do something
122122
}
123-
for (int i = 0; i < pvalues.size(); i++) {
123+
for (int i = 0; i < pvalues.length; i++) {
124124
rcl_interfaces.msg.Parameter parameter = new rcl_interfaces.msg.Parameter();
125-
parameter.setName(request.getNames().get(i));
126-
parameter.setValue(pvalues.get(i));
125+
parameter.setName(request.getNames()[i]);
126+
parameter.setValue(pvalues[i]);
127127
parameterVariants.add(ParameterVariant.fromParameter(parameter));
128128
}
129129
futureResult.set(parameterVariants);
@@ -151,13 +151,13 @@ public Future<List<ParameterType>> getParameterTypes(
151151
request, new Consumer<Future<rcl_interfaces.srv.GetParameterTypes_Response>>() {
152152
public void accept(final Future<rcl_interfaces.srv.GetParameterTypes_Response> future) {
153153
List<ParameterType> parameterTypes = new ArrayList<ParameterType>();
154-
List<Byte> pts = null;
154+
byte[] pts = new byte[0];
155155
try {
156156
pts = future.get().getTypes();
157157
} catch (Exception e) {
158158
// TODO(esteve): do something
159159
}
160-
for (Byte pt : pts) {
160+
for (byte pt : pts) {
161161
parameterTypes.add(ParameterType.fromByte(pt));
162162
}
163163
futureResult.set(parameterTypes);
@@ -194,7 +194,7 @@ public Future<List<rcl_interfaces.msg.SetParametersResult>> setParameters(
194194
public void accept(final Future<rcl_interfaces.srv.SetParameters_Response> future) {
195195
List<rcl_interfaces.msg.SetParametersResult> setParametersResult = null;
196196
try {
197-
setParametersResult = future.get().getResults();
197+
setParametersResult = future.get().getResultsAsList();
198198
} catch (Exception e) {
199199
// TODO(esteve): do something
200200
}
@@ -297,7 +297,7 @@ public Future<List<rcl_interfaces.msg.ParameterDescriptor>> describeParameters(
297297
public void accept(final Future<rcl_interfaces.srv.DescribeParameters_Response> future) {
298298
List<rcl_interfaces.msg.ParameterDescriptor> parameterDescriptors = null;
299299
try {
300-
parameterDescriptors = future.get().getDescriptors();
300+
parameterDescriptors = future.get().getDescriptorsAsList();
301301
} catch (Exception e) {
302302
// TODO(esteve): do something
303303
}

rcljava/src/main/java/org/ros2/rcljava/parameters/service/ParameterServiceImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ParameterServiceImpl(final Node node, final QoSProfile qosProfile)
4848
public void accept(RMWRequestId rmwRequestId,
4949
rcl_interfaces.srv.GetParameters_Request request,
5050
rcl_interfaces.srv.GetParameters_Response response) {
51-
List<ParameterVariant> values = node.getParameters(request.getNames());
51+
List<ParameterVariant> values = node.getParameters(request.getNamesAsList());
5252
List<rcl_interfaces.msg.ParameterValue> pvalues =
5353
new ArrayList<rcl_interfaces.msg.ParameterValue>();
5454
for (ParameterVariant pvariant : values) {
@@ -67,7 +67,7 @@ public void accept(RMWRequestId rmwRequestId,
6767
public void accept(RMWRequestId rmwRequestId,
6868
rcl_interfaces.srv.GetParameterTypes_Request request,
6969
rcl_interfaces.srv.GetParameterTypes_Response response) {
70-
List<ParameterType> types = node.getParameterTypes(request.getNames());
70+
List<ParameterType> types = node.getParameterTypes(request.getNamesAsList());
7171
List<Byte> ptypes = new ArrayList<Byte>();
7272
for (ParameterType type : types) {
7373
ptypes.add(type.getValue());
@@ -124,7 +124,7 @@ public void accept(RMWRequestId rmwRequestId,
124124
rcl_interfaces.srv.DescribeParameters_Request request,
125125
rcl_interfaces.srv.DescribeParameters_Response response) {
126126
List<rcl_interfaces.msg.ParameterDescriptor> descriptors =
127-
node.describeParameters(request.getNames());
127+
node.describeParameters(request.getNamesAsList());
128128
response.setDescriptors(descriptors);
129129
}
130130
},
@@ -139,7 +139,7 @@ public void accept(RMWRequestId rmwRequestId,
139139
rcl_interfaces.srv.ListParameters_Request request,
140140
rcl_interfaces.srv.ListParameters_Response response) {
141141
rcl_interfaces.msg.ListParametersResult result =
142-
node.listParameters(request.getPrefixes(), request.getDepth());
142+
node.listParameters(request.getPrefixesAsList(), request.getDepth());
143143
response.setResult(result);
144144
}
145145
},

rcljava/src/test/java/org/ros2/rcljava/node/NodeParametersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public final void testListParameters() {
338338

339339
rcl_interfaces.msg.ListParametersResult result = node.listParameters(new ArrayList<String>(), rcl_interfaces.srv.ListParameters_Request.DEPTH_RECURSIVE);
340340
// 2 we added above plus the implicit "use_sim_time"
341-
assertEquals(3, result.getNames().size());
341+
assertEquals(3, result.getNames().length);
342342
}
343343

344344
@Test

0 commit comments

Comments
 (0)