Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use array instead of List for ROS message types #165

Open
wants to merge 8 commits into
base: dashing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ public rcl_interfaces.msg.ListParametersResult listParameters(
new rcl_interfaces.msg.ListParametersResult();

String separator = ".";
List<String> resultNames = new ArrayList<String>();
List<String> resultPrefixes = new ArrayList<String>();
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
for (Map.Entry<String, ParameterVariant> entry : this.parameters.entrySet()) {
boolean getAll =
(prefixes.size() == 0)
Expand All @@ -500,16 +502,18 @@ public rcl_interfaces.msg.ListParametersResult listParameters(
}
}
if (getAll || prefixMatches) {
result.getNames().add(entry.getKey());
resultNames.add(entry.getKey());
int lastSeparator = entry.getKey().lastIndexOf(separator);
if (-1 != lastSeparator) {
String prefix = entry.getKey().substring(0, lastSeparator);
if (!result.getPrefixes().contains(prefix)) {
result.getPrefixes().add(prefix);
if (!resultPrefixes.contains(prefix)) {
resultPrefixes.add(prefix);
}
}
}
}
result.setNames(resultNames);
result.setPrefixes(resultPrefixes);
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public ParameterVariant(final String name, final String stringValue) {
this.value.setType(ParameterType.PARAMETER_STRING.getValue());
}

public ParameterVariant(final String name, final List<Byte> byteArrayValue) {
public ParameterVariant(final String name, final byte[] byteArrayValue) {
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
this.name = name;
this.value = new rcl_interfaces.msg.ParameterValue();
this.value.setByteArrayValue(byteArrayValue);
Expand Down Expand Up @@ -161,7 +161,7 @@ public final boolean asBool() {
return this.value.getBoolValue();
}

public final List<Byte> asByteArray() {
public final byte[] asByteArray() {
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
if (getType() != ParameterType.PARAMETER_BYTE_ARRAY) {
throw new IllegalArgumentException("Invalid type");
}
Expand Down Expand Up @@ -194,4 +194,4 @@ public static ParameterVariant fromParameter(final rcl_interfaces.msg.Parameter
"Unexpected type from ParameterVariant: " + parameter.getValue().getType());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ public Future<List<ParameterVariant>> getParameters(
request, new Consumer<Future<rcl_interfaces.srv.GetParameters_Response>>() {
public void accept(final Future<rcl_interfaces.srv.GetParameters_Response> future) {
List<ParameterVariant> parameterVariants = new ArrayList<ParameterVariant>();
List<rcl_interfaces.msg.ParameterValue> pvalues = null;
rcl_interfaces.msg.ParameterValue[] pvalues = new rcl_interfaces.msg.ParameterValue[0];
try {
pvalues = future.get().getValues();
} catch (Exception e) {
// TODO(esteve): do something
}
for (int i = 0; i < pvalues.size(); i++) {
for (int i = 0; i < pvalues.length; i++) {
rcl_interfaces.msg.Parameter parameter = new rcl_interfaces.msg.Parameter();
parameter.setName(request.getNames().get(i));
parameter.setValue(pvalues.get(i));
parameter.setName(request.getNames()[i]);
parameter.setValue(pvalues[i]);
parameterVariants.add(ParameterVariant.fromParameter(parameter));
}
futureResult.set(parameterVariants);
Expand Down Expand Up @@ -151,13 +151,13 @@ public Future<List<ParameterType>> getParameterTypes(
request, new Consumer<Future<rcl_interfaces.srv.GetParameterTypes_Response>>() {
public void accept(final Future<rcl_interfaces.srv.GetParameterTypes_Response> future) {
List<ParameterType> parameterTypes = new ArrayList<ParameterType>();
List<Byte> pts = null;
byte[] pts = new byte[0];
try {
pts = future.get().getTypes();
} catch (Exception e) {
// TODO(esteve): do something
}
for (Byte pt : pts) {
for (byte pt : pts) {
parameterTypes.add(ParameterType.fromByte(pt));
}
futureResult.set(parameterTypes);
Expand Down Expand Up @@ -194,7 +194,7 @@ public Future<List<rcl_interfaces.msg.SetParametersResult>> setParameters(
public void accept(final Future<rcl_interfaces.srv.SetParameters_Response> future) {
List<rcl_interfaces.msg.SetParametersResult> setParametersResult = null;
try {
setParametersResult = future.get().getResults();
setParametersResult = future.get().getResultsAsList();
} catch (Exception e) {
// TODO(esteve): do something
}
Expand Down Expand Up @@ -297,7 +297,7 @@ public Future<List<rcl_interfaces.msg.ParameterDescriptor>> describeParameters(
public void accept(final Future<rcl_interfaces.srv.DescribeParameters_Response> future) {
List<rcl_interfaces.msg.ParameterDescriptor> parameterDescriptors = null;
try {
parameterDescriptors = future.get().getDescriptors();
parameterDescriptors = future.get().getDescriptorsAsList();
} catch (Exception e) {
// TODO(esteve): do something
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ParameterServiceImpl(final Node node, final QoSProfile qosProfile)
public void accept(RMWRequestId rmwRequestId,
rcl_interfaces.srv.GetParameters_Request request,
rcl_interfaces.srv.GetParameters_Response response) {
List<ParameterVariant> values = node.getParameters(request.getNames());
List<ParameterVariant> values = node.getParameters(request.getNamesAsList());
List<rcl_interfaces.msg.ParameterValue> pvalues =
new ArrayList<rcl_interfaces.msg.ParameterValue>();
for (ParameterVariant pvariant : values) {
Expand All @@ -67,7 +67,7 @@ public void accept(RMWRequestId rmwRequestId,
public void accept(RMWRequestId rmwRequestId,
rcl_interfaces.srv.GetParameterTypes_Request request,
rcl_interfaces.srv.GetParameterTypes_Response response) {
List<ParameterType> types = node.getParameterTypes(request.getNames());
List<ParameterType> types = node.getParameterTypes(request.getNamesAsList());
List<Byte> ptypes = new ArrayList<Byte>();
for (ParameterType type : types) {
ptypes.add(type.getValue());
Expand Down Expand Up @@ -124,7 +124,7 @@ public void accept(RMWRequestId rmwRequestId,
rcl_interfaces.srv.DescribeParameters_Request request,
rcl_interfaces.srv.DescribeParameters_Response response) {
List<rcl_interfaces.msg.ParameterDescriptor> descriptors =
node.describeParameters(request.getNames());
node.describeParameters(request.getNamesAsList());
response.setDescriptors(descriptors);
}
},
Expand All @@ -139,7 +139,7 @@ public void accept(RMWRequestId rmwRequestId,
rcl_interfaces.srv.ListParameters_Request request,
rcl_interfaces.srv.ListParameters_Response response) {
rcl_interfaces.msg.ListParametersResult result =
node.listParameters(request.getPrefixes(), request.getDepth());
node.listParameters(request.getPrefixesAsList(), request.getDepth());
response.setResult(result);
}
},
Expand Down
102 changes: 51 additions & 51 deletions rcljava/src/test/java/org/ros2/rcljava/node/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ public final void testPubSubBoundedArrayNested() throws Exception {
rcljava.msg.BoundedArrayNested value = future.get();
assertNotEquals(null, value.getPrimitiveValues());

rcljava.msg.Primitives primitivesValue1 = value.getPrimitiveValues().get(0);
rcljava.msg.Primitives primitivesValue2 = value.getPrimitiveValues().get(1);
rcljava.msg.Primitives primitivesValue1 = value.getPrimitiveValues()[0];
rcljava.msg.Primitives primitivesValue2 = value.getPrimitiveValues()[1];

assertTrue(checkPrimitives(primitivesValue1, boolValue1, byteValue1, charValue1, float32Value1,
float64Value1, int8Value1, uint8Value1, int16Value1, uint16Value1, int32Value1,
Expand Down Expand Up @@ -322,20 +322,20 @@ public final void testPubSubBoundedArrayPrimitives() throws Exception {

rcljava.msg.BoundedArrayPrimitives value = future.get();

assertEquals(boolValues, value.getBoolValues());
assertEquals(byteValues, value.getByteValues());
assertEquals(charValues, value.getCharValues());
assertEquals(float32Values, value.getFloat32Values());
assertEquals(float64Values, value.getFloat64Values());
assertEquals(int8Values, value.getInt8Values());
assertEquals(uint8Values, value.getUint8Values());
assertEquals(int16Values, value.getInt16Values());
assertEquals(uint16Values, value.getUint16Values());
assertEquals(int32Values, value.getInt32Values());
assertEquals(uint32Values, value.getUint32Values());
assertEquals(int64Values, value.getInt64Values());
assertEquals(uint64Values, value.getUint64Values());
assertEquals(stringValues, value.getStringValues());
assertEquals(boolValues, value.getBoolValuesAsList());
assertEquals(byteValues, value.getByteValuesAsList());
assertEquals(charValues, value.getCharValuesAsList());
assertEquals(float32Values, value.getFloat32ValuesAsList());
assertEquals(float64Values, value.getFloat64ValuesAsList());
assertEquals(int8Values, value.getInt8ValuesAsList());
assertEquals(uint8Values, value.getUint8ValuesAsList());
assertEquals(int16Values, value.getInt16ValuesAsList());
assertEquals(uint16Values, value.getUint16ValuesAsList());
assertEquals(int32Values, value.getInt32ValuesAsList());
assertEquals(uint32Values, value.getUint32ValuesAsList());
assertEquals(int64Values, value.getInt64ValuesAsList());
assertEquals(uint64Values, value.getUint64ValuesAsList());
assertEquals(stringValues, value.getStringValuesAsList());

publisher.dispose();
assertEquals(0, publisher.getHandle());
Expand Down Expand Up @@ -414,8 +414,8 @@ public final void testPubSubDynamicArrayNested() throws Exception {
rcljava.msg.DynamicArrayNested value = future.get();
assertNotEquals(null, value.getPrimitiveValues());

rcljava.msg.Primitives primitivesValue1 = value.getPrimitiveValues().get(0);
rcljava.msg.Primitives primitivesValue2 = value.getPrimitiveValues().get(1);
rcljava.msg.Primitives primitivesValue1 = value.getPrimitiveValues()[0];
rcljava.msg.Primitives primitivesValue2 = value.getPrimitiveValues()[1];

assertTrue(checkPrimitives(primitivesValue1, boolValue1, byteValue1, charValue1, float32Value1,
float64Value1, int8Value1, uint8Value1, int16Value1, uint16Value1, int32Value1,
Expand Down Expand Up @@ -484,20 +484,20 @@ public final void testPubSubDynamicArrayPrimitives() throws Exception {

rcljava.msg.DynamicArrayPrimitives value = future.get();

assertEquals(boolValues, value.getBoolValues());
assertEquals(byteValues, value.getByteValues());
assertEquals(charValues, value.getCharValues());
assertEquals(float32Values, value.getFloat32Values());
assertEquals(float64Values, value.getFloat64Values());
assertEquals(int8Values, value.getInt8Values());
assertEquals(uint8Values, value.getUint8Values());
assertEquals(int16Values, value.getInt16Values());
assertEquals(uint16Values, value.getUint16Values());
assertEquals(int32Values, value.getInt32Values());
assertEquals(uint32Values, value.getUint32Values());
assertEquals(int64Values, value.getInt64Values());
assertEquals(uint64Values, value.getUint64Values());
assertEquals(stringValues, value.getStringValues());
assertEquals(boolValues, value.getBoolValuesAsList());
assertEquals(byteValues, value.getByteValuesAsList());
assertEquals(charValues, value.getCharValuesAsList());
assertEquals(float32Values, value.getFloat32ValuesAsList());
assertEquals(float64Values, value.getFloat64ValuesAsList());
assertEquals(int8Values, value.getInt8ValuesAsList());
assertEquals(uint8Values, value.getUint8ValuesAsList());
assertEquals(int16Values, value.getInt16ValuesAsList());
assertEquals(uint16Values, value.getUint16ValuesAsList());
assertEquals(int32Values, value.getInt32ValuesAsList());
assertEquals(uint32Values, value.getUint32ValuesAsList());
assertEquals(int64Values, value.getInt64ValuesAsList());
assertEquals(uint64Values, value.getUint64ValuesAsList());
assertEquals(stringValues, value.getStringValuesAsList());

publisher.dispose();
assertEquals(0, publisher.getHandle());
Expand Down Expand Up @@ -668,12 +668,12 @@ public final void testPubSubStaticArrayNested() throws Exception {
rcljava.msg.StaticArrayNested value = future.get();
assertNotEquals(null, value.getPrimitiveValues());

assertEquals(4, value.getPrimitiveValues().size());
assertEquals(4, value.getPrimitiveValues().length);

rcljava.msg.Primitives primitivesValue1 = value.getPrimitiveValues().get(0);
rcljava.msg.Primitives primitivesValue2 = value.getPrimitiveValues().get(1);
rcljava.msg.Primitives primitivesValue3 = value.getPrimitiveValues().get(2);
rcljava.msg.Primitives primitivesValue4 = value.getPrimitiveValues().get(3);
rcljava.msg.Primitives primitivesValue1 = value.getPrimitiveValues()[0];
rcljava.msg.Primitives primitivesValue2 = value.getPrimitiveValues()[1];
rcljava.msg.Primitives primitivesValue3 = value.getPrimitiveValues()[2];
rcljava.msg.Primitives primitivesValue4 = value.getPrimitiveValues()[3];

assertTrue(checkPrimitives(primitivesValue1, boolValue1, byteValue1, charValue1, float32Value1,
float64Value1, int8Value1, uint8Value1, int16Value1, uint16Value1, int32Value1,
Expand Down Expand Up @@ -751,20 +751,20 @@ public final void testPubSubStaticArrayPrimitives() throws Exception {

rcljava.msg.StaticArrayPrimitives value = future.get();

assertEquals(boolValues, value.getBoolValues());
assertEquals(byteValues, value.getByteValues());
assertEquals(charValues, value.getCharValues());
assertEquals(float32Values, value.getFloat32Values());
assertEquals(float64Values, value.getFloat64Values());
assertEquals(int8Values, value.getInt8Values());
assertEquals(uint8Values, value.getUint8Values());
assertEquals(int16Values, value.getInt16Values());
assertEquals(uint16Values, value.getUint16Values());
assertEquals(int32Values, value.getInt32Values());
assertEquals(uint32Values, value.getUint32Values());
assertEquals(int64Values, value.getInt64Values());
assertEquals(uint64Values, value.getUint64Values());
assertEquals(stringValues, value.getStringValues());
assertEquals(boolValues, value.getBoolValuesAsList());
assertEquals(byteValues, value.getByteValuesAsList());
assertEquals(charValues, value.getCharValuesAsList());
assertEquals(float32Values, value.getFloat32ValuesAsList());
assertEquals(float64Values, value.getFloat64ValuesAsList());
assertEquals(int8Values, value.getInt8ValuesAsList());
assertEquals(uint8Values, value.getUint8ValuesAsList());
assertEquals(int16Values, value.getInt16ValuesAsList());
assertEquals(uint16Values, value.getUint16ValuesAsList());
assertEquals(int32Values, value.getInt32ValuesAsList());
assertEquals(uint32Values, value.getUint32ValuesAsList());
assertEquals(int64Values, value.getInt64ValuesAsList());
assertEquals(uint64Values, value.getUint64ValuesAsList());
assertEquals(stringValues, value.getStringValuesAsList());

publisher.dispose();
assertEquals(0, publisher.getHandle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.ros2.rcljava.parameters;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -153,8 +154,8 @@ public final void testListParameters() throws Exception {
parametersClient.listParameters(
Arrays.asList(new String[] {"foo", "bar"}), 10, new TestConsumer(future));

assertEquals(Arrays.asList(new String[] {"foo.first", "foo.second"}), future.get().getNames());
assertEquals(Arrays.asList(new String[] {"foo"}), future.get().getPrefixes());
assertArrayEquals(new String[] {"foo.first", "foo.second"}, future.get().getNames());
assertArrayEquals(new String[] {"foo"}, future.get().getPrefixes());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.ros2.rcljava.parameters;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -130,9 +131,9 @@ public final void testListParameters() throws Exception {
rcl_interfaces.msg.ListParametersResult parametersAndPrefixes =
parametersClient.listParameters(Arrays.asList(new String[] {"foo", "bar"}), 10);

assertEquals(
parametersAndPrefixes.getNames(), Arrays.asList(new String[] {"foo.first", "foo.second"}));
assertEquals(parametersAndPrefixes.getPrefixes(), Arrays.asList(new String[] {"foo"}));
assertArrayEquals(
parametersAndPrefixes.getNames(), new String[] {"foo.first", "foo.second"});
assertEquals(parametersAndPrefixes.getPrefixes(), new String[] {"foo"});
}

@Test
Expand Down
Loading