Module inet.ipaddr

Class Partition<E>

java.lang.Object
inet.ipaddr.format.util.Partition<E>
Type Parameters:
E - the type being partitioned

public class Partition<E> extends Object
Represents a partition of an element, such as a subnet, into one or elements of the same type that represent the same set of values.

For instance, it can represent the partition of a subnet into prefix blocks, or a partition into individual addresses.

You can use the methods partitionWithSingleBlockSize(AddressSegmentSeries) or partitionWithSpanningBlocks(IPAddress) to create a partition, or you can create your own.

You can use this class with the trie classes to easily partition an address for applying an operation to the trie. Given an address or subnet s, you can partition into addresses or blocks that can used with a trie class using Partition.partitionWithSpanningBlocks(s) or Partition.partitionWithSingleBlockSize(s). Once you have your partition, you choose a method in the Partition class whose argument type matches the signature of the trie method you wish to use.

For instance, you can use methods like AddressTrieOps.contains(inet.ipaddr.Address) or AddressTrieOps.remove(inet.ipaddr.Address) with predicateForAny(Predicate) or predicateForEach(Predicate) since the contains or remove method matches the Predicate interface. Methods that return non-boolean values would match the applyForEach(Function) or forEach(Consumer) methods, as in the following code example for a given subnet s of type E: Map<E, TrieNode<E>> all = Partition.partitionWithSingleBlockSize(s).applyForEach(trie::getNode)

Author:
scfoley
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final Iterator<? extends E>
    An iterator supplying the partitioned addresses.
    The address or block count.
    final E
    The partitioned address.
    final E
    A field containing a partition into a single value.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Partition(E original)
     
    Partition(E original, E single)
     
    Partition(E original, Iterator<? extends E> blocks, int count)
     
    Partition(E original, Iterator<? extends E> blocks, BigInteger count)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <R> Map<E,R>
    applyForEach(Function<? super E,? extends R> func)
    Supplies to the given function each element of this partition, inserting non-null return values into the returned map.
    static <E extends Address>
    E
    If the given address is a single prefix block, it is returned.
    void
    forEach(Consumer<? super E> action)
    Supplies to the consumer each element of this partition.
    static <E extends AddressSegmentSeries>
    Partition<E>
    Partitions the address series into prefix blocks and single addresses.
    static <E extends IPAddress>
    Partition<E>
    Partitions the address series into prefix blocks and single addresses.
    boolean
    predicateForAny(Predicate<? super E> predicate)
    Applies the operation to each element of the partition, returning true if the given predicate returns true for any of the elements.
    boolean
    predicateForAny(Predicate<? super E> predicate, boolean returnEarly)
    Applies the operation to each element of the partition, returning true if the given predicate returns true for any of the elements.
    boolean
    predicateForEach(Predicate<? super E> predicate)
    Applies the operation to each element of the partition, returning true if they all return true, false otherwise
    boolean
    predicateForEach(Predicate<? super E> predicate, boolean returnEarly)
    Applies the operation to each element of the partition, returning true if they all return true, false otherwise

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • original

      public final E original
      The partitioned address.
    • single

      public final E single
      A field containing a partition into a single value.

      When blocks is null, the partition result is stored in this field. This will match original in terms of equality, but might not have the same prefix length.

    • blocks

      public final Iterator<? extends E> blocks
      An iterator supplying the partitioned addresses.

      When single is null, the partition result is stored in this field.

      If the partition result is multiple addresses or blocks, they are supplied by this iterator.

      If the partition result is just a single address, the result may be supplied by this iterator, or optionally this iterator may be null, in which case the result is given by single.

    • count

      public final BigInteger count
      The address or block count.

      If the result is greater than 1, the blocks are supplied by blocks. If the result is 1, then the single block or address is supplied by blocks if it is non-null, otherwise the single block or address is in single.

  • Constructor Details

    • Partition

      public Partition(E original)
    • Partition

      public Partition(E original, E single)
    • Partition

      public Partition(E original, Iterator<? extends E> blocks, BigInteger count)
    • Partition

      public Partition(E original, Iterator<? extends E> blocks, int count)
  • Method Details

    • applyForEach

      public <R> Map<E,R> applyForEach(Function<? super E,? extends R> func)
      Supplies to the given function each element of this partition, inserting non-null return values into the returned map.
      Type Parameters:
      R -
      Parameters:
      func -
      Returns:
    • forEach

      public void forEach(Consumer<? super E> action)
      Supplies to the consumer each element of this partition.
      Parameters:
      action -
    • predicateForEach

      public boolean predicateForEach(Predicate<? super E> predicate)
      Applies the operation to each element of the partition, returning true if they all return true, false otherwise
      Parameters:
      predicate -
      Returns:
    • predicateForEach

      public boolean predicateForEach(Predicate<? super E> predicate, boolean returnEarly)
      Applies the operation to each element of the partition, returning true if they all return true, false otherwise
      Parameters:
      predicate -
      returnEarly - returns as soon as one application of the predicate returns false (determining the overall result)
      Returns:
    • predicateForAny

      public boolean predicateForAny(Predicate<? super E> predicate, boolean returnEarly)
      Applies the operation to each element of the partition, returning true if the given predicate returns true for any of the elements.
      Parameters:
      predicate -
      returnEarly - returns as soon as one call to the predicate returns true
      Returns:
    • predicateForAny

      public boolean predicateForAny(Predicate<? super E> predicate)
      Applies the operation to each element of the partition, returning true if the given predicate returns true for any of the elements.
      Parameters:
      predicate -
      Returns:
    • partitionWithSpanningBlocks

      public static <E extends IPAddress> Partition<E> partitionWithSpanningBlocks(E newAddr)
      Partitions the address series into prefix blocks and single addresses.

      If null is returned, the argument is already an individual address or prefix block.

      This method iterates through a list of prefix blocks of different sizes that span the entire subnet.

      Parameters:
      newAddr -
      Returns:
    • partitionWithSingleBlockSize

      public static <E extends AddressSegmentSeries> Partition<E> partitionWithSingleBlockSize(E newAddr)
      Partitions the address series into prefix blocks and single addresses.

      If null is returned, the argument is already an individual address or prefix block.

      This method chooses the maximum block size for a list of prefix blocks contained by the address or subnet, and then iterates to produce blocks of that size.

      Parameters:
      newAddr -
      Returns:
    • checkBlockOrAddress

      public static <E extends Address> E checkBlockOrAddress(E addr)
      If the given address is a single prefix block, it is returned. If it can be converted to a single prefix block or address (by adjusting the prefix length), the converted block is returned. Otherwise, null is returned.
      Type Parameters:
      E -
      Parameters:
      addr -
      Returns: