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 import com.google.common.collect.Maps; 00021 00022 import org.ros.internal.message.definition.MessageDefinitionParser; 00023 import org.ros.internal.message.definition.MessageDefinitionParser.MessageDefinitionVisitor; 00024 import org.ros.message.MessageDeclaration; 00025 import org.ros.message.MessageFactory; 00026 00027 import java.util.Map; 00028 00032 public class MessageContextProvider { 00033 00034 private final Map<MessageDeclaration, MessageContext> cache; 00035 private final MessageFactory messageFactory; 00036 00037 public MessageContextProvider(MessageFactory messageFactory) { 00038 Preconditions.checkNotNull(messageFactory); 00039 this.messageFactory = messageFactory; 00040 cache = Maps.newConcurrentMap(); 00041 } 00042 00043 public MessageContext get(MessageDeclaration messageDeclaration) { 00044 MessageContext messageContext = cache.get(messageDeclaration); 00045 if (messageContext == null) { 00046 messageContext = new MessageContext(messageDeclaration, messageFactory); 00047 MessageDefinitionVisitor visitor = new MessageContextBuilder(messageContext); 00048 MessageDefinitionParser messageDefinitionParser = new MessageDefinitionParser(visitor); 00049 messageDefinitionParser.parse(messageDeclaration.getType(), 00050 messageDeclaration.getDefinition()); 00051 cache.put(messageDeclaration, messageContext); 00052 } 00053 return messageContext; 00054 } 00055 }