KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > common > IpAddress


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.common;
26
27 import java.net.InetAddress JavaDoc;
28
29 /**
30  * This class defines an IpAddress
31  *
32  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
33  * @version 1.0
34  */

35 public class IpAddress implements Address
36 {
37
38   private InetAddress JavaDoc address;
39   private int port;
40
41   /**
42    * Creates a new <code>IpAddress</code> object
43    */

44   public IpAddress(InetAddress JavaDoc address, int port)
45   {
46     this.address = address;
47     this.port = port;
48   }
49
50   /**
51    * Returns the address value.
52    *
53    * @return Returns the address.
54    */

55   public InetAddress JavaDoc getAddress()
56   {
57     return address;
58   }
59
60   /**
61    * Sets the address value.
62    *
63    * @param address The address to set.
64    */

65   public void setAddress(InetAddress JavaDoc address)
66   {
67     this.address = address;
68   }
69
70   /**
71    * Sets the port value.
72    *
73    * @param port The port to set.
74    */

75   public void setPort(int port)
76   {
77     this.port = port;
78   }
79
80   /**
81    * Returns the port value.
82    *
83    * @return Returns the port.
84    */

85   public int getPort()
86   {
87     return port;
88   }
89
90   /**
91    * @see java.lang.Object#toString()
92    */

93   public String JavaDoc toString()
94   {
95     return address.toString() + ":" + port;
96   }
97
98   /**
99    * @see java.lang.Object#equals(java.lang.Object)
100    */

101   public boolean equals(Object JavaDoc obj)
102   {
103     if (obj instanceof IpAddress)
104     {
105       IpAddress ip = (IpAddress) obj;
106       return (this.port == ip.port)
107           && (((this.getAddress() == null) && (ip.getAddress() == null)) || this
108               .getAddress().equals(ip.getAddress()));
109     }
110     return false;
111   }
112
113   /**
114    * @see java.lang.Object#hashCode()
115    */

116   public int hashCode()
117   {
118     return address.hashCode() + port;
119   }
120
121   /**
122    * @see java.lang.Comparable#compareTo(java.lang.Object)
123    */

124   public int compareTo(Object JavaDoc o)
125   {
126     if (o instanceof IpAddress)
127       return address.toString().compareTo(
128           ((IpAddress) o).getAddress().toString());
129     else
130       throw new ClassCastException JavaDoc(
131           "IpAddress can only be compared with another IpAddress");
132   }
133
134 }
Popular Tags