This project contains several annotations that represent units. Those annotations can be used as documentation. But they might also be used for code analysis.
A method with those annotations might look like that:
@mm
public double pixelsToMm( @px int pixels, @dpi double resolution ) {
return pixels / resolution * 25.4;
}Every developer using this method will instantly know which unit that methods expects/returns.
The annotations can also be used for local variables:
@dpi int res = 72; assertEquals( 10, pixelsToMm( 28, res ), 0.2 );
This is especially useful when there are several units in use that might be mixed up quite easily (like radiants vs degree):
public void setAngle( @rad double angle ) {The usage of annotations is backwards compatible. You can annotate old methods and improve the documentation that way.
The annotations can be used with every object. No need to convert to a container object.
No magic conversion involved that might result in poor performance or loss o precision. No wrapper objects are necessary.
The annotations will end up within the javadocs. No need to explain the expected units anymore.
The usage of those annotation is very simple and straight forward. They help improving the documentation a lot. But they don't force anybody to use them. No need to ship the annotations jar (they will simply be ignored at runtime).
Creating your own annotations for new units is just a matter of seconds. Just mail to js@cedarsoft.com for addition to the lib.
The jar file is automatically deployed to Maven Central:
http://repo2.maven.org/maven2/com/cedarsoft/unit/
For usage in a Maven project, add that dependency:
<dependency> <groupId>com.cedarsoft</groupId> <artifactId>unit</artifactId> <version>1.0-beta1-SNAPSHOT</version> </dependency>