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.node.topic; 00018 00019 import com.google.common.base.Preconditions; 00020 import com.google.common.collect.Sets; 00021 00022 import org.ros.internal.node.server.NodeIdentifier; 00023 import org.ros.internal.transport.ConnectionHeader; 00024 import org.ros.namespace.GraphName; 00025 import org.ros.node.Node; 00026 import org.ros.node.topic.Publisher; 00027 00028 import java.net.URI; 00029 import java.util.Collection; 00030 import java.util.Set; 00031 00037 public class PublisherIdentifier { 00038 00039 private final NodeIdentifier nodeIdentifier; 00040 private final TopicIdentifier topicIdentifier; 00041 00042 public static Collection<PublisherIdentifier> newCollectionFromUris( 00043 Collection<URI> publisherUris, TopicDeclaration topicDeclaration) { 00044 Set<PublisherIdentifier> publishers = Sets.newHashSet(); 00045 for (URI uri : publisherUris) { 00046 NodeIdentifier nodeIdentifier = new NodeIdentifier(null, uri); 00047 publishers.add(new PublisherIdentifier(nodeIdentifier, topicDeclaration.getIdentifier())); 00048 } 00049 return publishers; 00050 } 00051 00052 public static PublisherIdentifier newFromStrings(String nodeName, String uri, String topicName) { 00053 return new PublisherIdentifier(NodeIdentifier.forNameAndUri(nodeName, uri), 00054 TopicIdentifier.forName(topicName)); 00055 } 00056 00057 public PublisherIdentifier(NodeIdentifier nodeIdentifier, TopicIdentifier topicIdentifier) { 00058 Preconditions.checkNotNull(nodeIdentifier); 00059 Preconditions.checkNotNull(topicIdentifier); 00060 this.nodeIdentifier = nodeIdentifier; 00061 this.topicIdentifier = topicIdentifier; 00062 } 00063 00064 public ConnectionHeader toConnectionHeader() { 00065 ConnectionHeader connectionHeader = new ConnectionHeader(); 00066 connectionHeader.merge(nodeIdentifier.toConnectionHeader()); 00067 connectionHeader.merge(topicIdentifier.toConnectionHeader()); 00068 return connectionHeader; 00069 } 00070 00071 public NodeIdentifier getNodeIdentifier() { 00072 return nodeIdentifier; 00073 } 00074 00079 public GraphName getNodeName() { 00080 return nodeIdentifier.getName(); 00081 } 00082 00086 public URI getNodeUri() { 00087 return nodeIdentifier.getUri(); 00088 } 00089 00093 public TopicIdentifier getTopicIdentifier() { 00094 return topicIdentifier; 00095 } 00096 00100 public GraphName getTopicName() { 00101 return topicIdentifier.getName(); 00102 } 00103 00104 @Override 00105 public String toString() { 00106 return "PublisherIdentifier<" + nodeIdentifier + ", " + topicIdentifier + ">"; 00107 } 00108 00109 @Override 00110 public int hashCode() { 00111 final int prime = 31; 00112 int result = 1; 00113 result = prime * result + nodeIdentifier.hashCode(); 00114 result = prime * result + topicIdentifier.hashCode(); 00115 return result; 00116 } 00117 00118 @Override 00119 public boolean equals(Object obj) { 00120 if (this == obj) 00121 return true; 00122 if (obj == null) 00123 return false; 00124 if (getClass() != obj.getClass()) 00125 return false; 00126 PublisherIdentifier other = (PublisherIdentifier) obj; 00127 if (!nodeIdentifier.equals(other.nodeIdentifier)) 00128 return false; 00129 if (!topicIdentifier.equals(other.topicIdentifier)) 00130 return false; 00131 return true; 00132 } 00133 }