简体   繁体   English

如何使用Java Measurement API更改单元上的标签?

[英]How can I change the label on a Unit using the Java Measurement API?

Problem Introduction 问题介绍

I'm trying to use this implementation of the Java Units of Measurement (JSR 363) . 我正在尝试使用Java测量单位(JSR 363)的这种实现

I would like to change the behavior of several of the provided units. 我想改变几个提供的单位的行为。 An example of one is DEGREE_ANGLE , so that the degree symbol (°) is appended to the end of any Quantity being toString 'd. 一个例子是DEGREE_ANGLE ,因此度符号(°)被附加到任何数量为toString的末尾。 As it is right now, the quantity will print 6.1345983929 [rad?] 就像现在一样,数量将打印6.1345983929 [rad?]

An attempt at a Solution 尝试解决方案

I've tried plenty of different ways to achieve this, but it seems that one way which is present in other examples of AbstractSystemsOfUnits (like from this Unified Code for Units of Measure implementation) is to use a static block like the following: 我已经尝试了很多不同的方法来实现这一点,但似乎在AbstractSystemsOfUnits其他示例中存在的一种方法(比如来自这个统一代码的度量单位实现)是使用如下的静态块:

// //////////////////////////////////////////////////////////////////////////
// Label adjustments for UCUM system
static {
    SimpleUnitFormat.getInstance().label(ATOMIC_MASS_UNIT, "AMU");
    SimpleUnitFormat.getInstance().label(LITER, "l");
    SimpleUnitFormat.getInstance().label(OUNCE, "oz");      
    SimpleUnitFormat.getInstance().label(POUND, "lb");
}

I've tried to adapt this solution by extending the Units class of the implementation I'm using . 我试图通过扩展我正在使用的实现Units类来适应这个解决方案。

public final class MyUnits extends Units {
    static {
        SimpleUnitFormat.getInstance().label(DEGREE_ANGLE, "°");
    }
}

And a simple test trying to use this extension: 并尝试使用此扩展的简单测试:

Quantities.getQuantity(2.009880307999, MyUnits.RADIAN).to(MyUnits.DEGREE_ANGLE).toString();

Gives me 115.157658975 [rad?] 给我115.157658975 [rad?]

Question

How can I change the label on a Unit using the JSR 363 API? 如何使用JSR 363 API更改单元上的标签?

Hmm I gave it a shot and got no issue with the base approach you describe, with that library you use (version 1.0.7)... Have I missed something? 嗯,我给了它一个镜头,没有问题你描述的基本方法,你使用的库(版本1.0.7)...我错过了什么?

No need to extend, the base approach works, here is an example: 无需扩展,基本方法有效,这里有一个例子:

import tec.uom.se.ComparableQuantity;
import tec.uom.se.format.SimpleUnitFormat;
import tec.uom.se.quantity.Quantities;
import javax.measure.quantity.Angle;
import static tec.uom.se.unit.Units.DEGREE_ANGLE;
import static tec.uom.se.unit.Units.RADIAN;

public class CustomLabelForDegrees {

    public static void main(String[] args) {
        ComparableQuantity<Angle> x = Quantities.getQuantity(2.009880307999, RADIAN).to(DEGREE_ANGLE);
        System.out.println(x);
        SimpleUnitFormat.getInstance().label(DEGREE_ANGLE, "°");
        System.out.println(x);
        SimpleUnitFormat.getInstance().label(DEGREE_ANGLE, "☯");
        System.out.println(x);
        SimpleUnitFormat.getInstance().label(DEGREE_ANGLE, "degrees");
        System.out.println(x);
    }
}

This prints: 这打印:

115.15765897479669 [rad?]
115.15765897479669 °
115.15765897479669 ☯
115.15765897479669 degrees

You can do that anywhere you want, anytime. 您可以随时随地进行操作。 It's usually done in a static block in order to be done once, early enough, but it's not a requirement. 它通常在静态块中完成,以便尽早完成,但这不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM