Code Monkey home page Code Monkey logo

uom's Introduction

Units of Measurement

Build Status Coverage Status Sonatype Nexus (Snapshots) License

My personal take on a units of measurement library for java.

The design goal of the library is to include:

  • fully typed quantities:
    Length l1 = Length.of(1, SI.METRE);
    Length l2 = Length.ofMeter(2);          // convenience factory method for SI unit 
  • transparent support for double and arbitrary decimal precision quantities (using BigDecimal):
    Length l1 = Quantities.create(1, SI.METRE);
    Length l2 = Quantities.create(BigDecimal.ONE, Intl.YARD);
    
    Length l3 = l1.add(l2);   // quantities with different precision can be used together
  • support for generic quantities:
      Quantity<Speed> speed = Quantities.createGeneric(1, SI.METER_PER_SECOND);
    
      System.out.println(speed.add(Speed.ofMeterPerSecond(2))); // -> prints 3 m/s
  • support for quantity factories:
      QuantityFactory<Length> factory = DoubleQuantity.factory(Length.class);
    
      Length l1 = factory.create(1, SI.METRE);
    
      // default factories can be replaced
      // use decimal precision with MathContext DECIMAL128 for every quantity of type Length:
      Quantities.registerQuantityFactory(Length.class, DecimalQuantity.factory(MathContext.DECIMAL128, Length.class));
    
      // quantity factories with pooling behavior can be registered
      QuantityFactory<Length> myPoolFactory = ...;
      Quantities.registerQuantityFactory(Length.class, myPoolFactory);
  • support for the standard unit manipulations as defined by JSR-385 et al
  • support for as many units as possible, the amazing GNU units library is the reference to compare to

The library uses dynamic proxies to create concrete quantity classes for the specified precision at runtime.

Examples

Conversion of units between different system of units, e.g. SI and CGS

ElectricCharge e1 = ElectricCharge.of(1, SI.COULOMB);
ElectricCharge e2 = e1.to(ESU.STATCOULOMB);

System.out.println(e2);

prints

2.997925e+09 statC

Custom quantities and units:

    public interface Bmi extends Quantity<Bmi> {}

    ...

    final Unit<Bmi> bmiUnit = SI.KILOGRAM.divide(SI.METRE.pow(2)).withSymbol("B").forQuantity(Bmi.class);
    Quantity<Bmi> bmiDouble  = Quantities.createGeneric(19, bmiUnit);
    Quantity<Bmi> bmiDecimal = Quantities.createGeneric(BigDecimal.valueOf(21), bmiUnit);

    System.out.println(bmiDouble);

License

Code is under the Apache Licence v2.

uom's People

Contributors

netomi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

uom's Issues

Support non-physical dimensions

Currently the library assumes that only physical dimensions exist.

For general purpose use it would be good to also support non-physical dimensions.

A non-physical dimension keeps its nature regardless of how it is combined with itself:

Money times Money = Money

but such a dimension can be combined with another one:

Money divided by Time = Money / Time

Money divided by Time times Money = Money / Time

general rules

X^n x X^m = X^1 for any n and m
X x Y = XY
X x XY = XY
...

Formatter for Quantity and Unit instances

Similar to the javax.measure API or the joda libraries we should add a formatter for Quantity and Unit instances to be able to control how these instances are serialized to strings.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.