com.scalacraft.domain.v2

binary

package binary

Visibility
  1. Public
  2. All

Type Members

  1. case class Octet extends Product with Serializable

    An Octet represents an integer in the range [0, 255].

    An Octet represents an integer in the range [0, 255].

    The following constraints hold for instances of this class,

    - octet is in the range [0, 255]

    An instance can be created using a suitable overload of the opt method.

    val octetOpt1: Option[Octet] = Octet.opt(117)
    val octetOpt2: Option[Octet] = Octet.opt(0x75)
    val octetOpt3: Option[Octet] = Octet.opt("fe") // Decimal 254
    val octetOpt4: Option[Octet] = Octet.opt("d") // Decimal 13
    val octetOpt5: Option[Octet] = Octet.opt("12") // Decimal 18

    Note that the string variant takes either one or two hex characters.

    When any class constraint is violated the result is None.

    val octetOpt: Option[Octet] = Octet.opt("NaN") // None
    Pattern Matching

    Pattern matching is supported as the following examples demonstrate,

    7 match {
      case Octet(n) => n // 7
      case _ => None
    }

    The match target can be a string,

    val s: String = "20"
    
    s match {
      case Octet(n) => n  // 32
      case _ => None
    }

    Invalid octets are not matched,

    -129 match {
      case Octet(n) => n
      case _ => None // None
    }
    Implicit Conversions

    Implicit conversions exists which allow an instance of Octet to be used when an Int or String is required.

    val Some(hi) = Octet.opt(4)
    val Some(lo) = Octet.opt(3)
    val w: Int = 256 * hi + lo // 0x0403

    A conversion to the unconstrained version of this class is also available.

  2. case class OctetPair extends Product with Serializable

    An OctetPair represents two Octets.

    An OctetPair represents two Octets.

    The following constraints hold for instances of this class,

    - hi is not null - lo is not null

    An instance can be created using a suitable overload of the opt method.

    val op1: Option[OctetPair] = OctetPair.opt(34540)  // OctetPair(134, 236) = 34540
    val op2: Option[OctetPair] = OctetPair.opt(0x86ec) // OctetPair(134, 236) = 34540
    val op3: Option[OctetPair] = OctetPair.opt("d") // OctetPair(0, 13)
    val op3: Option[OctetPair] = OctetPair.opt("fe") // OctetPair(0, 254)
    val op5: Option[OctetPair] = OctetPair.opt("012") // Decimal 18
    val op4: Option[OctetPair] = OctetPair.opt("f11d") // Decimal 61725

    Note that the string variant takes between one to four hex characters.

    When any class constraint is violated the result is None.

    val op1: Option[OctetPair] = OctetPair.opt("NaN") // None
    val op2: Option[OctetPair] = OctetPair.opt(0xf1234) // None
    Pattern Matching

    Pattern matching is supported as the following examples demonstrate,

    0x3490 match {
      case OctetPair(hi, lo) => (hi, lo) // (Octet(0x34), Octet(0x90)
      case _ => None
    }

    The match target can be a string,

    val s: String = "4020"
    
    s match {
      case OctetPair(hi, lo) => (hi, lo) // (Octet(0x40), Octet(0x20)
      case _ => None
    }

    Invalid octet pairs are not matched,

    -129 match {
      case OctetPair(hi, lo) => (hi, lo)
      case _ => None // None
    }
    Implicit Conversions

    Implicit conversions are supplied which allow an instance of OctetPair to be used when an Int or String is required.

    val Some(pair) = OctetPair.opt(0x40cc)
    val w: Int = 1 + pair // 0x40cd
    val Some(pair) = OctetPair.opt("f")
    val s: String = pair: String // 000f

    A conversion to the unconstrained version of this class is also available.

Value Members

  1. object Octet extends Serializable

  2. object OctetPair extends Serializable

  3. package unconstrained

Ungrouped