jdk/src/share/classes/java/net/InterfaceAddress.java
author juh
Tue, 30 Jul 2013 11:04:19 -0700
changeset 19069 1d9cb0d080e3
parent 5506 202f599c92aa
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
8021833: javadoc cleanup in java.net Summary: <code> and <tt> converted to {@code }; package.html to package-info.java Reviewed-by: darcy, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class represents a Network Interface address. In short it's an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * IP address, a subnet mask and a broadcast address when the address is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * an IPv4 one. An IP address and a network prefix length in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @see java.net.NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class InterfaceAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private InetAddress address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private Inet4Address broadcast = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private short        maskLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * Package private constructor. Can't be built directly, instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * obtained through the NetworkInterface class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    InterfaceAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    50
     * Returns an {@code InetAddress} for this address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    52
     * @return the {@code InetAddress} for this address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public InetAddress getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    59
     * Returns an {@code InetAddress} for the brodcast address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * for this InterfaceAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Only IPv4 networks have broadcast address therefore, in the case
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    63
     * of an IPv6 network, {@code null} will be returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    65
     * @return the {@code InetAddress} representing the broadcast
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    66
     *         address or {@code null} if there is no broadcast address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public InetAddress getBroadcast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return broadcast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns the network prefix length for this address. This is also known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * as the subnet mask in the context of IPv4 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * or 24 (255.255.255.0). <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    79
     * @return a {@code short} representing the prefix length for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *         subnet of that address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     public short getNetworkPrefixLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return maskLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Compares this object against the specified object.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    88
     * The result is {@code true} if and only if the argument is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    89
     * not {@code null} and it represents the same interface address as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    92
     * Two instances of {@code InterfaceAddress} represent the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * address if the InetAddress, the prefix length and the broadcast are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * the same for both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param   obj   the object to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    97
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    98
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @see     java.net.InterfaceAddress#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (!(obj instanceof InterfaceAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        InterfaceAddress cmp = (InterfaceAddress) obj;
84
396d36146411 6628576: InterfaceAddress.equals() NPE when broadcast field == null
chegar
parents: 2
diff changeset
   106
        if ( !(address == null ? cmp.address == null : address.equals(cmp.address)) )
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return false;
84
396d36146411 6628576: InterfaceAddress.equals() NPE when broadcast field == null
chegar
parents: 2
diff changeset
   108
        if ( !(broadcast  == null ? cmp.broadcast == null : broadcast.equals(cmp.broadcast)) )
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (maskLength != cmp.maskLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Returns a hashcode for this Interface address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return  a hash code value for this Interface address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return address.hashCode() + ((broadcast != null) ? broadcast.hashCode() : 0) + maskLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   125
     * Converts this Interface address to a {@code String}. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * string returned is of the form: InetAddress / prefix length [ broadcast address ].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return  a string representation of this Interface address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return address + "/" + maskLength + " [" + broadcast + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}