\section{Port Number}

A Port Number is a 16 bit number.  Its binary representation is a Big Endian 16
bit unsigned integer (2 bytes).

\begin{code}
{-# LANGUAGE DeriveDataTypeable         #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StrictData                 #-}
module Network.Tox.NodeInfo.PortNumber where

import           Data.Binary               (Binary)
import           Data.MessagePack          (MessagePack)
import           Data.Typeable             (Typeable)
import           Data.Word                 (Word16)
import           GHC.Generics              (Generic)
import           Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)


{-------------------------------------------------------------------------------
 -
 - :: Implementation.
 -
 ------------------------------------------------------------------------------}


newtype PortNumber = PortNumber Word16
  deriving
    ( (forall x. PortNumber -> Rep PortNumber x)
-> (forall x. Rep PortNumber x -> PortNumber) -> Generic PortNumber
forall x. Rep PortNumber x -> PortNumber
forall x. PortNumber -> Rep PortNumber x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PortNumber x -> PortNumber
$cfrom :: forall x. PortNumber -> Rep PortNumber x
Generic, Typeable
    , PortNumber -> PortNumber -> Bool
(PortNumber -> PortNumber -> Bool)
-> (PortNumber -> PortNumber -> Bool) -> Eq PortNumber
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PortNumber -> PortNumber -> Bool
$c/= :: PortNumber -> PortNumber -> Bool
== :: PortNumber -> PortNumber -> Bool
$c== :: PortNumber -> PortNumber -> Bool
Eq, Eq PortNumber
Eq PortNumber
-> (PortNumber -> PortNumber -> Ordering)
-> (PortNumber -> PortNumber -> Bool)
-> (PortNumber -> PortNumber -> Bool)
-> (PortNumber -> PortNumber -> Bool)
-> (PortNumber -> PortNumber -> Bool)
-> (PortNumber -> PortNumber -> PortNumber)
-> (PortNumber -> PortNumber -> PortNumber)
-> Ord PortNumber
PortNumber -> PortNumber -> Bool
PortNumber -> PortNumber -> Ordering
PortNumber -> PortNumber -> PortNumber
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PortNumber -> PortNumber -> PortNumber
$cmin :: PortNumber -> PortNumber -> PortNumber
max :: PortNumber -> PortNumber -> PortNumber
$cmax :: PortNumber -> PortNumber -> PortNumber
>= :: PortNumber -> PortNumber -> Bool
$c>= :: PortNumber -> PortNumber -> Bool
> :: PortNumber -> PortNumber -> Bool
$c> :: PortNumber -> PortNumber -> Bool
<= :: PortNumber -> PortNumber -> Bool
$c<= :: PortNumber -> PortNumber -> Bool
< :: PortNumber -> PortNumber -> Bool
$c< :: PortNumber -> PortNumber -> Bool
compare :: PortNumber -> PortNumber -> Ordering
$ccompare :: PortNumber -> PortNumber -> Ordering
$cp1Ord :: Eq PortNumber
Ord
    , Int -> PortNumber -> ShowS
[PortNumber] -> ShowS
PortNumber -> String
(Int -> PortNumber -> ShowS)
-> (PortNumber -> String)
-> ([PortNumber] -> ShowS)
-> Show PortNumber
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PortNumber] -> ShowS
$cshowList :: [PortNumber] -> ShowS
show :: PortNumber -> String
$cshow :: PortNumber -> String
showsPrec :: Int -> PortNumber -> ShowS
$cshowsPrec :: Int -> PortNumber -> ShowS
Show, ReadPrec [PortNumber]
ReadPrec PortNumber
Int -> ReadS PortNumber
ReadS [PortNumber]
(Int -> ReadS PortNumber)
-> ReadS [PortNumber]
-> ReadPrec PortNumber
-> ReadPrec [PortNumber]
-> Read PortNumber
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PortNumber]
$creadListPrec :: ReadPrec [PortNumber]
readPrec :: ReadPrec PortNumber
$creadPrec :: ReadPrec PortNumber
readList :: ReadS [PortNumber]
$creadList :: ReadS [PortNumber]
readsPrec :: Int -> ReadS PortNumber
$creadsPrec :: Int -> ReadS PortNumber
Read
    , Get PortNumber
[PortNumber] -> Put
PortNumber -> Put
(PortNumber -> Put)
-> Get PortNumber -> ([PortNumber] -> Put) -> Binary PortNumber
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [PortNumber] -> Put
$cputList :: [PortNumber] -> Put
get :: Get PortNumber
$cget :: Get PortNumber
put :: PortNumber -> Put
$cput :: PortNumber -> Put
Binary
    , Integer -> PortNumber
