Package uk.ipfreely

Class Address<A extends Address<A>>

java.lang.Object
uk.ipfreely.Address<A>
Type Parameters:
A - the address type
All Implemented Interfaces:
Comparable<A>
Direct Known Subclasses:
V4, V6

public abstract class Address<A extends Address<A>> extends Object implements Comparable<A>

Abstract IP address type implemented by V4 and V6. Use Family to create instances.

Inheritance outside the package is not supported. Future implementations may become sealed. Future implementations may become value objects.

Usage Hints

Comparison with InetAddress
Feature Address InetAddress
IP address YES YES
Network I/O YES
DNS host names YES
Numeric type YES
Generic type YES
Canonical string form (RFC5952) YES
Comparable YES
Serializable YES

For InetAddress see the following libraries for overlapping utility methods: Apache Commons Net; Google Guava.

Conversion to/from InetAddress can be performed using:

  • Method Details

    • family

      public abstract Family<A> family()
      Internet protocol family - V4 or V6.
      Returns:
      address family
    • equals

      public abstract boolean equals(Object other)
      Object equality.
      Overrides:
      equals in class Object
      Parameters:
      other - another object or null
      Returns:
      true if same family and value
    • hashCode

      public abstract int hashCode()
      Hash code.
      Overrides:
      hashCode in class Object
      Returns:
      object hash
    • toString

      public abstract String toString()

      String form amenable to parsers.

      Overrides:
      toString in class Object
      Returns:
      canonical form
    • toBigInteger

      public abstract BigInteger toBigInteger()
      The address as a BigInteger.
      Returns:
      the address as a positive integer
    • toBytes

      public abstract byte[] toBytes()
      The address as bytes of length Family.width() / Byte.SIZE.
      Returns:
      the address as a byte sequence, most significant bits first
      See Also:
    • highBits

      public abstract long highBits()
      Useful for efficient conversion.
      Returns:
      the high part of the value
    • lowBits

      public abstract long lowBits()
      Useful for efficient conversion.
      Returns:
      the low part of the value
    • leadingZeros

      public abstract int leadingZeros()
      Returns:
      number of zero bits preceding the highest-order ("leftmost") one-bit
    • trailingZeros

      public abstract int trailingZeros()
      Returns:
      number of zero bits following the lowest-order ("rightmost") one-bit
    • doubleValue

      public abstract double doubleValue()
      Returns:
      approximate floating point value
    • add

      public abstract A add(A addend)
      Addition with overflow.
      Parameters:
      addend - the summand
      Returns:
      sum
    • subtract

      public abstract A subtract(A subtrahend)
      Subtraction with underflow.
      Parameters:
      subtrahend - number to subtract from this
      Returns:
      difference
    • multiply

      public abstract A multiply(A multiplicand)
      Multiplication with overflow.
      Parameters:
      multiplicand - the factor
      Returns:
      product
    • divide

      public abstract A divide(A denominator)
      Division.
      Parameters:
      denominator - the divisor
      Returns:
      quotient
      Throws:
      ArithmeticException - on divide-by-zero
    • mod

      public abstract A mod(A denominator)
      Modulus.
      Parameters:
      denominator - the divisor
      Returns:
      remainder
      Throws:
      ArithmeticException - on divide-by-zero
    • next

      public A next()
      Returns the IP address incremented by one, with overflow.
      Returns:
      the next IP address
    • prev

      public A prev()
      Returns the IP address decremented by one, with underflow.
      Returns:
      the previous IP address
    • and

      public abstract A and(A operand)
      Bitwise AND.
      
           // EXAMPLE
           // LinkLocal is fe80::/10
           private static V6 LL = Family.v6().parse("fe80::");
           private static V6 MASK = Family.v6().subnets().masks().get(10);
      
           public static boolean isLinkLocal(V6 candidate) {
               return MASK.and(candidate).equals(LL);
           }
       
      Parameters:
      operand - the mask address
      Returns:
      the AND'd address
    • or

      public abstract A or(A operand)
      Bitwise OR.
      Parameters:
      operand - the mask address
      Returns:
      the OR'd address
    • xor

      public abstract A xor(A operand)
      Bitwise exclusive OR.
      Parameters:
      operand - the mask address
      Returns:
      the XOR'd address
    • not

      public abstract A not()
      Bitwise NOT.
      
           // EXAMPLE
           // Define 192.168.0.0/24
           int maskBits = 24;
           V4 networkAddress = Family.v4().parse("192.168.0.0");
           // 255.255.255.0
           V4 mask = Family.v4().subnets().masks().get(maskBits);
           // 0.0.0.255
           V4 inverseMask = mask.not()
           // 192.168.0.255
           V4 lastAddress = inverseMask.or(networkAddress);
       
      Returns:
      the complement
    • shift

      public abstract A shift(int bits)
      Bitwise shift. Negative operands shift left. Positive operands shift right. Values exceeding Family.width() overflow as described in The Java Language Specification Java SE 21 Edition 15.19. Shift Operators.
      
           // EXAMPLE
           // 0.0.0.1
           V4 one = Family.v4().parse(1);
           // 0.0.0.2
           V4 two = one.shift(-1);
       
      Parameters:
      bits - number of bits to shift
      Returns:
      shifted value