I have some code that uses the javax.measure API to compute the speed of an asset being tracked. I pass the Quantity object as a value into a JTable renderer and the generic parameter (Speed) has been erased. Even if the Speed parameter were there, I would not be able to reflect on that to determine the type of Quantity. How do I determine that the Quantity is a Speed? I want to render the value differently when it is a speed. I currently have code that abuses the asType() method, but that does not seem like how the API should be used.
// FIXME: This seems hokey. There should be a better way.
public static <C extends Quantity<C>> boolean isOfType(final Quantity<?> q, final Class<C> c) {
try {
q.asType(c);
return true;
} catch (final ClassCastException e) {
return false;
}
}
public static boolean isSpeed(final Quantity<?> q) {
return isOfType(q, Speed.class);
}