-
Notifications
You must be signed in to change notification settings - Fork 122
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6122ff4
add ElectricFieldStrength, implement ElectricPotential unimplemented …
nvinuesa 6984d0a
Merge branch 'master' into issue-252-4
ff95d15
Merge branch 'master' into issue-252-4
30b2c63
Merge branch 'master' into issue-252-4
derekmorr e33b102
Merge branch 'master' into issue-252-4
derekmorr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
shared/src/main/scala/squants/electro/ElectricFieldStrength.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
shared/src/test/scala/squants/electro/ElectricFieldStrengthSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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