Java InetAddress inetAddressesCompare(final InetAddress[] addresses1, final InetAddress[] addresses2)

Here you can find the source of inetAddressesCompare(final InetAddress[] addresses1, final InetAddress[] addresses2)

Description

inet Addresses Compare

License

LGPL

Declaration

public static int inetAddressesCompare(final InetAddress[] addresses1, final InetAddress[] addresses2) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  . j a v a2s .  co  m
 * Cacheonix Systems licenses this file to You under the LGPL 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.cacheonix.org/products/cacheonix/license-lgpl-2.1.htm
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.InetAddress;

import java.util.Arrays;

public class Main {
    public static int inetAddressesCompare(final InetAddress[] addresses1, final InetAddress[] addresses2) {

        if (Arrays.equals(addresses1, addresses2)) {
            return 0;
        }

        if (addresses1 == null || addresses2 == null) {
            return -1;
        }

        if (addresses1.length > addresses2.length) {
            return 1;
        }

        if (addresses1.length < addresses2.length) {
            return -1;
        }

        for (int i = 0; i < addresses1.length; i++) {
            final InetAddress a1 = addresses1[i];
            final InetAddress a2 = addresses2[i];

            final int result = inetAddressCompare(a1, a2);
            if (result > 0) {
                return 1;
            }
            if (result < 0) {
                return -1;
            }
        }
        return 0;
    }

    public static int inetAddressCompare(final InetAddress address1, final InetAddress address2) {

        if (address1 == null || address2 == null) {
            return -1;
        }

        if (address1.equals(address2)) {
            return 0;
        }

        final byte[] ab1 = address1.getAddress();
        final byte[] ab2 = address2.getAddress();

        if (ab1.length > ab2.length) {
            return 1;
        }

        if (ab1.length < ab1.length) {
            return -1;
        }

        for (int i = 0; i < ab1.length; i++) {
            final byte b1 = ab1[i];
            final byte b2 = ab2[i];
            if (b1 > b2) {
                return 1;
            }
            if (b1 < b2) {
                return -1;
            }
        }
        return 0;
    }
}

Related

  1. hasIp(InetAddress ip)
  2. hasLocalScope(InetAddress addr)
  3. increment(InetAddress address)
  4. inetAddressCompare(final InetAddress address1, final InetAddress address2)
  5. inetAddressesComparator(final boolean sameHost)
  6. inetAddressFromThriftString(String ipAddress)
  7. inetAddressGt(InetAddress a, InetAddress b)
  8. inetAddrToByteArray(InetAddress a)
  9. newInterfaceAddress(InetAddress inetAddress, short maskLength)