AdvertiseAddress.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.address;
00018 
00019 import com.google.common.base.Preconditions;
00020 
00021 import org.ros.exception.RosRuntimeException;
00022 
00023 import java.net.InetAddress;
00024 import java.net.InetSocketAddress;
00025 import java.net.URI;
00026 import java.util.concurrent.Callable;
00027 
00039 public class AdvertiseAddress {
00040 
00041   private final String host;
00042 
00043   private Callable<Integer> portCallable;
00044 
00045   public static AdvertiseAddress newPrivate() {
00046     return new PrivateAdvertiseAddressFactory().newDefault();
00047   }
00048 
00056   public static AdvertiseAddress newPublic() {
00057     return new PublicAdvertiseAddressFactory().newDefault();
00058   }
00059 
00060   public AdvertiseAddress(String host) {
00061     Preconditions.checkNotNull(host);
00062     this.host = host;
00063   }
00064 
00065   public String getHost() {
00066     return host;
00067   }
00068 
00069   public void setStaticPort(final int port) {
00070     portCallable = new Callable<Integer>() {
00071       @Override
00072       public Integer call() throws Exception {
00073         return port;
00074       }
00075     };
00076   }
00077 
00078   public int getPort() {
00079     try {
00080       return portCallable.call();
00081     } catch (Exception e) {
00082       throw new RosRuntimeException(e);
00083     }
00084   }
00085 
00086   public void setPortCallable(Callable<Integer> portCallable) {
00087     this.portCallable = portCallable;
00088   }
00089 
00090   public InetAddress toInetAddress() {
00091     return InetAddressFactory.newFromHostString(host);
00092   }
00093 
00094   public InetSocketAddress toInetSocketAddress() {
00095     Preconditions.checkNotNull(portCallable);
00096     try {
00097       InetAddress address = toInetAddress();
00098       return new InetSocketAddress(address, portCallable.call());
00099     } catch (Exception e) {
00100       throw new RosRuntimeException(e);
00101     }
00102   }
00103 
00104   public URI toUri(String scheme) {
00105     Preconditions.checkNotNull(portCallable);
00106     try {
00107       return new URI(scheme, null, host, portCallable.call(), "/", null, null);
00108     } catch (Exception e) {
00109       throw new RosRuntimeException("Failed to create URI: " + this, e);
00110     }
00111   }
00112 
00113   public boolean isLoopbackAddress() {
00114     return toInetAddress().isLoopbackAddress();
00115   }
00116 
00117   @Override
00118   public String toString() {
00119     Preconditions.checkNotNull(portCallable);
00120     try {
00121       return "AdvertiseAddress<" + host + ", " + portCallable.call() + ">";
00122     } catch (Exception e) {
00123       throw new RosRuntimeException(e);
00124     }
00125   }
00126 
00127   @Override
00128   public int hashCode() {
00129     Preconditions.checkNotNull(portCallable);
00130     final int prime = 31;
00131     int result = 1;
00132     result = prime * result + ((host == null) ? 0 : host.hashCode());
00133     try {
00134       result = prime * result + portCallable.call();
00135     } catch (Exception e) {
00136       throw new RosRuntimeException(e);
00137     }
00138     return result;
00139   }
00140 
00141   @Override
00142   public boolean equals(Object obj) {
00143     Preconditions.checkNotNull(portCallable);
00144     if (this == obj)
00145       return true;
00146     if (obj == null)
00147       return false;
00148     if (getClass() != obj.getClass())
00149       return false;
00150     AdvertiseAddress other = (AdvertiseAddress) obj;
00151     if (host == null) {
00152       if (other.host != null)
00153         return false;
00154     } else if (!host.equals(other.host))
00155       return false;
00156     try {
00157       if (portCallable.call() != other.portCallable.call())
00158         return false;
00159     } catch (Exception e) {
00160       throw new RosRuntimeException(e);
00161     }
00162     return true;
00163   }
00164 
00165 }


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