Java - JScience 定义一个新单元

发布于 2024-09-28 23:15:31 字数 219 浏览 7 评论 0原文

我想在 JScience 中定义一个新的距离单位。项目网站的“教程”部分只是指向 Javadoc,虽然相当完整,但对我来说有点太密集了,无法理解我实际上是如何定义自己的单元的。

你能举个例子吗?

干杯。

皮特

I want to define a new distance unit in 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.

Could you provide an example?

Cheers.

Pete

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜泪 2024-10-05 23:15:31

我知道这是一篇旧帖子,但是..无论如何我都会发布答案,也许它对某人有用

为了在 JScience 中定义自定义单位,您必须扩展类 SystemOfUnits< /code> 并在此处定义所有自定义单位。

检查下面的示例(我正在定义 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;
   }
}

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.

Check the exemple below (i'm defining the unit for ACREs)

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;
   }
}
何其悲哀 2024-10-05 23:15:31

也许关于Java中单位的这个其他SO问题可以提供帮助。

perhaps this other SO question about units in Java can help.

ぽ尐不点ル 2024-10-05 23:15:31

请看一下 JSR 363 RI 如何做到这一点(JScience 4 实现的 275 的后继者)
https:// /github.com/unitsofmeasurement/unit-ri/blob/master/src/main/java/tec/units/ri/format/SimpleUnitFormat.java

SimpleUnitFormat 的默认风格支持 UTF-8,而 ASCII 用于可能没有 GUI 或有限字符集的有限环境或设备:

DEFAULT.label(MetricPrefix.MICRO(Units.LITRE), "µl");
ASCII.label(MetricPrefix.MICRO(Units.LITRE), "microL");

请注意,label() 方法将在公共草案的下一个候选版本中引入 JSR 363 API。目前它是 RI 的一部分 (https://github.com/unitsofmeasurement/unit-ri,不是 JScience,尽管它最终也可能在 V5 中迁移到新标准)

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

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:

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. 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文