Class IPortRange

java.lang.Object
com.aoapps.net.IPortRange
All Implemented Interfaces:
Serializable, Comparable<IPortRange>
Direct Known Subclasses:
Port, PortRange

public abstract class IPortRange extends Object implements Comparable<IPortRange>, Serializable
Something that can give a port range.

A single port must be represented by Port while a port range must use PortRange.

Port and PortRange are compatible with equals(java.lang.Object), hashCode(), and compareTo(com.aoapps.net.IPortRange).

Java 1.8: Make this an interface with default methods.

Author:
AO Industries, Inc.
See Also:
  • Field Details

  • Method Details

    • validate

      public static ValidationResult validate(int from, int to, Protocol protocol)
      See Also:
    • valueOf

      public static IPortRange valueOf(int from, int to, Protocol protocol) throws ValidationException
      Throws:
      ValidationException
      See Also:
    • equals

      public abstract boolean equals(Object obj)
      ports and port ranges will never equal each other because port range is forced to have a range larger than one port.
      Overrides:
      equals in class Object
    • hashCode

      public abstract int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public final int compareTo(IPortRange other)
      Ordered by from, to, protocol. The fact that is ordered by "from" is used to break loops, this ordering must not be changed without adjusting other code.
      Specified by:
      compareTo in interface Comparable<IPortRange>
    • toString

      public abstract String toString()
      Overrides:
      toString in class Object
      See Also:
    • getProtocol

      public final Protocol getProtocol()
    • getFrom

      public abstract int getFrom()
      Gets the first port number in the range.
    • getFromPort

      public abstract Port getFromPort()
      Gets the first port number in the range as a Port.
    • getTo

      public abstract int getTo()
      Gets the last port number in the range.
    • getToPort

      public abstract Port getToPort()
      Gets the last port number in the range as a Port.
    • overlaps

      public final boolean overlaps(IPortRange other)
      Checks if this port range is of the same protocol and overlaps the given port range.
    • splitBelow

      public abstract IPortRange splitBelow(int below)
      Returns:
      The part of this port range below, and not including, the given port or null if none.
    • splitAbove

      public abstract IPortRange splitAbove(int above)
      Returns:
      The part of this port range above, and not including, the given port or null if none.
    • coalesce

      public final IPortRange coalesce(IPortRange other)
      Combines this port range with the given port range if possible.
      1. If different protocols are non-overlapping and non-adjacent, returns null.
      2. If the combined range equals this, returns this.
      3. If the combined range equals other, returns other.
      4. Otherwise, returns a new port range covering the full range.
      Returns:
      When the protocols match and port ranges overlap or are adjacent, returns a port range spanning both. null when they cannot be combined.