KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > InetAddrPort


1 // ========================================================================
2
// $Id: InetAddrPort.java,v 1.7 2004/10/23 09:03:22 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18 import java.io.Serializable JavaDoc;
19 import java.net.InetAddress JavaDoc;
20
21 /* ======================================================================== */
22 /** InetAddress and Port.
23  */

24 public class InetAddrPort implements Serializable JavaDoc
25 {
26     /* ------------------------------------------------------------ */
27     public final static String JavaDoc __0_0_0_0 = "0.0.0.0";
28
29     /* ------------------------------------------------------------ */
30     private InetAddress JavaDoc _addr=null;
31     private boolean _addrIsHost=false;
32     private int _port=0;
33
34     /* ------------------------------------------------------------------- */
35     public InetAddrPort()
36     {}
37
38     /* ------------------------------------------------------------ */
39     /** Constructor for a port on all local host address.
40      * @param port
41      */

42     public InetAddrPort(int port)
43     {
44         _port=port;
45     }
46     
47     /* ------------------------------------------------------------ */
48     /** Constructor.
49      * @param addr
50      * @param port
51      */

52     public InetAddrPort(InetAddress JavaDoc addr, int port)
53     {
54         _addr=addr;
55         _port=port;
56     }
57     
58     /* ------------------------------------------------------------ */
59     /** Constructor.
60      * @param host
61      * @param port
62      */

63     public InetAddrPort(String JavaDoc host, int port)
64         throws java.net.UnknownHostException JavaDoc
65     {
66         setHost(host);
67         setPort(port);
68     }
69     
70     /* ------------------------------------------------------------ */
71     /** Constructor.
72      * @param inetAddrPort String of the form "addr:port"
73      */

74     public InetAddrPort(String JavaDoc inetAddrPort)
75         throws java.net.UnknownHostException JavaDoc
76     {
77         int c = inetAddrPort.indexOf(':');
78         if (c>=0)
79         {
80             String JavaDoc addr=inetAddrPort.substring(0,c);
81             if (addr.indexOf('/')>0)
82                 addr=addr.substring(addr.indexOf('/')+1);
83             inetAddrPort=inetAddrPort.substring(c+1);
84         
85             if (addr.length()>0 && ! __0_0_0_0.equals(addr))
86             {
87                 _addrIsHost=!Character.isDigit((addr.charAt(0)));
88                 this._addr=InetAddress.getByName(addr);
89             }
90         }
91         
92         _port = Integer.parseInt(inetAddrPort);
93     }
94     
95     /* ------------------------------------------------------------ */
96     /** Constructor.
97      * @param address InetAddrPort top copy.
98      */

99     public InetAddrPort(InetAddrPort address)
100     {
101         if (address!=null)
102         {
103             _addr=address._addr;
104             _port=address._port;
105         }
106     }
107     
108     /* ------------------------------------------------------------ */
109     /** Get the Host.
110      * @return The IP address
111      */

112     public String JavaDoc getHost()
113     {
114         if (_addr==null)
115             return __0_0_0_0;
116         
117         return _addrIsHost?_addr.getHostName():_addr.getHostAddress();
118     }
119     
120     /* ------------------------------------------------------------ */
121     /** Set the Host.
122      * @param host
123      * @exception java.net.UnknownHostException
124      */

125     public void setHost(String JavaDoc host)
126         throws java.net.UnknownHostException JavaDoc
127     {
128         _addr=null;
129         if (host!=null)
130         {
131             if (host.indexOf('/')>0)
132                 host=host.substring(0,host.indexOf('/'));
133             _addrIsHost=!Character.isDigit((host.charAt(0)));
134             _addr=InetAddress.getByName(host);
135         }
136     }
137     
138     /* ------------------------------------------------------------ */
139     /** Get the IP address.
140      * @return The IP address
141      */

142     public InetAddress JavaDoc getInetAddress()
143     {
144         return _addr;
145     }
146     
147     /* ------------------------------------------------------------ */
148     /** Set the IP address.
149      * @param addr The IP address
150      */

151     public void setInetAddress(InetAddress JavaDoc addr)
152     {
153         _addrIsHost=false;
154         _addr=addr;
155     }
156
157     /* ------------------------------------------------------------ */
158     /** Get the port.
159      * @return The port number
160      */

161     public int getPort()
162     {
163         return _port;
164     }
165     
166     /* ------------------------------------------------------------ */
167     /** Set the port.
168      * @param port The port number
169      */

170     public void setPort(int port)
171     {
172         _port=port;
173     }
174     
175     
176     /* ------------------------------------------------------------------- */
177     public String JavaDoc toString()
178     {
179         return getHost()+':'+_port;
180     }
181
182     /* ------------------------------------------------------------ */
183     /** Clone the InetAddrPort.
184      * @return A new instance.
185      */

186     public Object JavaDoc clone()
187     {
188         return new InetAddrPort(this);
189     }
190
191     /* ------------------------------------------------------------ */
192     /** Hash Code.
193      * @return hash Code.
194      */

195     public int hashCode()
196     {
197         return _port+((_addr==null)?0:_addr.hashCode());
198     }
199     
200     /* ------------------------------------------------------------ */
201     /** Equals.
202      * @param o
203      * @return True if is the same address and port.
204      */

205     public boolean equals(Object JavaDoc o)
206     {
207         if (o==null)
208             return false;
209         if (o==this)
210             return true;
211         if (o instanceof InetAddrPort)
212         {
213             InetAddrPort addr=(InetAddrPort)o;
214             return addr._port==_port &&
215                 ( addr._addr==_addr ||
216                   addr._addr!=null && addr._addr.equals(_addr));
217         }
218         return false;
219     }
220 }
221
222     
223
224
225
226
227
Popular Tags