Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.node.topic;
00018
00019 import com.google.common.base.Preconditions;
00020 import com.google.common.collect.Lists;
00021
00022 import org.ros.internal.message.topic.TopicDescription;
00023 import org.ros.internal.transport.ConnectionHeader;
00024 import org.ros.internal.transport.ConnectionHeaderFields;
00025 import org.ros.namespace.GraphName;
00026
00027 import java.util.List;
00028 import java.util.Map;
00029
00035 public class TopicDeclaration {
00036
00037 private final TopicIdentifier topicIdentifier;
00038 private final TopicDescription topicDescription;
00039
00045 public static TopicDeclaration newFromHeader(Map<String, String> header) {
00046 Preconditions.checkArgument(header.containsKey(ConnectionHeaderFields.TOPIC));
00047 GraphName name = GraphName.of(header.get(ConnectionHeaderFields.TOPIC));
00048 String type = header.get(ConnectionHeaderFields.TYPE);
00049 String definition = header.get(ConnectionHeaderFields.MESSAGE_DEFINITION);
00050 String md5Checksum = header.get(ConnectionHeaderFields.MD5_CHECKSUM);
00051 TopicDescription topicDescription = new TopicDescription(type, definition, md5Checksum);
00052 return new TopicDeclaration(new TopicIdentifier(name), topicDescription);
00053 }
00054
00055 public static TopicDeclaration newFromTopicName(GraphName topicName,
00056 TopicDescription topicDescription) {
00057 return new TopicDeclaration(new TopicIdentifier(topicName), topicDescription);
00058 }
00059
00060 public TopicDeclaration(TopicIdentifier topicIdentifier, TopicDescription topicDescription) {
00061 Preconditions.checkNotNull(topicIdentifier);
00062 Preconditions.checkNotNull(topicDescription);
00063 this.topicIdentifier = topicIdentifier;
00064 this.topicDescription = topicDescription;
00065 }
00066
00067 public TopicIdentifier getIdentifier() {
00068 return topicIdentifier;
00069 }
00070
00071 public GraphName getName() {
00072 return topicIdentifier.getName();
00073 }
00074
00075 public String getMessageType() {
00076 return topicDescription.getType();
00077 }
00078
00079 public ConnectionHeader toConnectionHeader() {
00080 ConnectionHeader connectionHeader = new ConnectionHeader();
00081 connectionHeader.merge(topicIdentifier.toConnectionHeader());
00082 connectionHeader.addField(ConnectionHeaderFields.TYPE, topicDescription.getType());
00083 connectionHeader.addField(ConnectionHeaderFields.MESSAGE_DEFINITION,
00084 topicDescription.getDefinition());
00085 connectionHeader.addField(ConnectionHeaderFields.MD5_CHECKSUM,
00086 topicDescription.getMd5Checksum());
00087 return connectionHeader;
00088 }
00089
00090 public List<String> toList() {
00091 return Lists.newArrayList(getName().toString(), getMessageType());
00092 }
00093
00094 @Override
00095 public String toString() {
00096 return "Topic<" + topicIdentifier + ", " + topicDescription.toString() + ">";
00097 }
00098
00099 @Override
00100 public int hashCode() {
00101 final int prime = 31;
00102 int result = 1;
00103 result = prime * result + ((topicDescription == null) ? 0 : topicDescription.hashCode());
00104 result = prime * result + ((topicIdentifier == null) ? 0 : topicIdentifier.hashCode());
00105 return result;
00106 }
00107
00108 @Override
00109 public boolean equals(Object obj) {
00110 if (this == obj)
00111 return true;
00112 if (obj == null)
00113 return false;
00114 if (getClass() != obj.getClass())
00115 return false;
00116 TopicDeclaration other = (TopicDeclaration) obj;
00117 if (topicDescription == null) {
00118 if (other.topicDescription != null)
00119 return false;
00120 } else if (!topicDescription.equals(other.topicDescription))
00121 return false;
00122 if (topicIdentifier == null) {
00123 if (other.topicIdentifier != null)
00124 return false;
00125 } else if (!topicIdentifier.equals(other.topicIdentifier))
00126 return false;
00127 return true;
00128 }
00129 }