Package uk.ipfreely

Class Family<A extends Address<A>>

java.lang.Object
uk.ipfreely.Family<A>
Type Parameters:
A - V4 or V6

public abstract class Family<A extends Address<A>> extends Object

Address factory and utility type for IpV4 and IpV6 obtained via v4() and v6().

Inheritance outside the package is not supported. Instances have identity equality. Future implementations may become sealed.

  • Method Details

    • v4

      public static Family<V4> v4()
      IPv4.
      
           // EXAMPLE
           V4 localhost = Family.v4().parse("127.0.0.1");
       
      Returns:
      IPv4 family of addresses
    • v6

      public static Family<V6> v6()
      IPv6.
      Returns:
      IPv6 family of addresses
    • toString

      public abstract String toString()
      Informational.
      Overrides:
      toString in class Object
      Returns:
      family as string
    • parse

      public abstract A parse(long high, long low)

      IP address instantiation from primitives.

      Only values up to (0L, 0xffffffffL) are valid for IPv4.

      All values are valid for IPv6.

      Parameters:
      high - high bits of the IP address
      low - low bits of the IP address
      Returns:
      the address
      Throws:
      ParseException - on invalid values
    • parse

      public abstract A parse(CharSequence candidate)
      Parses an IP address string. TODO: supported string forms.
      Parameters:
      candidate - valid IP address in this family
      Returns:
      parsed address
      Throws:
      ParseException - on invalid address
    • parse

      public abstract A parse(byte... address)
      Argument must be bitWidth() / 8 bytes in length.
      Parameters:
      address - the address as bytes
      Returns:
      parsed address
      Throws:
      ParseException - if the array is not the expected length
    • parse

      public abstract A parse(BigInteger address)
      Enables the conversion from BigInteger to the Address type. The largest acceptable value is max().toBigInteger().
      Parameters:
      address - must be between zero and the maximum value inclusive
      Returns:
      the new address
      Throws:
      ParseException - when the argument is out of range
      See Also:
    • parse

      public abstract A parse(int unsigned)
      Convenience method for creating Address from number. All values of int are valid for this method. Every value in the IPv4 range can be created with this method.
      Parameters:
      unsigned - integer treated as unsigned value
      Returns:
      IP address
    • width

      public abstract int width()
      Width of the IP address type in bits.
      Returns:
      32 (V4) or 128 (V6)
    • type

      public abstract Class<A> type()
      IP address type.
      Returns:
      V4.class or V6.class
    • subnets

      public Subnets<A> subnets()
      Subnet utilities.
      Returns:
      subnet methods for this family
    • min

      public A min()
      Zero.
      Returns:
      "0.0.0.0" (V4) or "::" (V6)
    • max

      public A max()
      Maximum IP value.
      Returns:
      "255.255.255.255" (V4) or "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" (V6)
    • regex

      public abstract String regex()

      Regular expression for detecting IP addresses in this family.

      
           // EXAMPLE
           String startOfString = "^";
           String endOfString = "$";
           String or = "|";
           String v4r = Family.v4().regex();
           String v6r = Family.v6().regex();
           Pattern addressPattern = Pattern.compile(startOfString + v4r + or + v6r + endOfString);
      
           for (String candidate : new String[]{"172.0.0.1", "foo", "::1",}) {
               Matcher m = addressPattern.matcher(candidate);
               if (m.matches()) {
                   System.out.println(Family.unknown(candidate).family() + "\t" + candidate);
               } else {
                   System.out.println("none\t" + candidate);
               }
           }
       
      Returns:
      regular expression for matching address patterns
      See Also:
    • unknown

      public static Address<?> unknown(CharSequence candidate)
      Uses heuristics to detect IP address family and calls parse(CharSequence).
      Parameters:
      candidate - IP address
      Returns:
      instance of V4 or V6
      Throws:
      ParseException - on invalid address
      See Also:
    • unknown

      public static Address<?> unknown(byte... address)
      Uses array length to detect IP address family and calls parse(byte...).
      Parameters:
      address - an IPv4 or IPv6 address in byte form
      Returns:
      parsed address
      Throws:
      ParseException - if array is not 4 (V4) or 16 (V6) bytes in length
      See Also: