5

Problem Introduction

I'm trying to use this implementation of the Java Units of Measurement (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. As it is right now, the quantity will print 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:

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

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?]

Question

How can I change the label on a Unit using the JSR 363 API?

karobar
  • 1,250
  • 8
  • 30
  • 61

1 Answers1

5

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?

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.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
  • Dang, I should've specified that I'm specifically looking for how to do this by extending a system of units. Now I feel bad because you've answered this question, yet I feel unsatisfied because I asked too broad of a question. As you've said, the question I asked doesn't really make sense (it's self-answering). – karobar Jun 20 '17 at 14:07
  • 1
    OK no problem, think of it as clarification, perhaps you can edit your question? I'll remove that answer. But before that, I'm not sure I understand: can you elaborate about what you are trying to achieve with "extending"? – Hugues M. Jun 20 '17 at 14:09
  • Awesome, thanks for being cool about this. It seems to me like extending the provided Units provides a big benefit in that it obscures the details of where units come from. I've added several custom units, and these can be used alongside the ones from Units, by accessing MyUnits.CUSTOM or MyUnits.LITRE, for example. What do you think about this? – karobar Jun 20 '17 at 14:21
  • 1
    OK, how do you define/initialize `MyUnits.DEGREE_ANGLE` ? Because guess what, if I do that it also works for me :o) Are you sure there is no confusion between `MyUnits.DEGREE_ANGLE` and `Units.DEGREE_ANGLE` ? Perhaps you should name yours differently. -- Your best course of action for now would be to edit your question with those few clarifications. I'm about to leave away from the IDE where I have this setup and won't be able to help for a few hours, maybe until tomorrow. – Hugues M. Jun 20 '17 at 14:40
  • Your reassurance that this was possible made me re-evaluate some previous solutions and I was able to solve my more specific question. I'll write a new question later with the extra specifics and self-answer, I think. Thanks! – karobar Jun 20 '17 at 15:33
  • Dang, @karobar, why didn't you ever add the question and self-answered it?!. – Johannes Jander Nov 04 '19 at 13:07