-
-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to resolve a Quantity instance for a Number and Unit #201
Comments
Matt, Thanks a lot for your question. You are scratching an older itch I'm afraid and the Java language itself has no real solution here at runtime, see a recent analysis: https://stackify.com/jvm-generics-type-erasure/ None of the solutions work really well without either passing the type in every case (instead of a class this is also what ICU4J does) or being restricted to a special condition like inline code or static methods where the generic type may sometimes be available via Reflection. All in all, that is a question for future Java releases, Project Valhalla (I did some very early experiments with it here: https://github.com/unitsofmeasurement/uom-demos/tree/master/console/valhalla) may help, but it is not yet clear, which aspects of it make it into a future JDK and which version. So I put a deferred label on it, we only finalized JSR 385, so it might have a bit of time anyway. Without changing the API we could try to help with that in the RI, e.g. |
Actually I think unitsofmeasurement/indriya#224 might be a ticket for that already. It's phrased very "thin" but I would interpret "list of all valid Dimension for a Quantity LENGTH..." as list of valid units for a |
Hi Werner, thanks for your response. I've dealt with erasure problems like this, I understand what you mean. I guess I was wishing the Class<Q> getQuantityClass(); Which would of course require all private final Class<Q> quantityClass;
/**
* Constructor.
*
* @param quantityClass the quantity class.
*/
protected AbstractUnit(Class<Q> quantityClass) {
this.quantityClass = quantityClass;
}
/**
* Constructor setting a symbol.
*
* @param quantityClass the quantity class.
* @param symbol the unit symbol.
*/
protected AbstractUnit(Class<Q> quantityClass, String symbol) {
this.quantityClass = quantityClass;
this.symbol = symbol;
}
public Class<Q> getQuantityClass() {
return quantityClass;
} I understand that impacts all implementations of This issue comes up for me on a project where I need to deal with arbitrary, user-configured units defined as strings so that I can later capture amount values associated with those units into Thanks again for the info; it looks like for now I will need to use Indriya directly. Perhaps a tweak to the |
I'm not sure if |
Hello, I am not finding how to resolve a
Quantity<?>
instance if I am starting with aNumber
and aUnit<?>
instance, using only thejavax.measure
API. The trouble here is that I don't know theQuantity
class at runtime, so I can't calljavax.measure.spi.ServiceProvider.getQuantityFactory(Class<Q> quantity)
to get the appropriateQuantityFactory
on which I could calljavax.measure.spi.QuantityFactory.create(Number value, Unit<Q> unit)
.I don't know there is a very easy way using Indriya:
tech.units.indriya.quantity.Quantities.getQuantity(Number value, Unit<Q> unit)
but I am trying to understand how to achieve the same thing with only thejavax.measure
API. Could you point me in the right direction?The text was updated successfully, but these errors were encountered: