MessageContextBuilder.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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 }


rosjava_bootstrap
Author(s): Daniel Stonier , Damon Kohler
autogenerated on Fri Aug 28 2015 12:41:44