squants
package squants
Squants
The Scala API for Quantities, Units of Measure and Dimensional Analysis
Overview
Squants is a framework of data types and a domain specific language (DSL) for representing Quantities, their Units of Measure, and their Dimensional relationships. The API supports typesafe dimensional analysis, improved domain models and more. All types are immutable and thread-safe.
Typedefs and implicits for common usages
- Source
- package.scala
- Version
0.1
- Since
0.1
- Alphabetic
- By Inheritance
- squants
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Package Members
- package electro
- Since
0.1
- package energy
- Since
0.1
- package experimental
- package information
- package market
Squants Market API
Squants Market API
Market Types are similar but not quite the same as other quantities in the library.
The primary type, squants.market.Money, is derived from Quantity, and its Units of Measure are Currencies. However, because the conversion multipliers between units can not be predefined, many of the behaviors have been overridden and augmented to realize correct behavior.
squants.market.Prices represent a Ratio between Money and some other Quantity. Prices can be created from ratios of Money and a Quantity
val money: Money = USD(10) val length: Length = Meters(1) val price: Price[Length] = money / length
Multiplying a Price * Quantity will yield Money amount that represents the cost of the quantity
val cost: Money = price * Meters(3.8)
and multiplying Price * Money will yield the corresponding Quantity amount
val budget: Money = USD(250) val quote: Length = price * budget
squants.market.CurrencyExchangeRates represent conversion rates between currencies. Use them to explicitly convert Money values in one currency to values in another.
squants.market.MoneyContext provide the implicit context necessary to perform cross-currency operations on Money values with conversions automatically applied.
Some binary math operations will work on Moneys of like Currency with no MoneyContext in scope. Attempts to perform these operations on Moneys of dissimilar currencies will throw an exception at runtime.
Other operations, including direct conversions to other currencies, require a MoneyContext and will not compile without it. However, there is no compile time check to determine if the correct exchange rates will be available at runtime. Operation requiring conversion without the required rates available will throw a NoSuchExchangeRateException at runtime.
The defaultMoneyContext uses the USD as the default and provides a list of ~20 common currencies, and NO exchange rates. If your application requires something different you should initialize your own implicit MoneyContext
- Since
0.1
- package mass
- Since
0.1
- package motion
- Since
0.1
- package photo
- Since
0.1
- package radio
- Since
0.1
- package space
- Since
0.1
- package thermal
- Since
0.1
- package time
- Since
0.1
Type Members
- final class A extends Quantity2[A] with TimeDerivative2[V] with TimeIntegral2[J] with SecondTimeDerivative2[L]
- abstract class AbstractQuantityNumeric[A <: Quantity[A]] extends Numeric[A]
Base class for creating objects to manage quantities as Numeric.
Base class for creating objects to manage quantities as Numeric.
One limitation is the
times
operation which is not supported by every quantity type- A
Quantity type
- type Acceleration = squants.motion.Acceleration
- type Angle = squants.space.Angle
- type Area = squants.space.Area
- trait BaseDimension extends AnyRef
SI Base Quantity
- type ChemicalAmount = squants.mass.ChemicalAmount
- type Density = squants.mass.Density
- trait Dimension[A <: Quantity[A]] extends AnyRef
Represents a Dimension or Quantity Type
Represents a Dimension or Quantity Type
This trait should be mixed into the Companion Objects of specific Quantity Types.
- A
Quantity Type
- final class Dimensionless extends Quantity[Dimensionless] with TimeIntegral[Frequency]
Represents a quantity of some thing for which there is no dimension.
Represents a quantity of some thing for which there is no dimension.
This may be used to represent counts or other discrete amounts of everyday life, but may also represent ratios between like quantities where the units have cancelled out.
- Since
0.1
- trait DimensionlessUnit extends UnitOfMeasure[Dimensionless] with UnitConverter
Base trait for units of squants.Dimensionless
Base trait for units of squants.Dimensionless
The DimensionlessUnit is a useful paradox
- case class DoubleVector(coordinates: Double*) extends SVector[Double] with Product with Serializable
Double (Real Number) Vector
Double (Real Number) Vector
- coordinates
Double*
- Since
0.3.0
- type ElectricCurrent = squants.electro.ElectricCurrent
- type Energy = squants.energy.Energy
- type Force = squants.motion.Force
- final class J extends Quantity2[J] with TimeDerivative2[A] with SecondTimeDerivative2[V]
- type Jerk = squants.motion.Jerk
- final class L extends Quantity2[L] with TimeIntegral2[V] with SecondTimeIntegral2[A]
- type Length = squants.space.Length
- trait LikeRatio[A <: Quantity[A]] extends Ratio[A, A]
- type LuminousIntensity = squants.photo.LuminousIntensity
- type Mass = squants.mass.Mass
- type MassFlow = squants.motion.MassFlow
- type Momentum = squants.motion.Momentum
- type Money = squants.market.Money
- type Power = squants.energy.Power
- type PowerRamp = squants.energy.PowerRamp
- type Price[A <: Quantity[A]] = squants.market.Price[A]
- trait PrimaryUnit extends UnitConverter
Identifies the Unit of Measure with a conversionFactor of 1.0.
Identifies the Unit of Measure with a conversionFactor of 1.0.
It is used as the intermediary unit during conversions
Each Quantity should have one and only one ValueUnit
- abstract class Quantity[A <: Quantity[A]] extends Serializable with Ordered[A]
A base class for measurable quantities, instances of which contain a value and a unit
A base class for measurable quantities, instances of which contain a value and a unit
- Since
0.1
- abstract class Quantity2[A <: Quantity2[A]] extends Serializable
- case class QuantityParseException(message: String, expression: String) extends Exception with Product with Serializable
- case class QuantityRange[A <: Quantity[A]](lower: A, upper: A) extends Product with Serializable
Represents a Range starting at one Quantity value and going up to another
Represents a Range starting at one Quantity value and going up to another
- A
the Quantity Type
- lower
Quantity representing the lower bound of the range
- upper
Quantity representing the upper bound of the range
- Since
0.1
- type QuantitySeries[A <: Quantity[A]] = IndexedSeq[QuantityRange[A]]
- case class QuantityVector[A <: Quantity[A]](coordinates: A*) extends SVector[A] with Product with Serializable
Quantity Vector
Quantity Vector
- A
QuantityType
- coordinates
Variable list of A
- Since
0.3.0
- trait Ratio[A <: Quantity[A], B <: Quantity[B]] extends AnyRef
Defines an interface and partial implementation for types that represent a ratio between any two quantities
Defines an interface and partial implementation for types that represent a ratio between any two quantities
- A
Quantity A
- B
Quantity B
- Since
0.1
- trait SVector[A] extends AnyRef
Root trait for representing Vectors
Root trait for representing Vectors
- A
Type for the Vector's coordinate values
- Since
0.3.0
- trait SecondTimeDerivative2[A <: SecondTimeIntegral2[_]] extends AnyRef
- trait SecondTimeIntegral2[A <: SecondTimeDerivative2[_]] extends AnyRef
- trait SiBaseUnit extends SiUnit
A marker trait identifying SI Base Units
- trait SiUnit extends AnyRef
A marker trait identifying SI Units
- type SolidAngle = squants.space.SolidAngle
- implicit class SquantifiedBigDecimal extends AnyRef
Provides implicit conversions that allow BigDecimals to lead in * and / by Time operations
Provides implicit conversions that allow BigDecimals to lead in * and / by Time operations
BigDecimal(1.5) * Kilometers(10) should be(Kilometers(15))
- implicit class SquantifiedDouble extends AnyRef
Provides implicit conversions that allow Doubles to lead in * and / by Time operations
Provides implicit conversions that allow Doubles to lead in * and / by Time operations
1.5 * Kilometers(10) should be(Kilometers(15))
- implicit class SquantifiedInt extends AnyRef
Provides implicit conversions that allow Int to lead in * and / by Time operations
Provides implicit conversions that allow Int to lead in * and / by Time operations
5 * Kilometers(10) should be(Kilometers(15))
- implicit class SquantifiedLong extends AnyRef
Provides implicit conversions that allow Longs to lead in * and / by Time operations
Provides implicit conversions that allow Longs to lead in * and / by Time operations
5 * Kilometers(10) should be(Kilometers(15))
- type Temperature = squants.thermal.Temperature
- type Time = squants.time.Time
- trait TimeDerivative2[A <: Quantity2[A] with TimeIntegral2[_]] extends AnyRef
- trait TimeIntegral2[A <: Quantity2[A] with TimeDerivative2[_]] extends AnyRef
- trait UnitConverter extends AnyRef
A Unit of Measure that require a simple multiplier for converting to and from the underlying value's unit
- trait UnitOfMeasure[A <: Quantity[A]] extends Serializable
A Unit of Measure is used to define the scale of a quantity measurement
A Unit of Measure is used to define the scale of a quantity measurement
Each Quantity Dimension must include at least one Unit of Measure, and one and only one Primary. Other units of measure are defined with conversionFactors relative to the Primary.
- A
The type of Quantity being measured
- Since
0.1
- final class V extends Quantity2[V] with TimeIntegral2[A] with SecondTimeIntegral2[J] with TimeDerivative2[L]
- type Velocity = squants.motion.Velocity
- type Volume = squants.space.Volume
- type VolumeFlow = squants.motion.VolumeFlow
Value Members
- val Amperes: squants.electro.Amperes.type
- val Candelas: squants.photo.Candelas.type
- val Kelvin: squants.thermal.Kelvin.type
- val Kilograms: squants.mass.Kilograms.type
- val Meters: squants.space.Meters.type
- val Moles: squants.mass.Moles.type
- val Radians: squants.space.Radians.type
- val Seconds: squants.time.Seconds.type
- val SquareRadians: SquaredRadians.type
- object BinarySystem
Singleton defining Metric System multipliers
Singleton defining Metric System multipliers
- Since
0.1
- object Dimensionless extends Dimension[Dimensionless] with Serializable
Factory singleton for squants.Dimensionless
- object DimensionlessConversions
- object Dozen extends DimensionlessUnit
Represents a unit of dozen (12)
- object Each extends DimensionlessUnit with PrimaryUnit with SiUnit
Represents a unit of singles
- object Gross extends DimensionlessUnit
Represents a unit of gross (144)
- object MetricSystem
Singleton defining Metric System multipliers
Singleton defining Metric System multipliers
- Since
0.1
- object Percent extends DimensionlessUnit
Represents a number of hundredths (0.01)
- object Platform
- object SVector
- object Score extends DimensionlessUnit
Represents a unit of scores (20)