Jonasvdbo / measure

Work with units of measurement

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Measure

Work with units of measurement.

Motivation

Projects often need a way to express different quantities. For example, building software for electric grids one encounters quantities like power, energy, amperage etcetera.

When one choses to represent this quantities as BigDecimal, one needs to be very vigilant when calculating with these quantities. It is easy to aid power and energy when they both are represented as a BigDecimal, even though it does not make sense on physical grounds.

Rather, one would represents quantities as a multiple of a unit of measurement. This project provides exactly that.

Usage

In using measure library there are two parts. The generic Measure and Units provided by the measure library, and the specific units that your project are interested in.

val power: Measure<Power> = 10 * kiloWatt
val duration: Measure<Time> = 15 * minutes

val energy: Measure<Energy> = power * duration

assertThat(energy `as` megaJoule).equals(9 * megaJoule)

The code above demonstrates a fully annotated calculation with quantities. Often times the compiler can infer the corresponding types.

Development

This project is written in Kotlin and uses Gradle as a build tool. To see which tasks are available run

./gradlew tasks

About

Work with units of measurement

License:Mozilla Public License 2.0


Languages

Language:Kotlin 100.0%