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.definition; 00018 00019 import com.google.common.collect.Lists; 00020 import com.google.common.collect.Sets; 00021 00022 import org.ros.message.MessageDefinitionProvider; 00023 import org.ros.message.MessageIdentifier; 00024 00025 import java.util.Collection; 00026 import java.util.NoSuchElementException; 00027 import java.util.Set; 00028 00032 public class MessageDefinitionProviderChain implements MessageDefinitionProvider { 00033 00034 private final Collection<MessageDefinitionProvider> messageDefinitionProviders; 00035 00036 public MessageDefinitionProviderChain() { 00037 messageDefinitionProviders = Lists.newArrayList(); 00038 } 00039 00040 public void addMessageDefinitionProvider(MessageDefinitionProvider messageDefinitionProvider) { 00041 messageDefinitionProviders.add(messageDefinitionProvider); 00042 } 00043 00044 @Override 00045 public String get(String messageType) { 00046 for (MessageDefinitionProvider messageDefinitionProvider : messageDefinitionProviders) { 00047 if (messageDefinitionProvider.has(messageType)) { 00048 return messageDefinitionProvider.get(messageType); 00049 } 00050 } 00051 throw new NoSuchElementException("No message definition available for: " + messageType); 00052 } 00053 00054 @Override 00055 public boolean has(String messageType) { 00056 for (MessageDefinitionProvider messageDefinitionProvider : messageDefinitionProviders) { 00057 if (messageDefinitionProvider.has(messageType)) { 00058 return true; 00059 } 00060 } 00061 return false; 00062 } 00063 00064 @Override 00065 public Collection<String> getPackages() { 00066 Set<String> result = Sets.newHashSet(); 00067 for (MessageDefinitionProvider messageDefinitionProvider : messageDefinitionProviders) { 00068 Collection<String> packages = messageDefinitionProvider.getPackages(); 00069 result.addAll(packages); 00070 } 00071 return result; 00072 } 00073 00074 @Override 00075 public Collection<MessageIdentifier> getMessageIdentifiersByPackage(String pkg) { 00076 Set<MessageIdentifier> result = Sets.newHashSet(); 00077 for (MessageDefinitionProvider messageDefinitionProvider : messageDefinitionProviders) { 00078 Collection<MessageIdentifier> messageIdentifiers = 00079 messageDefinitionProvider.getMessageIdentifiersByPackage(pkg); 00080 if (messageIdentifiers != null) { 00081 result.addAll(messageIdentifiers); 00082 } 00083 } 00084 return result; 00085 } 00086 }