com.h3c.util.ip

## Class PortNumber

- java.lang.Object
- - com.h3c.util.cache.CacheableDataType
  - - com.h3c.util.ip.UnsignedId
    - - com.h3c.util.ip.UnsignedIntBasedId
      - - com.h3c.util.ip.U16Id
        - - com.h3c.util.ip.PortNumber

- All Implemented Interfaces:
    Serializable, Comparable<PortNumber>

      public final class PortNumber
      extends U16Id
      implements Comparable<PortNumber>

  Represents a port number (unsigned 16-bit).

  All constructors for this class are private. Creating instances of `PortNumber` is done via the static methods on the class.

  Instances of this class are immutable, making them inherently threadsafe.

  This class overrides `UnsignedIntBasedId.equals(java.lang.Object)` and `UnsignedIntBasedId.hashCode()` so that instances play nicely with the Collection classes.

  This class implements the `Comparable` interface to ensure that a sorted list of port numbers is presented in an intuitive order.

  See Also:
    `BigPortNumber`, Serialized Form

- - ### Field Summary

    - ### Fields inherited from class com.h3c.util.ip.U16Id

      `E_BYTES_BAD_LEN, LENGTH_IN_BYTES, MAX_VALUE`

    - ### Fields inherited from class com.h3c.util.ip.UnsignedIntBasedId

      `id`

    - ### Fields inherited from class com.h3c.util.ip.UnsignedId

      `E_BAD, E_NULL_BYTES, E_NULL_STR, E_OOR, MIN_VALUE`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `compareTo(PortNumber o)` Implements the Comparable interface, to return port numbers in natural order.
    `static PortNumber` | `valueOf(byte[] bytes)` Returns an object that represents the port number defined by the specified byte array.
    `static PortNumber` | `valueOf(int port)` Returns an object that represents the port number defined by the specified integer.
    `static PortNumber` | `valueOf(String portStr)` Returns an object that represents the port number defined by the specified string.

    - ### Methods inherited from class com.h3c.util.ip.U16Id

      `rangeCheck, toByteArray`

    - ### Methods inherited from class com.h3c.util.ip.UnsignedIntBasedId

      `equals, hashCode, toInt, toString`

    - ### Methods inherited from class com.h3c.util.ip.UnsignedId

      `parseIntStr, parseLongStr`

    - ### Methods inherited from class com.h3c.util.cache.CacheableDataType

      `getRefQ, keyFromBytes`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Method Detail

    - #### compareTo

          public int compareTo(PortNumber o)

      Implements the Comparable interface, to return port numbers in natural order.
      Specified by:
        `compareTo` in interface `Comparable<PortNumber>`
      Parameters:
        `o` - the other port number
      Returns:
        an integer value indicating relative ordering
      See Also:
        `Comparable.compareTo(T)`

    - #### valueOf

          public static PortNumber valueOf(int port)

      Returns an object that represents the port number defined by the specified integer.
      Parameters:
        `port` - the port number
      Returns:
        a PortNumber instance
      Throws:
        `IllegalArgumentException` - if the port number is invalid

    - #### valueOf

          public static PortNumber valueOf(String portStr)

      Returns an object that represents the port number defined by the specified string. The string is parsed as a base-10 integer.
      Parameters:
        `portStr` - the port number as a string
      Returns:
        a PortNumber instance
      Throws:
        `IllegalArgumentException` - if the string is invalid
        `NullPointerException` - if the string is null

    - #### valueOf

          public static PortNumber valueOf(byte[] bytes)

      Returns an object that represents the port number defined by the specified byte array. The array is expected to be `U16Id.LENGTH_IN_BYTES` bytes long.
      Parameters:
        `bytes` - the encoded id
      Returns:
        a PortNumber instance
      Throws:
        `NullPointerException` - if the byte array is null
        `IllegalArgumentException` - if the byte array is not 2 bytes long

Copyright © 2014. All Rights Reserved.