Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.message.context;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import org.ros.internal.message.definition.MessageDefinitionParser.MessageDefinitionVisitor;
00022 import org.ros.internal.message.field.Field;
00023 import org.ros.internal.message.field.FieldFactory;
00024 import org.ros.internal.message.field.FieldType;
00025 import org.ros.internal.message.field.MessageFieldType;
00026 import org.ros.internal.message.field.PrimitiveFieldType;
00027 import org.ros.message.MessageIdentifier;
00028
00032 class MessageContextBuilder implements MessageDefinitionVisitor {
00033
00034 private final MessageContext messageContext;
00035
00036 public MessageContextBuilder(MessageContext context) {
00037 this.messageContext = context;
00038 }
00039
00040 private FieldType getFieldType(String type) {
00041 Preconditions.checkArgument(!type.equals(messageContext.getType()),
00042 "Message definitions may not be self-referential.");
00043 FieldType fieldType;
00044 if (PrimitiveFieldType.existsFor(type)) {
00045 fieldType = PrimitiveFieldType.valueOf(type.toUpperCase());
00046 } else {
00047 fieldType =
00048 new MessageFieldType(MessageIdentifier.of(type), messageContext.getMessageFactory());
00049 }
00050 return fieldType;
00051 }
00052
00053 @Override
00054 public void variableValue(String type, final String name) {
00055 final FieldType fieldType = getFieldType(type);
00056 messageContext.addFieldFactory(name, new FieldFactory() {
00057 @Override
00058 public Field create() {
00059 return fieldType.newVariableValue(name);
00060 }
00061 });
00062 }
00063
00064 @Override
00065 public void variableList(String type, final int size, final String name) {
00066 final FieldType fieldType = getFieldType(type);
00067 messageContext.addFieldFactory(name, new FieldFactory() {
00068 @Override
00069 public Field create() {
00070 return fieldType.newVariableList(name, size);
00071 }
00072 });
00073 }
00074
00075 @Override
00076 public void constantValue(String type, final String name, final String value) {
00077 final FieldType fieldType = getFieldType(type);
00078 messageContext.addFieldFactory(name, new FieldFactory() {
00079 @Override
00080 public Field create() {
00081 return fieldType.newConstantValue(name, fieldType.parseFromString(value));
00082 }
00083 });
00084 }
00085 }