Advertisement
Guest User

Untitled

a guest
Dec 5th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.66 KB | None | 0 0
  1. import scala.math.Ordered
  2.  
  3. case class IPv4Addr(val address: Int)
  4.   extends AnyVal
  5.   with Product4[Byte, Byte, Byte, Byte]
  6.   // with Ordered[IPv4Addr]
  7. {
  8.   @inline def _1: Byte = (address & 0xff).toByte
  9.   @inline def _2: Byte = ((address >>> 8) & 0xff).toByte
  10.   @inline def _3: Byte = ((address >>> 16) & 0xff).toByte
  11.   @inline def _4: Byte = ((address >>> 24) & 0xff).toByte
  12.  
  13.   def toBytes: Array[Byte] = Array(_1, _2, _3, _4)
  14.  
  15.   @inline private def printByte(byte: Byte): String =
  16.     (byte.toInt + 128).toString
  17.  
  18.   override def toString(): String =
  19.     s"${printByte(_1)}.${printByte(_2)}.${printByte(_3)}.${printByte(_4)}"
  20. }
  21.  
  22. object IPv4Addr {
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement