Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ElectricFieldStrength, implement ElectricPotential unimplemented method #272

Merged
merged 5 commits into from
Jun 25, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions shared/src/main/scala/squants/electro/ElectricFieldStrength.scala
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some of these could be val as we've discussed in other PRs

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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}