/** * Casts this unit to a parameterized unit of specified nature or * throw a <code>ClassCastException</code> if the dimension of the * specified quantity and this unit's dimension do not match. * For example:[code] * Unit<Length> LIGHT_YEAR = NonSI.C.times(NonSI.YEAR).asType(Length.class); * [/code] * * @param type the quantity class identifying the nature of the unit. * @return this unit parameterized with the specified type. * @throws ClassCastException if the dimension of this unit is different * from the specified quantity dimension. * @throws UnsupportedOperationException if the specified quantity class * does not have a public static field named "UNIT" holding the * standard unit for the quantity. */ @SuppressWarnings("unchecked") public final <T extends Quantity> Unit<T> asType(Class<T> type) throws ClassCastException { Dimension dim1 = this.getDimension(); Unit<T> u = null; try { u = (Unit<T>)type.getField("UNIT").get(null); } catch (Exception e) { throw new Error(e); } Dimension dim2 = u.getDimension(); if (!dim1.equals(dim2)) throw new ClassCastException(); return (Unit<T>)this; }
Dimension dim1 = this.getDimension(); Unit<T> u = null; try { throw new Error(e); Dimension dim2 = u.getDimension(); if (!dim1.equals(dim2)) throw new ClassCastException();
/** * Casts this unit to a parameterized unit of specified nature or * throw a <code>ClassCastException</code> if the dimension of the * specified quantity and this unit's dimension do not match. * For example:[code] * Unit<Length> LIGHT_YEAR = NonSI.C.times(NonSI.YEAR).asType(Length.class); * [/code] * * @param type the quantity class identifying the nature of the unit. * @return this unit parameterized with the specified type. * @throws ClassCastException if the dimension of this unit is different * from the specified quantity dimension. * @throws UnsupportedOperationException if the specified quantity class * does not have a public static field named "UNIT" holding the * standard unit for the quantity. */ @SuppressWarnings("unchecked") public final <T extends Quantity> Unit<T> asType(Class<T> type) throws ClassCastException { Dimension dim1 = this.getDimension(); Unit<T> u = null; try { u = (Unit<T>)type.getField("UNIT").get(null); } catch (Exception e) { throw new Error(e); } Dimension dim2 = u.getDimension(); if (!dim1.equals(dim2)) throw new ClassCastException(); return (Unit<T>)this; }
/** * Indicates if this unit is compatible with the unit specified. * Units don't need to be equals to be compatible. For example:[code] * RADIAN.equals(ONE) == false * RADIAN.isCompatible(ONE) == true * [/code] * @param that the other unit. * @return <code>this.getDimension().equals(that.getDimension())</code> * @see #getDimension() */ public final boolean isCompatible(Unit<?> that) { return (this == that) || this.getStandardUnit().equals(that.getStandardUnit()) || this.getDimension().equals(that.getDimension()); }
/** * Indicates if this unit is compatible with the unit specified. * Units don't need to be equals to be compatible. For example:[code] * RADIAN.equals(ONE) == false * RADIAN.isCompatible(ONE) == true * [/code] * @param that the other unit. * @return <code>this.getDimension().equals(that.getDimension())</code> * @see #getDimension() */ public final boolean isCompatible(Unit<?> that) { return (this == that) || this.getStandardUnit().equals(that.getStandardUnit()) || this.getDimension().equals(that.getDimension()); }
/** * Indicates if this unit is compatible with the unit specified. * Units don't need to be equals to be compatible. For example:[code] * RADIAN.equals(ONE) == false * RADIAN.isCompatible(ONE) == true * [/code] * @param that the other unit. * @return <code>this.getDimension().equals(that.getDimension())</code> * @see #getDimension() */ public final boolean isCompatible(Unit<?> that) { return (this == that) || this.getStandardUnit().equals(that.getStandardUnit()) || this.getDimension().equals(that.getDimension()); }
@Override public boolean isVolumeDensity() { boolean ret = false; if (_unit instanceof ProductUnit<?>) { ProductUnit<?> pu = (ProductUnit<?>) _unit; for (int i = 0; i < pu.getUnitCount(); i++) { javax.measure.unit.Unit<?> su = pu.getUnit(i); int power = pu.getUnitPow(i); if (su.getDimension().equals(Dimension.LENGTH.pow(3)) && power == -1 || (su.getDimension().equals(Dimension.LENGTH) && power == -3)) { ret = true; break; } } } return ret; }
@Override public boolean isArealDensity() { boolean ret = false; if (_unit instanceof ProductUnit<?>) { ProductUnit<?> pu = (ProductUnit<?>) _unit; for (int i = 0; i < pu.getUnitCount(); i++) { javax.measure.unit.Unit<?> su = pu.getUnit(i); int power = pu.getUnitPow(i); if ((su.getDimension().equals(Dimension.LENGTH.pow(2)) && power == -1) || (su.getDimension().equals(Dimension.LENGTH) && power == -2)) { ret = true; break; } } } return ret; }
@Override public IUnit getVolumeExtentUnit() { if (_unit instanceof ProductUnit<?>) { ProductUnit<?> pu = (ProductUnit<?>) _unit; for (int i = 0; i < pu.getUnitCount(); i++) { javax.measure.unit.Unit<?> su = pu.getUnit(i); int power = pu.getUnitPow(i); if (su.getDimension().equals(Dimension.LENGTH.pow(3)) && power == -1 || (su.getDimension().equals(Dimension.LENGTH) && power == -3)) { return new Unit(su); } } } return null; }
@Override public boolean isRate() { boolean ret = false; if (_unit instanceof ProductUnit<?>) { ProductUnit<?> pu = (ProductUnit<?>) _unit; for (int i = 0; i < pu.getUnitCount(); i++) { javax.measure.unit.Unit<?> su = pu.getUnit(i); int power = pu.getUnitPow(i); if (su.getDimension().equals(Dimension.TIME) && power == -1) { ret = true; break; } } } return ret; }
@Override public boolean isLengthDensity() { boolean ret = false; if (_unit instanceof ProductUnit<?>) { ProductUnit<?> pu = (ProductUnit<?>) _unit; for (int i = 0; i < pu.getUnitCount(); i++) { javax.measure.unit.Unit<?> su = pu.getUnit(i); int power = pu.getUnitPow(i); if (su.getDimension().equals(Dimension.LENGTH) && power == -1) { ret = true; break; } } } return ret; }
@Override public IUnit getArealExtentUnit() { if (_unit instanceof ProductUnit<?>) { ProductUnit<?> pu = (ProductUnit<?>) _unit; for (int i = 0; i < pu.getUnitCount(); i++) { javax.measure.unit.Unit<?> su = pu.getUnit(i); int power = pu.getUnitPow(i); if (su.getDimension().equals(Dimension.LENGTH.pow(2)) && power == -1) { return new Unit(su); } else if (su.getDimension().equals(Dimension.LENGTH) && power == -2) { return new Unit(su.pow(2)); } } } return null; }
@Override public IUnit getTimeExtentUnit() { if (_unit instanceof ProductUnit<?>) { ProductUnit<?> pu = (ProductUnit<?>) _unit; for (int i = 0; i < pu.getUnitCount(); i++) { javax.measure.unit.Unit<?> su = pu.getUnit(i); int power = pu.getUnitPow(i); if (su.getDimension().equals(Dimension.TIME) && power == -1) { return new Unit(su); } } } return null; }
private static boolean areFunctionallyIdenticalUnits(Unit<?> left, Unit<?> right) { if (!left.isCompatible(right)) { return false; } Unit<?> divided = left.divide(right); if (!divided.getDimension().equals(javax.measure.unit.Dimension.NONE)) { return false; } return divided.asType(Dimensionless.class).getConverterTo(Unit.ONE).equals( UnitConverter.IDENTITY); }
@Override public IUnit getLengthExtentUnit() { if (_unit instanceof ProductUnit<?>) { ProductUnit<?> pu = (ProductUnit<?>) _unit; for (int i = 0; i < pu.getUnitCount(); i++) { javax.measure.unit.Unit<?> su = pu.getUnit(i); int power = pu.getUnitPow(i); if (su.getDimension().equals(Dimension.LENGTH) && power == -1) { return new Unit(su); } } } return null; }
/** * Returns the dimension of this unit (depends upon the current * dimensional {@link Dimension.Model model}). * * @return the dimension of this unit for the current model. */ public final Dimension getDimension() { Unit<?> systemUnit = this.getStandardUnit(); if (systemUnit instanceof BaseUnit) return Dimension.getModel().getDimension((BaseUnit<?>) systemUnit); if (systemUnit instanceof AlternateUnit) return ((AlternateUnit<?>) systemUnit).getParent().getDimension(); // Product of units. ProductUnit<?> productUnit = (ProductUnit<?>) systemUnit; Dimension dimension = Dimension.NONE; for (int i = 0; i < productUnit.getUnitCount(); i++) { Unit<?> unit = productUnit.getUnit(i); Dimension d = unit.getDimension().pow(productUnit.getUnitPow(i)) .root(productUnit.getUnitRoot(i)); dimension = dimension.times(d); } return dimension; }
/** * Returns the dimension of this unit (depends upon the current * dimensional {@link Dimension.Model model}). * * @return the dimension of this unit for the current model. */ public final Dimension getDimension() { Unit<?> systemUnit = this.getStandardUnit(); if (systemUnit instanceof BaseUnit) return Dimension.getModel().getDimension((BaseUnit<?>) systemUnit); if (systemUnit instanceof AlternateUnit) return ((AlternateUnit<?>) systemUnit).getParent().getDimension(); // Product of units. ProductUnit<?> productUnit = (ProductUnit<?>) systemUnit; Dimension dimension = Dimension.NONE; for (int i = 0; i < productUnit.getUnitCount(); i++) { Unit<?> unit = productUnit.getUnit(i); Dimension d = unit.getDimension().pow(productUnit.getUnitPow(i)) .root(productUnit.getUnitRoot(i)); dimension = dimension.times(d); } return dimension; }
/** * Returns the dimension of this unit (depends upon the current * dimensional {@link Dimension.Model model}). * * @return the dimension of this unit for the current model. */ public final Dimension getDimension() { Unit<?> systemUnit = this.getStandardUnit(); if (systemUnit instanceof BaseUnit) return Dimension.getModel().getDimension((BaseUnit<?>) systemUnit); if (systemUnit instanceof AlternateUnit) return ((AlternateUnit<?>) systemUnit).getParent().getDimension(); // Product of units. ProductUnit<?> productUnit = (ProductUnit<?>) systemUnit; Dimension dimension = Dimension.NONE; for (int i = 0; i < productUnit.getUnitCount(); i++) { Unit<?> unit = productUnit.getUnit(i); Dimension d = unit.getDimension().pow(productUnit.getUnitPow(i)) .root(productUnit.getUnitRoot(i)); dimension = dimension.times(d); } return dimension; }
/** * Returns a converter of numeric values from this unit to another unit. * * @param that the unit to which to convert the numeric values. * @return the converter from this unit to <code>that</code> unit. * @throws ConversionException if the conveter cannot be constructed * (e.g. <code>!this.isCompatible(that)</code>). */ public final UnitConverter getConverterTo(Unit<?> that) throws ConversionException { if (this.equals(that)) return UnitConverter.IDENTITY; Unit<?> thisSystemUnit = this.getStandardUnit(); Unit<?> thatSystemUnit = that.getStandardUnit(); if (thisSystemUnit.equals(thatSystemUnit)) return that.toStandardUnit().inverse().concatenate( this.toStandardUnit()); // Use dimensional transforms. if (!thisSystemUnit.getDimension() .equals(thatSystemUnit.getDimension())) throw new ConversionException(this + " is not compatible with " + that); // Transform between SystemUnit and BaseUnits is Identity. UnitConverter thisTransform = this.toStandardUnit().concatenate( transformOf(this.getBaseUnits())); UnitConverter thatTransform = that.toStandardUnit().concatenate( transformOf(that.getBaseUnits())); return thatTransform.inverse().concatenate(thisTransform); }
/** * Returns a converter of numeric values from this unit to another unit. * * @param that the unit to which to convert the numeric values. * @return the converter from this unit to <code>that</code> unit. * @throws ConversionException if the conveter cannot be constructed * (e.g. <code>!this.isCompatible(that)</code>). */ public final UnitConverter getConverterTo(Unit<?> that) throws ConversionException { if (this.equals(that)) return UnitConverter.IDENTITY; Unit<?> thisSystemUnit = this.getStandardUnit(); Unit<?> thatSystemUnit = that.getStandardUnit(); if (thisSystemUnit.equals(thatSystemUnit)) return that.toStandardUnit().inverse().concatenate( this.toStandardUnit()); // Use dimensional transforms. if (!thisSystemUnit.getDimension() .equals(thatSystemUnit.getDimension())) throw new ConversionException(this + " is not compatible with " + that); // Transform between SystemUnit and BaseUnits is Identity. UnitConverter thisTransform = this.toStandardUnit().concatenate( transformOf(this.getBaseUnits())); UnitConverter thatTransform = that.toStandardUnit().concatenate( transformOf(that.getBaseUnits())); return thatTransform.inverse().concatenate(thisTransform); }