Skip to content

Commit

Permalink
Add support for {Gibi|Mebi|Kilo|etc}bits (typelevel#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Dobel committed Sep 17, 2018
1 parent ed961e4 commit 4624503
Show file tree
Hide file tree
Showing 4 changed files with 539 additions and 3 deletions.
149 changes: 147 additions & 2 deletions shared/src/main/scala/squants/information/DataRate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import squants._
import squants.time.TimeDerivative

/**
* Represnets a rate of transfer of information
* Represents a rate of transfer of information
*/
final class DataRate private(val value: Double, val unit: DataRateUnit)
extends Quantity[DataRate]
Expand Down Expand Up @@ -32,6 +32,25 @@ final class DataRate private(val value: Double, val unit: DataRateUnit)
def toExbibytesPerSecond = to(ExbibytesPerSecond)
def toZebibytesPerSecond = to(ZebibytesPerSecond)
def toYobibytesPerSecond = to(YobibytesPerSecond)

def toBitsPerSecond = to(BitsPerSecond)
def toKilobitsPerSecond = to(KilobitsPerSecond)
def toMegabitsPerSecond = to(MegabitsPerSecond)
def toGigabitsPerSecond = to(GigabitsPerSecond)
def toTerabitsPerSecond = to(TerabitsPerSecond)
def toPetabitsPerSecond = to(PetabitsPerSecond)
def toExabitsPerSecond = to(ExabitsPerSecond)
def toZettabitsPerSecond = to(ZettabitsPerSecond)
def toYottabitsPerSecond = to(YottabitsPerSecond)

def toKibibitsPerSecond = to(KibibitsPerSecond)
def toMebibitsPerSecond = to(MebibitsPerSecond)
def toGibibitsPerSecond = to(GibibitsPerSecond)
def toTebibitsPerSecond = to(TebibitsPerSecond)
def toPebibitsPerSecond = to(PebibitsPerSecond)
def toExbibitsPerSecond = to(ExbibitsPerSecond)
def toZebibitsPerSecond = to(ZebibitsPerSecond)
def toYobibitsPerSecond = to(YobibitsPerSecond)
}


Expand All @@ -47,7 +66,11 @@ object DataRate extends Dimension[DataRate] {
def units = Set(BytesPerSecond, KilobytesPerSecond, KibibytesPerSecond, MegabytesPerSecond, MebibytesPerSecond,
GigabytesPerSecond, GibibytesPerSecond, TerabytesPerSecond, TebibytesPerSecond,
PetabytesPerSecond, PebibytesPerSecond, ExabytesPerSecond, ExbibytesPerSecond,
ZettabytesPerSecond, ZebibytesPerSecond, YottabytesPerSecond, YobibytesPerSecond)
ZettabytesPerSecond, ZebibytesPerSecond, YottabytesPerSecond, YobibytesPerSecond,
BitsPerSecond, KilobitsPerSecond, KibibitsPerSecond, MegabitsPerSecond, MebibitsPerSecond,
GigabitsPerSecond, GibibitsPerSecond, TerabitsPerSecond, TebibitsPerSecond,
PetabitsPerSecond, PebibitsPerSecond, ExabitsPerSecond, ExbibitsPerSecond,
ZettabitsPerSecond, ZebibitsPerSecond, YottabitsPerSecond, YobibitsPerSecond)
}

