001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.core;
028:
029: /**
030: * Holds the hostname:port.
031: *
032: *@version JAIN-SIP-1.1
033: *
034: *
035: *<a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
036: *
037: */
038: public final class HostPort {
039:
040: // host / ipv4/ ipv6/
041: /** Host field. */
042: protected Host host;
043:
044: /** Port field. */
045: protected Integer port;
046:
047: /** Default constructor. */
048: public HostPort() {
049:
050: host = null;
051: port = null; // marker for not set.
052: }
053:
054: /**
055: * Encodes this hostport into its string representation.
056: * Note that this could be different from the string that has
057: * been parsed if something has been edited.
058: * @return host and port encoded string
059: */
060: public String encode() {
061: StringBuffer retval = new StringBuffer();
062: if (host != null)
063: retval.append(host.encode());
064: if (port != null)
065: retval.append(':').append(port.toString());
066: return retval.toString();
067: }
068:
069: /**
070: * Returns true if the two objects are equals, false otherwise.
071: * @param other Object to set
072: * @return boolean
073: */
074: public boolean equals(Object other) {
075: if (!this .getClass().equals(other.getClass())) {
076: return false;
077: }
078: HostPort that = (HostPort) other;
079: if ((this .port == null && that.port != null)
080: || (this .port != null && that.port == null))
081: return false;
082: else if (this .port == that.port && this .host.equals(that.host))
083: return true;
084: else
085: return this .host.equals(that.host)
086: && this .port.equals(that.port);
087: }
088:
089: /**
090: * Gets the Host field.
091: * @return host field
092: */
093: public Host getHost() {
094: return host;
095: }
096:
097: /**
098: * Gets the port field.
099: * @return int
100: */
101: public int getPort() {
102: if (port == null) {
103: return -1;
104: } else {
105: return port.intValue();
106: }
107: }
108:
109: /**
110: * Returns boolean value indicating if Header has port
111: * @return boolean value indicating if Header has port
112: */
113: public boolean hasPort() {
114: return port != null;
115: }
116:
117: /** Removes the port. */
118: public void removePort() {
119: port = null;
120: }
121:
122: /**
123: * Sets the host member.
124: * @param h Host to set
125: */
126: public void setHost(Host h) {
127: host = h;
128: }
129:
130: /**
131: * Sets the port member.
132: * @param p int to set
133: * @throws IllegalArgumentException in case of illegal port value
134: */
135: public void setPort(int p) throws IllegalArgumentException {
136: if (p > 65535 || p < 0) {
137: throw new IllegalArgumentException("Illegal port value "
138: + p);
139: }
140: port = new Integer(p);
141: }
142:
143: /**
144: * Makes a copy of the current instance.
145: * @return copy of current object
146: */
147: public Object clone() {
148: HostPort retval = new HostPort();
149: if (this .host != null)
150: retval.host = (Host) this .host.clone();
151: if (this .port != null)
152: retval.port = new Integer(this.port.intValue());
153: return retval;
154: }
155: }
|