DefaultMessageInterfaceClassProvider.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;
00018 
00019 import com.google.common.annotations.VisibleForTesting;
00020 import com.google.common.collect.Maps;
00021 
00022 import java.util.Map;
00023 
00027 public class DefaultMessageInterfaceClassProvider implements MessageInterfaceClassProvider {
00028 
00029   private final Map<String, Class<?>> cache;
00030 
00031   public DefaultMessageInterfaceClassProvider() {
00032     cache = Maps.newConcurrentMap();
00033   }
00034 
00035   @SuppressWarnings("unchecked")
00036   @Override
00037   public <T> Class<T> get(String messageType) {
00038     if (cache.containsKey(messageType)) {
00039       return (Class<T>) cache.get(messageType);
00040     }
00041     try {
00042       String className = messageType.replace("/", ".");
00043       Class<T> messageInterfaceClass = (Class<T>) getClass().getClassLoader().loadClass(className);
00044       cache.put(messageType, messageInterfaceClass);
00045       return messageInterfaceClass;
00046     } catch (ClassNotFoundException e) {
00047       return (Class<T>) RawMessage.class;
00048     }
00049   }
00050 
00051   @VisibleForTesting
00052   <T> void add(String messageType, Class<T> messageInterfaceClass) {
00053     cache.put(messageType, messageInterfaceClass);
00054   }
00055 }


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