trait DataRateUnit extends UnitOfMeasure[DataRate] with UnitConverter {
Expand Down Expand Up @@ -138,6 +161,92 @@ object YobibytesPerSecond extends DataRateUnit {
val conversionFactor = Yobibytes.conversionFactor
}

object BitsPerSecond extends DataRateUnit {
val symbol = "bps"
val conversionFactor = Bits.conversionFactor
}

object KilobitsPerSecond extends DataRateUnit {
val symbol = "Kbps"
val conversionFactor = BitsPerSecond.conversionFactor * Kilobytes.conversionFactor
}

object KibibitsPerSecond extends DataRateUnit {
val symbol = "Kibps"
val conversionFactor = BitsPerSecond.conversionFactor * Kibibytes.conversionFactor
}

object MegabitsPerSecond extends DataRateUnit {
val symbol = "Mbps"
val conversionFactor = BitsPerSecond.conversionFactor * Megabytes.conversionFactor
}

object MebibitsPerSecond extends DataRateUnit {
val symbol = "Mibps"
val conversionFactor = BitsPerSecond.conversionFactor * Mebibytes.conversionFactor
}

object GigabitsPerSecond extends DataRateUnit {
val symbol = "Gbps"
val conversionFactor = BitsPerSecond.conversionFactor * Gigabytes.conversionFactor
}

object GibibitsPerSecond extends DataRateUnit {
val symbol = "Gibps"
val conversionFactor = BitsPerSecond.conversionFactor * Gibibytes.conversionFactor
}

object TerabitsPerSecond extends DataRateUnit {
val symbol = "Tbps"
val conversionFactor = BitsPerSecond.conversionFactor * Terabytes.conversionFactor
}

object TebibitsPerSecond extends DataRateUnit {
val symbol = "Tibps"
val conversionFactor = BitsPerSecond.conversionFactor * Tebibytes.conversionFactor
}

object PetabitsPerSecond extends DataRateUnit {
val symbol = "Pbps"
val conversionFactor = BitsPerSecond.conversionFactor * Petabytes.conversionFactor
}

object PebibitsPerSecond extends DataRateUnit {
val symbol = "Pibps"
val conversionFactor = BitsPerSecond.conversionFactor * Pebibytes.conversionFactor
}

object ExabitsPerSecond extends DataRateUnit {
val symbol = "Ebps"
val conversionFactor = BitsPerSecond.conversionFactor * Exabytes.conversionFactor
}

object ExbibitsPerSecond extends DataRateUnit {
val symbol = "Eibps"
val conversionFactor = BitsPerSecond.conversionFactor * Exbibytes.conversionFactor
}

object ZettabitsPerSecond extends DataRateUnit {
val symbol = "Zbps"
val conversionFactor = BitsPerSecond.conversionFactor * Zettabytes.conversionFactor
}

object ZebibitsPerSecond extends DataRateUnit {
val symbol = "Zibps"
val conversionFactor = BitsPerSecond.conversionFactor * Zebibytes.conversionFactor
}

object YottabitsPerSecond extends DataRateUnit {
val symbol = "Ybps"
val conversionFactor = BitsPerSecond.conversionFactor * Yottabytes.conversionFactor
}

object YobibitsPerSecond extends DataRateUnit {
val symbol = "Yibps"
val conversionFactor = BitsPerSecond.conversionFactor * Yobibytes.conversionFactor
}


object DataRateConversions {
lazy val bytesPerSecond = BytesPerSecond(1)
lazy val kilobytesPerSecond = KilobytesPerSecond(1)
Expand All @@ -157,6 +266,24 @@ object DataRateConversions {
lazy val yottabytesPerSecond = YottabytesPerSecond(1)
lazy val yobibytesPerSecond = YobibytesPerSecond(1)

lazy val bitsPerSecond = BitsPerSecond(1)
lazy val kilobitsPerSecond = KilobitsPerSecond(1)
lazy val kibibitsPerSecond = KibibitsPerSecond(1)
lazy val megabitsPerSecond = MegabitsPerSecond(1)
lazy val mebibitsPerSecond = MebibitsPerSecond(1)
lazy val gigabitsPerSecond = GigabitsPerSecond(1)
lazy val gibibitsPerSecond = GibibitsPerSecond(1)
lazy val terabitsPerSecond = TerabitsPerSecond(1)
lazy val tebibitsPerSecond = TebibitsPerSecond(1)
lazy val petabitsPerSecond = PetabitsPerSecond(1)
lazy val pebibitsPerSecond = PebibitsPerSecond(1)
lazy val exabitsPerSecond = ExabitsPerSecond(1)
lazy val exbibitsPerSecond = ExbibitsPerSecond(1)
lazy val zettabitsPerSecond = ZettabitsPerSecond(1)
lazy val zebibitsPerSecond = ZebibitsPerSecond(1)
lazy val yottabitsPerSecond = YottabitsPerSecond(1)
lazy val yobibitsPerSecond = YobibitsPerSecond(1)

implicit class DataRateConversions[A](n: A)(implicit num: Numeric[A]) {
def bytesPerSecond = BytesPerSecond(n)
def kilobytesPerSecond = KilobytesPerSecond(n)
Expand All @@ -175,6 +302,24 @@ object DataRateConversions {
def zebibytesPerSecond = ZebibytesPerSecond(n)
def yottabytesPerSecond = YottabytesPerSecond(n)
def yobibytesPerSecond = YobibytesPerSecond(n)

def bitsPerSecond = BitsPerSecond(n)
def kilobitsPerSecond = KilobitsPerSecond(n)
def kibibitsPerSecond = KibibitsPerSecond(n)
def megabitsPerSecond = MegabitsPerSecond(n)
def mebibitsPerSecond = MebibitsPerSecond(n)
def gigabitsPerSecond = GigabitsPerSecond(n)
def gibibitsPerSecond = GibibitsPerSecond(n)
def terabitsPerSecond = TerabitsPerSecond(n)
def tebibitsPerSecond = TebibitsPerSecond(n)
def petabitsPerSecond = PetabitsPerSecond(n)
def pebibitsPerSecond = PebibitsPerSecond(n)
def exabitsPerSecond = ExabitsPerSecond(n)
def exbibitsPerSecond = ExbibitsPerSecond(n)
def zettabitsPerSecond = ZettabitsPerSecond(n)
def zebibitsPerSecond = ZebibitsPerSecond(n)
def yottabitsPerSecond = YottabitsPerSecond(n)
def yobibitsPerSecond = YobibitsPerSecond(n)
}

implicit object DataRateNumeric extends AbstractQuantityNumeric[DataRate](DataRate.primaryUnit)
Expand Down
145 changes: 144 additions & 1 deletion shared/src/main/scala/squants/information/Information.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ final class Information private(val value: Double, val unit: InformationUnit)
def toZebibytes = to(Zebibytes)
def toYottabytes = to(Yottabytes)
def toYobibytes = to(Yobibytes)

def toBits = to(Bits)
def toKilobits = to(Kilobits)
def toKibibits = to(Kibibits)
def toMegabits = to(Megabits)
def toMebibits = to(Mebibits)
def toGigabits = to(Gigabits)
def toGibibits = to(Gibibits)
def toTerabits = to(Terabits)
def toTebibits = to(Tebibits)
def toPetabits = to(Petabits)
def toPebibits = to(Pebibits)
def toExabits = to(Exabits)
def toExbibits = to(Exbibits)
def toZettabits = to(Zettabits)
def toZebibits = to(Zebibits)
def toYottabits = to(Yottabits)
def toYobibits = to(Yobibits)
}

trait InformationUnit extends UnitOfMeasure[Information] with UnitConverter {
Expand All @@ -61,7 +79,10 @@ object Information extends Dimension[Information] with BaseDimension {
def siUnit = Bytes
def units = Set(Bytes, Kilobytes, Kibibytes, Megabytes, Mebibytes,
Gigabytes, Gibibytes, Terabytes, Tebibytes, Petabytes, Pebibytes,
Exabytes, Exbibytes, Zettabytes, Zebibytes, Yottabytes, Yobibytes)
Exabytes, Exbibytes, Zettabytes, Zebibytes, Yottabytes, Yobibytes,
Bits, Kilobits, Kibibits, Megabits, Mebibits, Gigabits, Gibibits,
Terabits, Tebibits, Petabits, Pebibits, Exabits, Exbibits,
Zettabits, Zebibits, Yottabits, Yobibits)
def dimensionSymbol = "B"
}

Expand Down Expand Up @@ -154,6 +175,91 @@ object Yobibytes extends InformationUnit {
def symbol = "YiB"
}

object Bits extends InformationUnit {
def conversionFactor = 0.125d
val symbol = "bit"
}

object Kilobits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * MetricSystem.Kilo
val symbol = "Kbit"
}

object Kibibits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * BinarySystem.Kilo
val symbol = "Kibit"
}

object Megabits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * MetricSystem.Mega
val symbol = "Mbit"
}