PortNumber -> PortNumber
PortNumber -> PortNumber -> PortNumber
(PortNumber -> PortNumber -> PortNumber)
-> (PortNumber -> PortNumber -> PortNumber)
-> (PortNumber -> PortNumber -> PortNumber)
-> (PortNumber -> PortNumber)
-> (PortNumber -> PortNumber)
-> (PortNumber -> PortNumber)
-> (Integer -> PortNumber)
-> Num PortNumber
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> PortNumber
$cfromInteger :: Integer -> PortNumber
signum :: PortNumber -> PortNumber
$csignum :: PortNumber -> PortNumber
abs :: PortNumber -> PortNumber
$cabs :: PortNumber -> PortNumber
negate :: PortNumber -> PortNumber
$cnegate :: PortNumber -> PortNumber
* :: PortNumber -> PortNumber -> PortNumber
$c* :: PortNumber -> PortNumber -> PortNumber
- :: PortNumber -> PortNumber -> PortNumber
$c- :: PortNumber -> PortNumber -> PortNumber
+ :: PortNumber -> PortNumber -> PortNumber
$c+ :: PortNumber -> PortNumber -> PortNumber
Num, Enum PortNumber
Real PortNumber
Real PortNumber
-> Enum PortNumber
-> (PortNumber -> PortNumber -> PortNumber)
-> (PortNumber -> PortNumber -> PortNumber)
-> (PortNumber -> PortNumber -> PortNumber)
-> (PortNumber -> PortNumber -> PortNumber)
-> (PortNumber -> PortNumber -> (PortNumber, PortNumber))
-> (PortNumber -> PortNumber -> (PortNumber, PortNumber))
-> (PortNumber -> Integer)
-> Integral PortNumber
PortNumber -> Integer
PortNumber -> PortNumber -> (PortNumber, PortNumber)
PortNumber -> PortNumber -> PortNumber
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: PortNumber -> Integer
$ctoInteger :: PortNumber -> Integer
divMod :: PortNumber -> PortNumber -> (PortNumber, PortNumber)
$cdivMod :: PortNumber -> PortNumber -> (PortNumber, PortNumber)
quotRem :: PortNumber -> PortNumber -> (PortNumber, PortNumber)
$cquotRem :: PortNumber -> PortNumber -> (PortNumber, PortNumber)
mod :: PortNumber -> PortNumber -> PortNumber
$cmod :: PortNumber -> PortNumber -> PortNumber
div :: PortNumber -> PortNumber -> PortNumber
$cdiv :: PortNumber -> PortNumber -> PortNumber
rem :: PortNumber -> PortNumber -> PortNumber
$crem :: PortNumber -> PortNumber -> PortNumber
quot :: PortNumber -> PortNumber -> PortNumber
$cquot :: PortNumber -> PortNumber -> PortNumber
$cp2Integral :: Enum PortNumber
$cp1Integral :: Real PortNumber
Integral, Num PortNumber
Ord PortNumber
Num PortNumber
-> Ord PortNumber -> (PortNumber -> Rational) -> Real PortNumber
PortNumber -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: PortNumber -> Rational
$ctoRational :: PortNumber -> Rational
$cp2Real :: Ord PortNumber
$cp1Real :: Num PortNumber
Real, PortNumber
PortNumber -> PortNumber -> Bounded PortNumber
forall a. a -> a -> Bounded a
maxBound :: PortNumber
$cmaxBound :: PortNumber
minBound :: PortNumber
$cminBound :: PortNumber
Bounded, Int -> PortNumber
PortNumber -> Int
PortNumber -> [PortNumber]
PortNumber -> PortNumber
PortNumber -> PortNumber -> [PortNumber]
PortNumber -> PortNumber -> PortNumber -> [PortNumber]
(PortNumber -> PortNumber)
-> (PortNumber -> PortNumber)
-> (Int -> PortNumber)
-> (PortNumber -> Int)
-> (PortNumber -> [PortNumber])
-> (PortNumber -> PortNumber -> [PortNumber])
-> (PortNumber -> PortNumber -> [PortNumber])
-> (PortNumber -> PortNumber -> PortNumber -> [PortNumber])
-> Enum PortNumber
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: PortNumber -> PortNumber -> PortNumber -> [PortNumber]
$cenumFromThenTo :: PortNumber -> PortNumber -> PortNumber -> [PortNumber]
enumFromTo :: PortNumber -> PortNumber -> [PortNumber]
$cenumFromTo :: PortNumber -> PortNumber -> [PortNumber]
enumFromThen :: PortNumber -> PortNumber -> [PortNumber]
$cenumFromThen :: PortNumber -> PortNumber -> [PortNumber]
enumFrom :: PortNumber -> [PortNumber]
$cenumFrom :: PortNumber -> [PortNumber]
fromEnum :: PortNumber -> Int
$cfromEnum :: PortNumber -> Int
toEnum :: Int -> PortNumber
$ctoEnum :: Int -> PortNumber
pred :: PortNumber -> PortNumber
$cpred :: PortNumber -> PortNumber
succ :: PortNumber -> PortNumber
$csucc :: PortNumber -> PortNumber
Enum)

instance MessagePack PortNumber


{-------------------------------------------------------------------------------
 -
 - :: Tests.
 -
 ------------------------------------------------------------------------------}


instance Arbitrary PortNumber where
  arbitrary :: Gen PortNumber
arbitrary =
    Word16 -> PortNumber
PortNumber (Word16 -> PortNumber)
-> (Integer -> Word16) -> Integer -> PortNumber
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Word16
forall a. Num a => Integer -> a
fromInteger (Integer -> PortNumber) -> Gen Integer -> Gen PortNumber
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Integer
forall a. Arbitrary a => Gen a
arbitrary
\end{code}