KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > InterfaceAddress


1 /*
2  * @(#)InterfaceAddress.java 1.3 06/01/12
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 /**
11  * This class represents a Network Interface address. In short it's an
12  * IP address, a subnet mask and a broadcast address when the address is
13  * an IPv4 one. An IP address and a network prefix length in the case
14  * of IPv6 address.
15  *
16  * @see java.net.NetworkInterface
17  * @since 1.6
18  */

19 public class InterfaceAddress {
20     private InetAddress JavaDoc address = null;
21     private Inet4Address JavaDoc broadcast = null;
22     private short maskLength = 0;
23
24     /*
25      * Package private constructor. Can't be built directly, instances are
26      * obtained through the NetworkInterface class.
27      */

28     InterfaceAddress() {
29     }
30
31     /**
32      * Returns an <code>InetAddress</code> for this address.
33      *
34      * @return the <code>InetAddress</code> for this address.
35      */

36     public InetAddress JavaDoc getAddress() {
37     return address;
38     }
39
40     /**
41      * Returns an <code>InetAddress</code> for the brodcast address
42      * for this InterfaceAddress.
43      * <p>
44      * Only IPv4 networks have broadcast address therefore, in the case
45      * of an IPv6 network, <code>null</code> will be returned.
46      *
47      * @return the <code>InetAddress</code> representing the broadcast
48      * address or <code>null</code> if there is no broadcast address.
49      */

50     public InetAddress JavaDoc getBroadcast() {
51     return broadcast;
52     }
53
54     /**
55      * Returns the network prefix length for this address. This is also known
56      * as the subnet mask in the context of IPv4 addresses.
57      * Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0)
58      * or 24 (255.255.255.0). <p>
59      * Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)
60      *
61      * @return a <code>short</code> representing the prefix length for the
62      * subnet of that address.
63      */

64      public short getNetworkPrefixLength() {
65     return maskLength;
66     }
67
68     /**
69      * Compares this object against the specified object.
70      * The result is <code>true</code> if and only if the argument is
71      * not <code>null</code> and it represents the same interface address as
72      * this object.
73      * <p>
74      * Two instances of <code>InterfaceAddress</code> represent the same
75      * address if the InetAddress, the prefix length and the broadcast are
76      * the same for both.
77      *
78      * @param obj the object to compare against.
79      * @return <code>true</code> if the objects are the same;
80      * <code>false</code> otherwise.
81      * @see java.net.InterfaceAddress#hashCode()
82      */

83     public boolean equals(Object JavaDoc obj) {
84     if (!(obj instanceof InterfaceAddress JavaDoc)) {
85         return false;
86     }
87     InterfaceAddress JavaDoc cmp = (InterfaceAddress JavaDoc) obj;
88     if ((address != null & cmp.address == null) ||
89         (!address.equals(cmp.address)))
90         return false;
91     if ((broadcast != null & cmp.broadcast == null) ||
92         (!broadcast.equals(cmp.broadcast)))
93         return false;
94     if (maskLength != cmp.maskLength)
95         return false;
96     return true;
97     }
98
99     /**
100      * Returns a hashcode for this Interface address.
101      *
102      * @return a hash code value for this Interface address.
103      */

104     public int hashCode() {
105     return address.hashCode() + ((broadcast != null) ? broadcast.hashCode() : 0) + maskLength;
106     }
107
108     /**
109      * Converts this Interface address to a <code>String</code>. The
110      * string returned is of the form: InetAddress / prefix length [ broadcast address ].
111      *
112      * @return a string representation of this Interface address.
113      */

114     public String JavaDoc toString() {
115     return address + "/" + maskLength + " [" + broadcast + "]";
116     }
117
118 }
119
Popular Tags