object Mebibits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * BinarySystem.Mega
val symbol = "Mibit"
}

object Gigabits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * MetricSystem.Giga
val symbol = "Gbit"
}

object Gibibits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * BinarySystem.Giga
val symbol = "Gibit"
}

object Terabits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * MetricSystem.Tera
val symbol = "Tbit"
}

object Tebibits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * BinarySystem.Tera
val symbol = "Tibit"
}

object Petabits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * MetricSystem.Peta
val symbol = "Pbit"
}

object Pebibits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * BinarySystem.Peta
val symbol = "Pibit"
}

object Exabits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * MetricSystem.Exa
val symbol = "Ebit"
}

object Exbibits extends InformationUnit {
val conversionFactor = Bits.conversionFactor * BinarySystem.Exa
val symbol = "Eibit"
}

object Zettabits extends InformationUnit {
def conversionFactor = Bits.conversionFactor * MetricSystem.Zetta
def symbol = "Zbit"
}

object Zebibits extends InformationUnit {
def conversionFactor = Bits.conversionFactor * BinarySystem.Zetta
def symbol = "Zibit"
}

object Yottabits extends InformationUnit {
def conversionFactor = Bits.conversionFactor * MetricSystem.Yotta
def symbol = "Ybit"
}

object Yobibits extends InformationUnit {
def conversionFactor = Bits.conversionFactor * BinarySystem.Yotta
def symbol = "Yibit"
}

