Package uk.ipfreely

Class V4

All Implemented Interfaces:
Comparable<V4>

public final class V4 extends Address<V4>
Immutable IPv4 Address and 32-bit unsigned integer value. Use Family.v4() to create values.
  • Method Details

    • family

      public Family<V4> family()
      Description copied from class: Address
      Internet protocol family - V4 or V6.
      Specified by:
      family in class Address<V4>
      Returns:
      address family
    • equals

      public boolean equals(Object other)
      Description copied from class: Address
      Object equality.
      Specified by:
      equals in class Address<V4>
      Parameters:
      other - another object or null
      Returns:
      true if same family and value
    • hashCode

      public int hashCode()
      Description copied from class: Address
      Hash code.
      Specified by:
      hashCode in class Address<V4>
      Returns:
      object hash
    • toString

      public String toString()
      IPv4 address in dotted quad notation.
      Specified by:
      toString in class Address<V4>
      Returns:
      IP address as four base10 integers separated by periods
    • compareTo

      public int compareTo(V4 o)
      Useful for sorting.
      Parameters:
      o - another address
      Returns:
      negative, zero, or positive integer as this is less than, equal to, or greater than other address
    • toBigInteger

      public BigInteger toBigInteger()
      Description copied from class: Address
      The address as a BigInteger.
      Specified by:
      toBigInteger in class Address<V4>
      Returns:
      the address as a positive integer
    • toBytes

      public byte[] toBytes()
      Description copied from class: Address
      The address as bytes of length Family.width() / Byte.SIZE.
      Specified by:
      toBytes in class Address<V4>
      Returns:
      the address as a byte sequence, most significant bits first
      See Also:
    • highBits

      public long highBits()
      The high bits are out of range for IPv4.
      Specified by:
      highBits in class Address<V4>
      Returns:
      zero
    • lowBits

      public long lowBits()
      The IP address as n long between 0L and 0xFFFFFFFFL.
      Specified by:
      lowBits in class Address<V4>
      Returns:
      the IP address as a long
    • leadingZeros

      public int leadingZeros()
      Description copied from class: Address
      Specified by:
      leadingZeros in class Address<V4>
      Returns:
      number of zero bits preceding the highest-order ("leftmost") one-bit
    • trailingZeros

      public int trailingZeros()
      Description copied from class: Address
      Specified by:
      trailingZeros in class Address<V4>
      Returns:
      number of zero bits following the lowest-order ("rightmost") one-bit
    • doubleValue

      public double doubleValue()
      Description copied from class: Address
      Specified by:
      doubleValue in class Address<V4>
      Returns:
      approximate floating point value
    • add

      public V4 add(V4 addend)
      Description copied from class: Address
      Addition with overflow.
      Specified by:
      add in class Address<V4>
      Parameters:
      addend - the summand
      Returns:
      sum
    • subtract

      public V4 subtract(V4 subtrahend)
      Description copied from class: Address
      Subtraction with underflow.
      Specified by:
      subtract in class Address<V4>
      Parameters:
      subtrahend - number to subtract from this
      Returns:
      difference
    • multiply

      public V4 multiply(V4 multiplicand)
      Description copied from class: Address
      Multiplication with overflow.
      Specified by:
      multiply in class Address<V4>
      Parameters:
      multiplicand - the factor
      Returns:
      product
    • divide

      public V4 divide(V4 denominator)
      Description copied from class: Address
      Division.
      Specified by:
      divide in class Address<V4>
      Parameters:
      denominator - the divisor
      Returns:
      quotient
    • mod

      public V4 mod(V4 denominator)
      Description copied from class: Address
      Modulus.
      Specified by:
      mod in class Address<V4>
      Parameters:
      denominator - the divisor
      Returns:
      remainder
    • and

      public V4 and(V4 operand)
      Description copied from class: Address
      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);
           }
       
      Specified by:
      and in class Address<V4>
      Parameters:
      operand - the mask address
      Returns:
      the AND'd address
    • or

      public V4 or(V4 operand)
      Description copied from class: Address
      Bitwise OR.
      Specified by:
      or in class Address<V4>
      Parameters:
      operand - the mask address
      Returns:
      the OR'd address
    • xor

      public V4 xor(V4 operand)
      Description copied from class: Address
      Bitwise exclusive OR.
      Specified by:
      xor in class Address<V4>
      Parameters:
      operand - the mask address
      Returns:
      the XOR'd address
    • not

      public V4 not()
      Description copied from class: Address
      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);
       
      Specified by:
      not in class Address<V4>
      Returns:
      the complement
    • shift

      public V4 shift(int bits)
      Description copied from class: Address
      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);
       
      Specified by:
      shift in class Address<V4>
      Parameters:
      bits - -31 to 31
      Returns:
      shifted number