{"id":16167500,"url":"https://github.com/samcarlberg/units","last_synced_at":"2025-04-07T05:51:41.726Z","repository":{"id":82586649,"uuid":"81153558","full_name":"SamCarlberg/units","owner":"SamCarlberg","description":"A simple Java unit library","archived":false,"fork":false,"pushed_at":"2023-05-27T04:54:06.000Z","size":201,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-13T10:18:25.770Z","etag":null,"topics":["java","units","units-of-measure"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SamCarlberg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-07T01:47:22.000Z","updated_at":"2017-07-05T22:38:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"5359f1b0-0519-40fd-a49a-e3a003e725bb","html_url":"https://github.com/SamCarlberg/units","commit_stats":{"total_commits":20,"total_committers":1,"mean_commits":20.0,"dds":0.0,"last_synced_commit":"e97ee221de570318d787595f58893e0fd1b19599"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamCarlberg%2Funits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamCarlberg%2Funits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamCarlberg%2Funits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamCarlberg%2Funits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SamCarlberg","download_url":"https://codeload.github.com/SamCarlberg/units/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601485,"owners_count":20964864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","units","units-of-measure"],"created_at":"2024-10-10T03:08:14.415Z","updated_at":"2025-04-07T05:51:41.708Z","avatar_url":"https://github.com/SamCarlberg.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Units\n\nA simple Java unit library to provide compile-time safety for units. No more of this:\n\n```java\ndouble distance = 12.34; // What unit is this?\n```\n\nNow you can have this:\n\n```java\nMeasure\u003cDistance\u003e distance = Units.Inches.of(12.34); // It has to be inches!\n```\n\nAnd since `Inches` is a built-in unit, we can use a special subclass of `Measure\u003cDistance\u003e`\n\n```java\nInches distance = Inches.of(12.34);\n```\n\nNote that this `Inches` class is _not_ the same thing as `Units.Inches`\n\nOf course, units can be converted from one type to another, as long as they measure the same kind of thing!\n\n```java\nvoid doSomething(Measure\u003cDistance\u003e distance) {\n  // Convert the distance to some known type that this class can use\n  double inches = distance.as(Units.Inches);\n  // ...\n}\n```\n\n\n## Creating custom units\n\nThis library has many common units, but sometimes you just want to use furlongs or stone:\n\n```java\nUnit\u003cDistance\u003e Furlongs = Units.Feet.aggregate(660);\nUnit\u003cMass\u003e Stone = Units.Pounds.aggregate(14);\nUnit\u003cMass\u003e Milligram = Units.Grams.splitInto(1000);\n```\n\n#### `aggregate(double)` and `splitInto(double)`\n\n`unit.aggregate(n)` creates a new unit that is equivalent to _n_ of the original unit; the new unit is the \"aggregate\" of _n_ of the original unit\n\n`unit.splitInto(n)` is the reverse; it creates a new unit that is equivalent to 1 / _n_ of the original unit (the original unit is \"split into\" _n_ equal parts, each of which is equal to 1 of the resulting unit)\n\nFor example,\n```java\nUnit kilo = someUnit.aggregate(1000);\nUnit milli = someUnit.splitInto(1000);\n```\n\n## Sample use\n\n```java\ninterface Gyro {\n  Measure\u003cAngle\u003e getAngle();\n}\n\nGyro gyro = ...\n\nMeasure\u003cAngle\u003e angle = gyro.getAngle();\n```\n\nGenerally, if you want to declare some constant measure (such as a target angle or distance), those should be declared using the specialized measure classes, eg `Inches targetDistance = Inches.of(...)`. This makes the code much clearer since the unit is determinable just by looking at the left-hand side of the declaration.\n\nIf you're just grabbing the output of a sensor and passing that to something that consumes that kind of measure (e.g. reading a voltage from an analog sensor and passing that to a motor), then it's okay to use a generic `Measure\u003cElectricPotential\u003e sensorOutput = mySensor.getRawVoltage()`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamcarlberg%2Funits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamcarlberg%2Funits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamcarlberg%2Funits/lists"}