Class AddressSets

java.lang.Object
uk.ipfreely.sets.AddressSets

public final class AddressSets extends Object

Static factory methods for creating AddressSets, Ranges and Blocks.

  • Method Details

    • of

      @SafeVarargs public static <A extends Address<A>, S extends AddressSet<A>> AddressSet<A> of(S... sets)

      Factory method for AddressSet.

      If all members form a contiguous range, returns Range. If all members form CIDR block, returns Block.

      
           // EXAMPLE
           Range<V4> classA = AddressSets.parseCidr(Family.v4(), "10.0.0.0/8");
           Range<V4> classB = AddressSets.parseCidr(Family.v4(), "176.16.0.0/12");
           Range<V4> classC = AddressSets.parseCidr(Family.v4(), "192.168.0.0/16");
           // RFC-1918 Address Allocation for Private Internets
           AddressSet<V4> privateRanges = AddressSets.of(classA, classB, classC);
       
      Type Parameters:
      A - address type
      S - range type
      Parameters:
      sets - source sets
      Returns:
      union of given sets
    • from

      public static <A extends Address<A>, S extends AddressSet<A>> AddressSet<A> from(Iterable<S> sets)

      Version of of(AddressSet[]) intended for standard collections.

      
           // EXAMPLE
           AddressSet<V4> empty = AddressSets.from(Collections.emptySet());
       
      
           // EXAMPLE
           List<String> addresses = Arrays.asList("192.168.0.1", "192.168.0.10", "192.168.0.11", "192.168.0.12");
           // 4 entries: "192.168.0.1/32", "192.168.0.10/32", "192.168.0.11/32", "192.168.0.12/32"
           Set<Block<V4>> raw = addresses.stream()
                       .map(Family.v4()::parse)
                       .map(AddressSets::address)
                       .collect(Collectors.toSet());
           // 2 entries: "192.168.0.1/32", "192.168.0.10-192.168.0.12"
           Set<Range<V4>> rationalized = AddressSets.from(raw)
                       .ranges()
                       .collect(Collectors.toSet());
       
      Type Parameters:
      A - address type
      S - set type
      Parameters:
      sets - source sets
      Returns:
      union of given sets
    • address

      public static <A extends Address<A>> Block<A> address(A address)
      Single address as Block.
      Type Parameters:
      A - IP version
      Parameters:
      address - IP
      Returns:
      immutable instance
    • block

      public static <A extends Address<A>> Block<A> block(A first, int maskSize)

      Creates Block from network address and mask size. The maskSize must be greater or equal to zero and less than or equal to Family.width(). The mask must cover all the true bits of the address.

      Type Parameters:
      A - the Ip type
      Parameters:
      first - the first IP in the block
      maskSize - the number of mask bits
      Returns:
      the block instance
    • block

      public static <A extends Address<A>> Block<A> block(A first, A last)

      Creates a block from the given addresses which MUST form a valid CIDR block.

      Type Parameters:
      A - IP version
      Parameters:
      first - address
      last - address which must be greater or equal to the first address
      Returns:
      block
    • range

      public static <A extends Address<A>> Range<A> range(A first, A last)
      Creates Range instance. Returns Block when the range is a valid CIDR block.
      Type Parameters:
      A - the IP type
      Parameters:
      first - first element
      last - last element which MUST be greater or equal to first
      Returns:
      an immutable range of IP addresses
    • parseCidr

      public static Block<?> parseCidr(String cidrBlock)
      Parses a CIDR string form as defined by RFC4632. Example: "127.0.0.1/32".
      Parameters:
      cidrBlock - the CIDR notation string
      Returns:
      the block instance
      Throws:
      ParseException - on invalid expression
    • parseCidr

      public static <A extends Address<A>> Block<A> parseCidr(Family<A> family, String cidrBlock)
      As parseCidr(String) with version expectation.
      Type Parameters:
      A - the address type
      Parameters:
      family - the IP family
      cidrBlock - the CIDR notation string
      Returns:
      block instance
      Throws:
      ParseException - on invalid expression or wrong IP version