From 6122ff471c83f43fcff8d2c8157cccd0b08c09be Mon Sep 17 00:00:00 2001 From: underscorenico Date: Tue, 20 Jun 2017 21:57:31 +0200 Subject: [PATCH] add ElectricFieldStrength, implement ElectricPotential unimplemented method --- .../electro/ElectricFieldStrength.scala | 48 +++++++++++++++ .../squants/electro/ElectricPotential.scala | 2 +- .../unitgroups/ImplicitDimensions.scala | 1 + .../electro/ElectricFieldStrengthSpec.scala | 60 +++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 shared/src/main/scala/squants/electro/ElectricFieldStrength.scala create mode 100644 shared/src/test/scala/squants/electro/ElectricFieldStrengthSpec.scala diff --git a/shared/src/main/scala/squants/electro/ElectricFieldStrength.scala b/shared/src/main/scala/squants/electro/ElectricFieldStrength.scala new file mode 100644 index 00000000..3f6df402 --- /dev/null +++ b/shared/src/main/scala/squants/electro/ElectricFieldStrength.scala @@ -0,0 +1,48 @@ +package squants.electro + +import squants.space.{Length, Meters} +import squants.{AbstractQuantityNumeric, Dimension, PrimaryUnit, Quantity, SiUnit, UnitConverter, UnitOfMeasure} + +/** + * + * @author Nicolas Vinuesa + * @since 1.4 + * + * @param value Double + */ +final class ElectricFieldStrength private (val value: Double, val unit: ElectricFieldStrengthUnit) + extends Quantity[ElectricFieldStrength] { + + def dimension = ElectricFieldStrength + + def *(that: Length): ElectricPotential = Volts(this.toVoltsPerMeter * that.toMeters) + + def toVoltsPerMeter = to(VoltsPerMeter) +} + +object ElectricFieldStrength extends Dimension[ElectricFieldStrength] { + private[electro] def apply[A](n: A, unit: ElectricFieldStrengthUnit)(implicit num: Numeric[A]) = new ElectricFieldStrength(num.toDouble(n), unit) + def apply = parse _ + def name = "ElectricFieldStrength" + def primaryUnit = VoltsPerMeter + def siUnit = VoltsPerMeter + def units = Set(VoltsPerMeter) +} + +trait ElectricFieldStrengthUnit extends UnitOfMeasure[ElectricFieldStrength] with UnitConverter { + def apply[A](n: A)(implicit num: Numeric[A]) = ElectricFieldStrength(n, this) +} + +object VoltsPerMeter extends ElectricFieldStrengthUnit with PrimaryUnit with SiUnit { + val symbol = Volts.symbol + "/" + Meters.symbol +} + +object ElectricFieldStrengthConversions { + lazy val voltPerMeter = VoltsPerMeter(1) + + implicit class ElectricFieldStrengthConversions[A](n: A)(implicit num: Numeric[A]) { + def voltsPerMeter = VoltsPerMeter(n) + } + + implicit object ElectricFieldStrengthNumeric extends AbstractQuantityNumeric[ElectricFieldStrength](ElectricFieldStrength.primaryUnit) +} diff --git a/shared/src/main/scala/squants/electro/ElectricPotential.scala b/shared/src/main/scala/squants/electro/ElectricPotential.scala index 7f3d28ab..00e8cc7b 100644 --- a/shared/src/main/scala/squants/electro/ElectricPotential.scala +++ b/shared/src/main/scala/squants/electro/ElectricPotential.scala @@ -33,7 +33,7 @@ final class ElectricPotential private (val value: Double, val unit: ElectricPote def /(that: ElectricCurrent): ElectricalResistance = Ohms(this.toVolts / that.toAmperes) def /(that: ElectricalResistance): ElectricCurrent = Amperes(this.toVolts / that.toOhms) - def /(that: Length) = ??? // returns ElectricFieldStrength + def /(that: Length): ElectricFieldStrength = VoltsPerMeter(this.toVolts / that.toMeters) def toVolts = to(Volts) def toMicrovolts = to(Microvolts) diff --git a/shared/src/main/scala/squants/experimental/unitgroups/ImplicitDimensions.scala b/shared/src/main/scala/squants/experimental/unitgroups/ImplicitDimensions.scala index 286a7e70..f5aeb39f 100644 --- a/shared/src/main/scala/squants/experimental/unitgroups/ImplicitDimensions.scala +++ b/shared/src/main/scala/squants/experimental/unitgroups/ImplicitDimensions.scala @@ -24,6 +24,7 @@ object ImplicitDimensions { implicit val implicitElectricalConductance: Dimension[ElectricalConductance] = ElectricalConductance implicit val implicitElectricalResistance: Dimension[ElectricalResistance] = ElectricalResistance implicit val implicitElectricCharge: Dimension[ElectricCharge] = ElectricCharge + implicit val implicitElectricFieldStrength: Dimension[ElectricFieldStrength] = ElectricFieldStrength implicit val implicitElectricPotential: Dimension[ElectricPotential] = ElectricPotential implicit val implicitInductance: Dimension[Inductance] = Inductance implicit val implicitMagneticFlux: Dimension[MagneticFlux] = MagneticFlux diff --git a/shared/src/test/scala/squants/electro/ElectricFieldStrengthSpec.scala b/shared/src/test/scala/squants/electro/ElectricFieldStrengthSpec.scala new file mode 100644 index 00000000..61f20bac --- /dev/null +++ b/shared/src/test/scala/squants/electro/ElectricFieldStrengthSpec.scala @@ -0,0 +1,60 @@ +package squants.electro + +import org.scalatest.{FlatSpec, Matchers} +import squants.QuantityParseException +import squants.space.Meters + +/** + * @author Nicolas Vinuesa + * @since 1.4 + * + */ +class ElectricFieldStrengthSpec extends FlatSpec with Matchers { + + behavior of "ElectricFieldStrength and its Units of Measure" + + it should "create values using UOM factories" in { + VoltsPerMeter(1).toVoltsPerMeter should be(1) + } + + it should "create values from properly formatted Strings" in { + ElectricFieldStrength("10.22 V/m").get should be(VoltsPerMeter(10.22)) + ElectricFieldStrength("10.22 zz").failed.get should be(QuantityParseException("Unable to parse ElectricFieldStrength", "10.22 zz")) + ElectricFieldStrength("zz V/m").failed.get should be(QuantityParseException("Unable to parse ElectricFieldStrength", "zz V/m")) + } + + it should "properly convert to all supported Units of Measure" in { + val x = VoltsPerMeter(10) + x.toVoltsPerMeter should be(10.0) + } + + it should "return properly formatted strings for all supported Units of Measure" in { + VoltsPerMeter(1).toString(VoltsPerMeter) should be("1.0 V/m") + } + + it should "return Electric Potential when multiplied by Length" in { + VoltsPerMeter(1) * Meters(1) should be(Volts(1)) + } + + behavior of "ElectricFieldStrengthConversions" + + it should "provide aliases for single unit values" in { + import ElectricFieldStrengthConversions._ + + voltPerMeter should be(VoltsPerMeter(1)) + } + + it should "provide implicit conversion from Double" in { + import ElectricFieldStrengthConversions._ + + val d = 10.22 + d.voltsPerMeter should be(VoltsPerMeter(d)) + } + + it should "provide Numeric support" in { + import ElectricFieldStrengthConversions.ElectricFieldStrengthNumeric + + val rs = List(VoltsPerMeter(100), VoltsPerMeter(10)) + rs.sum should be(VoltsPerMeter(110)) + } +}