1 /* 2 * Conditions Of Use 3 * 4 * This software was developed by employees of the National Institute of 5 * Standards and Technology (NIST), an agency of the Federal Government. 6 * Pursuant to title 15 Untied States Code Section 105, works of NIST 7 * employees are not subject to copyright protection in the United States 8 * and are considered to be in the public domain. As a result, a formal 9 * license is not needed to use the software. 10 * 11 * This software is provided by NIST as a service and is expressly 12 * provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED 13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF 14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT 15 * AND DATA ACCURACY. NIST does not warrant or make any representations 16 * regarding the use of the software or the results thereof, including but 17 * not limited to the correctness, accuracy, reliability or usefulness of 18 * the software. 19 * 20 * Permission to use this software is contingent upon your acceptance 21 * of the terms of this agreement 22 * 23 * . 24 * 25 */ 26 /******************************************************************************* 27 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD). * 28 *******************************************************************************/ 29 package gov.nist.core; 30 import java.net.*; 31 32 /** 33 * Holds the hostname:port. 34 * 35 *@version 1.2 36 * 37 *@author M. Ranganathan 38 * 39 * 40 * 41 */ 42 public final class HostPort extends GenericObject { 43 44 45 private static final long serialVersionUID = -7103412227431884523L; 46 47 // host / ipv4/ ipv6/ 48 /** host field 49 */ 50 protected Host host; 51 52 /** port field 53 * 54 */ 55 protected int port; 56 57 /** Default constructor 58 */ HostPort()59 public HostPort() { 60 61 host = null; 62 port = -1; // marker for not set. 63 } 64 65 /** 66 * Encode this hostport into its string representation. 67 * Note that this could be different from the string that has 68 * been parsed if something has been edited. 69 * @return String 70 */ encode()71 public String encode() { 72 return encode(new StringBuffer()).toString(); 73 } 74 encode(StringBuffer buffer)75 public StringBuffer encode(StringBuffer buffer) { 76 host.encode(buffer); 77 if (port != -1) 78 buffer.append(COLON).append(port); 79 return buffer; 80 } 81 82 /** returns true if the two objects are equals, false otherwise. 83 * @param other Object to set 84 * @return boolean 85 */ equals(Object other)86 public boolean equals(Object other) { 87 if (other == null) return false; 88 if (getClass () != other.getClass ()) { 89 return false; 90 } 91 HostPort that = (HostPort) other; 92 return port == that.port && host.equals(that.host); 93 } 94 95 /** get the Host field 96 * @return host field 97 */ getHost()98 public Host getHost() { 99 return host; 100 } 101 102 /** get the port field 103 * @return int 104 */ getPort()105 public int getPort() { 106 return port; 107 } 108 109 /** 110 * Returns boolean value indicating if Header has port 111 * @return boolean value indicating if Header has port 112 */ hasPort()113 public boolean hasPort() { 114 return port != -1; 115 } 116 117 /** remove port. 118 */ removePort()119 public void removePort() { 120 port = -1; 121 } 122 123 /** 124 * Set the host member 125 * @param h Host to set 126 */ setHost(Host h)127 public void setHost(Host h) { 128 host = h; 129 } 130 131 /** 132 * Set the port member 133 * @param p int to set 134 */ setPort(int p)135 public void setPort(int p) { 136 port = p; 137 } 138 139 /** Return the internet address corresponding to the host. 140 *@throws java.net.UnkownHostException if host name cannot be resolved. 141 *@return the inet address for the host. 142 */ getInetAddress()143 public InetAddress getInetAddress() throws java.net.UnknownHostException { 144 if (host == null) 145 return null; 146 else 147 return host.getInetAddress(); 148 } 149 merge(Object mergeObject)150 public void merge(Object mergeObject) { 151 super.merge (mergeObject); 152 if (port == -1) 153 port = ((HostPort) mergeObject).port; 154 } 155 clone()156 public Object clone() { 157 HostPort retval = (HostPort) super.clone(); 158 if (this.host != null) 159 retval.host = (Host) this.host.clone(); 160 return retval; 161 } 162 toString()163 public String toString() { 164 return this.encode(); 165 } 166 167 @Override hashCode()168 public int hashCode() { 169 return this.host.hashCode() + this.port; 170 } 171 } 172