-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MolarEnergy, implement Energy unimplemented method
- Loading branch information
Showing
5 changed files
with
121 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package squants.energy | ||
|
||
import squants.mass.{ChemicalAmount, Moles} | ||
import squants.{AbstractQuantityNumeric, Dimension, PrimaryUnit, Quantity, SiUnit, UnitConverter, UnitOfMeasure} | ||
|
||
/** | ||
* | ||
* @author Nicolas Vinuesa | ||
* @since 1.4 | ||
* | ||
* @param value Double | ||
*/ | ||
final class MolarEnergy private (val value: Double, val unit: MolarEnergyUnit) | ||
extends Quantity[MolarEnergy] { | ||
|
||
def dimension = MolarEnergy | ||
|
||
def *(that: ChemicalAmount): Energy = Joules(this.toJoulesPerMol * that.toMoles) | ||
|
||
def toJoulesPerMol = to(JoulesPerMol) | ||
} | ||
|
||
object MolarEnergy extends Dimension[MolarEnergy] { | ||
private[energy] def apply[A](n: A, unit: MolarEnergyUnit)(implicit num: Numeric[A]) = new MolarEnergy(num.toDouble(n), unit) | ||
def apply = parse _ | ||
def name = "MolarEnergy" | ||
def primaryUnit = JoulesPerMol | ||
def siUnit = JoulesPerMol | ||
def units = Set(JoulesPerMol) | ||
} | ||
|
||
trait MolarEnergyUnit extends UnitOfMeasure[MolarEnergy] with UnitConverter { | ||
def apply[A](n: A)(implicit num: Numeric[A]) = MolarEnergy(n, this) | ||
} | ||
|
||
object JoulesPerMol extends MolarEnergyUnit with PrimaryUnit with SiUnit { | ||
val symbol = Joules.symbol + "/" + Moles.symbol | ||
} | ||
|
||
object MolarEnergyConversions { | ||
lazy val joulePerMol = JoulesPerMol(1) | ||
|
||
implicit class MolarEnergyConversions[A](n: A)(implicit num: Numeric[A]) { | ||
def joulesPerMol = JoulesPerMol(n) | ||
} | ||
|
||
implicit object MolarEnergyNumeric extends AbstractQuantityNumeric[MolarEnergy](MolarEnergy.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/energy/MolarEnergySpec.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.energy | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
import squants.QuantityParseException | ||
import squants.mass.Moles | ||
|
||
/** | ||
* @author Nicolas Vinuesa | ||
* @since 1.4 | ||
* | ||
*/ | ||
class MolarEnergySpec extends FlatSpec with Matchers { | ||
|
||
behavior of "MolarEnergy and its Units of Measure" | ||
|
||
it should "create values using UOM factories" in { | ||
JoulesPerMol(1).toJoulesPerMol should be(1) | ||
} | ||
|
||
it should "create values from properly formatted Strings" in { | ||
MolarEnergy("10.22 J/mol").get should be(JoulesPerMol(10.22)) | ||
MolarEnergy("10.22 zz").failed.get should be(QuantityParseException("Unable to parse MolarEnergy", "10.22 zz")) | ||
MolarEnergy("zz J/mol").failed.get should be(QuantityParseException("Unable to parse MolarEnergy", "zz J/mol")) | ||
} | ||
|
||
it should "properly convert to all supported Units of Measure" in { | ||
val x = JoulesPerMol(10) | ||
x.toJoulesPerMol should be(10.0) | ||
} | ||
|
||
it should "return properly formatted strings for all supported Units of Measure" in { | ||
JoulesPerMol(1).toString(JoulesPerMol) should be("1.0 J/mol") | ||
} | ||
|
||
it should "return Energy when multiplied by ChemicalAmount" in { | ||
JoulesPerMol(1) * Moles(1) should be(Joules(1)) | ||
} | ||
|
||
behavior of "MolarEnergyConversions" | ||
|
||
it should "provide aliases for single unit values" in { | ||
import MolarEnergyConversions._ | ||
|
||
joulePerMol should be(JoulesPerMol(1)) | ||
} | ||
|
||
it should "provide implicit conversion from Double" in { | ||
import MolarEnergyConversions._ | ||
|
||
val d = 10.22 | ||
d.joulesPerMol should be(JoulesPerMol(d)) | ||
} | ||
|
||
it should "provide Numeric support" in { | ||
import MolarEnergyConversions.MolarEnergyNumeric | ||
|
||
val rs = List(JoulesPerMol(100), JoulesPerMol(10)) | ||
rs.sum should be(JoulesPerMol(110)) | ||
} | ||
} |