KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > TCPAddress


1 package org.sapia.ubik.net;
2
3 import java.io.IOException JavaDoc;
4 import java.io.ObjectInput JavaDoc;
5 import java.io.ObjectOutput JavaDoc;
6
7
8 /**
9  * This class models a TCP "server address". This class encapsulates host and port information.
10  *
11  * @author Yanick Duchesne
12  * <dl>
13  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
14  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
15  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
16  * </dl>
17  */

18 public class TCPAddress implements java.io.Externalizable JavaDoc, ServerAddress {
19   static final long serialVersionUID = 1L;
20   public static final String JavaDoc TRANSPORT_TYPE = "tcp/socket";
21   private String JavaDoc _host;
22   private int _port;
23   private int _hashCode;
24   protected String JavaDoc _transportType = TRANSPORT_TYPE;
25
26   /** Do not call; used for externalization only. */
27   public TCPAddress() {
28   }
29
30   /**
31    * Creates an instance of this class with the given host and port
32    * information.
33    *
34    * @param host the address of a host.
35    * @param port the port of the server running on the given host.
36    *
37    */

38   public TCPAddress(String JavaDoc host, int port) {
39     _host = host;
40     _port = port;
41     _hashCode = (host + port).hashCode();
42   }
43
44   /**
45    * Overrides equals; two host identifiers are equal if they have the
46    * same host and port.
47    *
48    * @param obj the instance with which to perform the comparison.
49    */

50   public boolean equals(Object JavaDoc obj) {
51     TCPAddress other;
52
53     try {
54       other = (TCPAddress) obj;
55
56       return (other._port == _port) && other._host.equals(_host);
57     } catch (ClassCastException JavaDoc e) {
58       return false;
59     }
60   }
61
62   /**
63    * Returns the address of the host to which this instance corresponds.
64    *
65    * @return this instance's host address.
66    */

67   public String JavaDoc getHost() {
68     return _host;
69   }
70
71   /**
72    * Returns the port of the server to which this instance corresponds.
73    *
74    * @return a server port..
75    */

76   public int getPort() {
77     return _port;
78   }
79
80   /**
81    * This method returns a hash code based on this instances host and port.
82    *
83    * @return a hash code, as an <code>int</code>.
84    */

85   public int hashCode() {
86     return _hashCode;
87   }
88
89   /**
90    * @see java.io.Externalizable#readExternal(ObjectInput)
91    */

92   public void readExternal(ObjectInput JavaDoc in)
93     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
94     _host = in.readUTF();
95     _port = in.readInt();
96     _hashCode = in.readInt();
97     _transportType = in.readUTF();
98   }
99
100   /**
101    * @see java.io.Externalizable#writeExternal(ObjectOutput)
102    */

103   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
104     out.writeUTF(_host);
105     out.writeInt(_port);
106     out.writeInt(_hashCode);
107     out.writeUTF(_transportType);
108   }
109
110   /**
111    * @see org.sapia.ubik.net.ServerAddress#getTransportType()
112    */

113   public String JavaDoc getTransportType() {
114     return _transportType;
115   }
116
117   /**
118    * Returns a string representation of this instance.
119    *
120    * @return a <code>String</code>.
121    */

122   public String JavaDoc toString() {
123     return "[ host=" + _host + ", port=" + _port + ", type=" + _transportType +
124     " ]";
125   }
126 }
127
Popular Tags