[英]Java - JScience define a new unit
I want to define a new distance unit in JScience . 我想在JScience中定义一个新的距离单位。 The "Tutorial" section of the project website just leads to Javadoc which, while fairly complete, is a bit too dense for me to fathom how I actually go about defining my own unit.
项目网站的“教程”部分只是导致Javadoc,虽然相当完整,但有点过于密集,我无法理解我实际上如何定义自己的单元。
Could you provide an example? 你能提供一个例子吗?
Cheers. 干杯。
Pete 皮特
I know this is an old post, but.. i'm going to post the answer anyways, maybe it'll be useful to someone 我知道这是一个很老的帖子,但是......不管怎样我会发布答案,也许这对某人有用
In order to define a custom unit in JScience, you have to extend the class SystemOfUnits
and define here all your custom units. 为了在JScience中定义自定义单元,您必须扩展
SystemOfUnits
类并在此定义所有自定义单元。
Check the exemple below (i'm defining the unit for ACREs) 检查下面的例子(我正在定义ACRE的单位)
public class MyUnits extends SystemOfUnits
{
private static HashSet<Unit<?>> UNITS = new HashSet();
private static final MyUnits INSTANCE = new MyUnits();
public static final Unit<Area> ACRE = myUnits((SI.METER.pow(2)).times(4046.8564224).asType(Area.class));
public static MyUnits getInstance()
{
return INSTANCE;
}
@Override
public Set<Unit<?>> getUnits()
{
return Collections.unmodifiableSet(UNITS);
}
private static <U extends Unit<?>> U myUnits(U unit)
{
UNITS.add(unit);
return unit;
}
}
Please have a look at how JSR 363 RI does this (the successor to 275 which was implemented by JScience 4) https://github.com/unitsofmeasurement/unit-ri/blob/master/src/main/java/tec/units/ri/format/SimpleUnitFormat.java 请看看JSR 363 RI如何做到这一点(由JScience 4实现的275的继任者) https://github.com/unitsofmeasurement/unit-ri/blob/master/src/main/java/tec/units /ri/format/SimpleUnitFormat.java
The default flavor of SimpleUnitFormat supports UTF-8, while ASCII is for limited environments or devices that may have no GUI or limited character sets: SimpleUnitFormat的默认风格支持UTF-8,而ASCII适用于可能没有GUI或有限字符集的有限环境或设备:
DEFAULT.label(MetricPrefix.MICRO(Units.LITRE), "µl"); ASCII.label(MetricPrefix.MICRO(Units.LITRE), "microL");
Note, the label() method will be introduced to JSR 363 API in the next candidate release for Public Draft. 注意,label()方法将在公共草案的下一个候选版本中引入JSR 363 API。 Currently it's part of the RI ( https://github.com/unitsofmeasurement/unit-ri , not JScience, though it may eventually migrate to the new standard, too in V5)
目前它是RI的一部分( https://github.com/unitsofmeasurement/unit-ri ,而不是JScience,尽管它最终可能会在V5中迁移到新标准)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.