NodeIdentifier.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.node.server;
00018 
00019 import com.google.common.base.Preconditions;
00020 
00021 import org.ros.exception.RosRuntimeException;
00022 import org.ros.internal.transport.ConnectionHeader;
00023 import org.ros.internal.transport.ConnectionHeaderFields;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.node.Node;
00026 
00027 import java.net.URI;
00028 import java.net.URISyntaxException;
00029 
00036 public class NodeIdentifier {
00037 
00038   private final GraphName name;
00039   private final URI uri;
00040 
00041   public static NodeIdentifier forName(String name) {
00042     return new NodeIdentifier(GraphName.of(name), null);
00043   }
00044 
00045   public static NodeIdentifier forUri(String uri) {
00046     try {
00047       return new NodeIdentifier(null, new URI(uri));
00048     } catch (URISyntaxException e) {
00049       throw new RosRuntimeException(e);
00050     }
00051   }
00052 
00053   public static NodeIdentifier forNameAndUri(String name, String uri) {
00054     try {
00055       return new NodeIdentifier(GraphName.of(name), new URI(uri));
00056     } catch (URISyntaxException e) {
00057       throw new RosRuntimeException(e);
00058     }
00059   }
00060 
00079   public NodeIdentifier(GraphName name, URI uri) {
00080     Preconditions.checkArgument(name != null || uri != null);
00081     if (name != null) {
00082       Preconditions.checkArgument(name.isGlobal());
00083     }
00084     this.name = name;
00085     this.uri = uri;
00086   }
00087 
00088   public GraphName getName() {
00089     return name;
00090   }
00091 
00092   public URI getUri() {
00093     return uri;
00094   }
00095 
00096   public ConnectionHeader toConnectionHeader() {
00097     ConnectionHeader connectionHeader = new ConnectionHeader();
00098     connectionHeader.addField(ConnectionHeaderFields.CALLER_ID, name.toString());
00099     return connectionHeader;
00100   }
00101 
00102   @Override
00103   public String toString() {
00104     return "NodeIdentifier<" + name + ", " + uri + ">";
00105   }
00106 
00107   @Override
00108   public int hashCode() {
00109     final int prime = 31;
00110     int result = 1;
00111     result = prime * result + ((name == null) ? 0 : name.hashCode());
00112     result = prime * result + ((uri == null) ? 0 : uri.hashCode());
00113     return result;
00114   }
00115 
00116   @Override
00117   public boolean equals(Object obj) {
00118     if (this == obj)
00119       return true;
00120     if (obj == null)
00121       return false;
00122     if (getClass() != obj.getClass())
00123       return false;
00124     NodeIdentifier other = (NodeIdentifier) obj;
00125     if (name == null) {
00126       if (other.name != null)
00127         return false;
00128     } else if (!name.equals(other.name))
00129       return false;
00130     if (uri == null) {
00131       if (other.uri != null)
00132         return false;
00133     } else if (!uri.equals(other.uri))
00134       return false;
00135     return true;
00136   }
00137 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49