object InformationConversions {
lazy val byte = Bytes(1)
lazy val kilobyte = Kilobytes(1)
Expand All @@ -173,6 +279,24 @@ object InformationConversions {
lazy val yottabyte = Yottabytes(1)
lazy val yobibyte = Yobibytes(1)

lazy val bit = Bits(1)
lazy val kilobit = Kilobits(1)
lazy val kibibit = Kibibits(1)
lazy val megabit = Megabits(1)
lazy val mebibit = Mebibits(1)
lazy val gigabit = Gigabits(1)
lazy val gibibit = Gibibits(1)
lazy val terabit = Terabits(1)
lazy val tebibit = Tebibits(1)
lazy val petabit = Petabits(1)
lazy val pebibit = Pebibits(1)
lazy val exabit = Exabits(1)
lazy val exbibit = Exbibits(1)
lazy val zettabit = Zettabits(1)
lazy val zebibit = Zebibits(1)
lazy val yottabit = Yottabits(1)
lazy val yobibit = Yobibits(1)

implicit class InformationConversions[A](n: A)(implicit num: Numeric[A]) {
def bytes = Bytes(n)
def kb = Kilobytes(n)
Expand Down Expand Up @@ -208,6 +332,25 @@ object InformationConversions {
def zebibytes = Zebibytes(n)
def yib = Yobibytes(n)
def yobibytes = Yobibytes(n)

def bits = Bits(n)
def kilobits = Kilobits(n)
def megabits = Megabits(n)
def gigabits = Gigabits(n)
def terabits = Terabits(n)
def petabits = Petabits(n)
def exabits = Exabits(n)
def zettabits = Zettabits(n)
def yottabits = Yottabits(n)

def kibibits = Kibibits(n)
def mebibits = Mebibits(n)
def gibibits = Gibibits(n)
def tebibits = Tebibits(n)
def pebibits = Pebibits(n)
def exbibits = Exbibits(n)
def zebibits = Zebibits(n)
def yobibits = Yobibits(n)
}

implicit class InformationStringConversions(s: String) {
Expand Down
Loading

0 comments on commit 4624503

Please sign in to comment.