Skip to content
This repository has been archived by the owner on Jan 14, 2023. It is now read-only.

Storing fields in MessageFields as indexed List instead of multiple Maps #51

Open
wants to merge 1 commit into
base: obsolete/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ public class MessageContext {
private final Map<String, String> fieldSetterNames;
private final List<String> fieldNames;

private final Map<String, Integer> fieldIndexes;
private final Map<String, Integer> fieldGetterIndexes;
private final Map<String, Integer> fieldSetterIndexes;

public MessageContext(MessageDeclaration messageDeclaration, MessageFactory messageFactory) {
this.messageDeclaration = messageDeclaration;
this.messageFactory = messageFactory;
this.fieldFactories = Maps.newHashMap();
this.fieldGetterNames = Maps.newHashMap();
this.fieldSetterNames = Maps.newHashMap();
this.fieldNames = Lists.newArrayList();
this.fieldIndexes = Maps.newHashMap();
this.fieldGetterIndexes = Maps.newHashMap();
this.fieldSetterIndexes = Maps.newHashMap();
}

public MessageFactory getMessageFactory() {
Expand Down Expand Up @@ -79,9 +86,16 @@ public String getDefinition() {

public void addFieldFactory(String name, FieldFactory fieldFactory) {
fieldFactories.put(name, fieldFactory);
fieldGetterNames.put(name, "get" + getJavaName(name));
fieldSetterNames.put(name, "set" + getJavaName(name));
String javaName = getJavaName(name);
String getter = "get" + javaName;
String setter = "set" + javaName;
fieldGetterNames.put(name, getter);
fieldSetterNames.put(name, setter);
fieldNames.add(name);
int index = fieldIndexes.size();
fieldIndexes.put(name, index);
fieldGetterIndexes.put(getter, index);
fieldSetterIndexes.put(setter, index);
}

private String getJavaName(String name) {
Expand Down Expand Up @@ -110,6 +124,18 @@ public FieldFactory getFieldFactory(String name) {
return fieldFactories.get(name);
}

public Integer getFieldIndexByName(String name) {
return fieldIndexes.get(name);
}

public Integer getFieldIndexByGetterName(String name) {
return fieldGetterIndexes.get(name);
}

public Integer getFieldIndexBySetterName(String name) {
return fieldSetterIndexes.get(name);
}

/**
* @return a {@link List} of field names in the order they were added
*/
Expand Down Expand Up @@ -141,4 +167,5 @@ public boolean equals(Object obj) {
return false;
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,62 @@

package org.ros.internal.message.field;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.ros.exception.RosMessageRuntimeException;
import org.ros.internal.message.context.MessageContext;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* @author [email protected] (Damon Kohler)
*/
public class MessageFields {

private final Map<String, Field> fields;
private final Map<String, Field> setters;
private final Map<String, Field> getters;
private final List<Field> orderedFields;
private final MessageContext messageContext;
private final List<Field> fields;

public MessageFields(MessageContext messageContext) {
fields = Maps.newHashMap();
setters = Maps.newHashMap();
getters = Maps.newHashMap();
orderedFields = Lists.newArrayList();
for (String name : messageContext.getFieldNames()) {
Field field = messageContext.getFieldFactory(name).create();
fields.put(name, field);
getters.put(messageContext.getFieldGetterName(name), field);
setters.put(messageContext.getFieldSetterName(name), field);
orderedFields.add(field);
this.messageContext = messageContext;
List<String> fieldNames = messageContext.getFieldNames();
Field[] fieldsArr = new Field[fieldNames.size()];
for (int i = 0; i < fieldsArr.length; i++) {
String fieldName = fieldNames.get(i);
fieldsArr[i] = messageContext.getFieldFactory(fieldName).create();
}
fields = Collections.unmodifiableList(Arrays.asList(fieldsArr));
}

public Field getField(String name) {
return fields.get(name);
return getField(messageContext.getFieldIndexByName(name));
}

public Field getSetterField(String name) {
return setters.get(name);
return getField(messageContext.getFieldIndexBySetterName(name));
}

public Field getGetterField(String name) {
return getters.get(name);
return getField(messageContext.getFieldIndexByGetterName(name));
}

private Field getField(Integer index) {
return index != null ? fields.get(index) : null;
}

public List<Field> getFields() {
return Collections.unmodifiableList(orderedFields);
return fields;
}

public Object getFieldValue(String name) {
Field field = fields.get(name);
Field field = getField(name);
if (field != null) {
return field.getValue();
}
throw new RosMessageRuntimeException("Uknown field: " + name);
}

public void setFieldValue(String name, Object value) {
Field field = fields.get(name);
Field field = getField(name);
if (field != null) {
field.setValue(value);
} else {
Expand All @@ -85,32 +81,27 @@ public void setFieldValue(String name, Object value) {

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((fields == null) ? 0 : fields.hashCode());
result = prime * result + ((orderedFields == null) ? 0 : orderedFields.hashCode());
int result = messageContext.hashCode();
result = 31 * result + fields.hashCode();
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
public boolean equals(Object o) {
if (this == o) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MessageFields other = (MessageFields) obj;
if (fields == null) {
if (other.fields != null)
return false;
} else if (!fields.equals(other.fields))
}
if (!(o instanceof MessageFields)) {
return false;
if (orderedFields == null) {
if (other.orderedFields != null)
return false;
} else if (!orderedFields.equals(other.orderedFields))
}

MessageFields that = (MessageFields) o;

if (!messageContext.equals(that.messageContext)) {
return false;
return true;
}
return fields.equals(that.fields);

}

}