Refine search
UnitConverter toKilometers = MILE.getConverterTo(KILOMETER); double km = toKilometers.convert(Measure.valueOf(100, MILE).doubleValue(MILE));
public double doubleValue(Unit<Q> unit) { if ((unit == _unit) || (unit.equals(_unit))) return _value; return _unit.getConverterTo(unit).convert(_value); }
if (cs.getAxis(j).getDirection().equals(targetDirections[i])) { axisIndices[i] = j; converters[i] = cs.getAxis(j).getUnit().getConverterTo(targetUnits[i]); notfound = false; returnable[(i * dimension) + j] = converters[j].convert(positions[i].getOrdinate(axisIndices[j]));
/** * Returns the latitude value as <code>double</code> * * @param unit the angle unit of the latitude to return. * @return the latitude stated in the specified unit. */ public final double latitudeValue(Unit<Angle> unit) { return (unit == DEGREE_ANGLE) ? _latitude : (unit == RADIAN) ? DEGREE_TO_RADIAN.convert(_latitude) : DEGREE_ANGLE.getConverterTo(unit).convert(_latitude); }
if (isNotBlank(nextValue.getCode())) { Unit<? extends javax.measure.quantity.Quantity> unit = Unit.valueOf(nextValue.getCode()); javax.measure.converter.UnitConverter dayConverter = unit.getConverterTo(NonSI.DAY); double dayValue = dayConverter.convert(nextValue.getValue().doubleValue()); Duration newValue = new Duration(); newValue.setSystem(SearchParamConstants.UCUM_NS); newValue.setCode(NonSI.DAY.toString()); newValue.setValue(dayValue); nextValue = newValue;
public double getOrdinate(int dimension) throws IndexOutOfBoundsException { if (dimension == 0) { Unit<?> u = GeographicCRS.LATITUDE_LONGITUDE_CS.getAxis(0).getUnit(); return DEGREE_ANGLE.getConverterTo(u).convert(_latitude); } else if (dimension == 1) { Unit<?> u = GeographicCRS.LATITUDE_LONGITUDE_CS.getAxis(1).getUnit(); return DEGREE_ANGLE.getConverterTo(u).convert(_longitude); } else { throw new IndexOutOfBoundsException(); } }
if (linearUnit != null && !SI.METER.isCompatible(linearUnit)) { throw new IllegalArgumentException(Errors.format( ErrorKeys.NON_LINEAR_UNIT_$1, linearUnit)); if (SI.METER.isCompatible(linearUnit)) { if (SI.METER.equals(linearUnit)) { metadata.addGeoShortParam( GeoTiffPCSCodes.ProjLinearUnitsGeoKey, metadata.addGeoDoubleParam( GeoTiffPCSCodes.ProjLinearUnitSizeGeoKey, linearUnit .getConverterTo(SI.METER).convert(1)); if (NonSI.FOOT.equals(linearUnit)) { metadata.addGeoDoubleParam( GeoTiffPCSCodes.ProjLinearUnitSizeGeoKey, linearUnit .getConverterTo(SI.METER).convert(1)); if (NonSI.YARD.equals(linearUnit)) { .getConverterTo(SI.METER).convert(1));
AmountWrapper getInternalAmount(String description) { Unit<?> unit = CategoryMapperUtil.findDurationUnit(description); if (unit != null && unit.isCompatible(STANDARD_PER_WEEK_UNIT)) { return AmountWrapper.create( Amount.rangeOf((double) 3, NonSI.DAY.inverse().getConverterTo(unit).convert(1) - 1, unit) .to(STANDARD_PER_WEEK_UNIT), false); } return null; } }
convertAndCast(final Class<N> type, final Unit<?> targetUnits) throws ConversionException if (targetUnits == null || targetUnits.equals(units)) { if (type.equals(elementClass)) { @SuppressWarnings("unchecked") return new MeasurementRange<N>(type, this, targetUnits); final UnitConverter converter = units.getConverterTo(targetUnits); if (converter.equals(UnitConverter.IDENTITY)) { return new MeasurementRange<N>(type, this, targetUnits); Double minimum = converter.convert(getMinimum()); Double maximum = converter.convert(getMaximum()); if (minimum.compareTo(maximum) > 0) { final Double td = minimum;
public static Unit<?> getMostGeneralUnit(List<Unit<?>> candidateUnits) { candidateUnits.sort( (o1, o2) -> { UnitConverter converterTo = o1.inverse().getConverterTo(o2.inverse()); if (converterTo.convert(1) > 1) { return -1; } else { return 1; } }); return candidateUnits.isEmpty() ? null : candidateUnits.get(0); }
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); }
/** * 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 the projected distance of the position from the central meridian. * * @param unit the length unit of the easting to return. * @return the easting stated in the specified unit. */ public final double eastingValue(Unit<Length> unit) { return unit.equals(METRE) ? _easting : METRE.getConverterTo(unit) .convert(_easting); }
/** * Indicates if this transformed unit is considered equals to the specified * object (both are transformed units with equal parent unit and equal * converter to parent unit). * * @param that the object to compare for equality. * @return <code>true</code> if <code>this</code> and <code>that</code> * are considered equals; <code>false</code>otherwise. */ public boolean equals(Object that) { if (this == that) return true; if (!(that instanceof TransformedUnit)) return false; TransformedUnit<?> thatUnit = (TransformedUnit<?>) that; return this._parentUnit.equals(thatUnit._parentUnit) && this._toParentUnit.equals(thatUnit._toParentUnit); }
UnitConverter multiplier = UnitConverter.IDENTITY; if(!(from).isCompatible(to) && (from.inverse()).isCompatible(to)){ fromParent = fromParent.inverse(); multiplier = new MultiplyConverter(-1); return (getConverter(fromParent, to)).concatenate((transform.inverse()).concatenate(multiplier)); return multiplier.concatenate(transform.concatenate(getConverter(from, toParent)));
@Override public UnitConverter toStandardUnit() { if (hasOnlyStandardUnit()) return UnitConverter.IDENTITY; UnitConverter converter = UnitConverter.IDENTITY; for (int i = 0; i < _elements.length; i++) { UnitConverter cvtr = _elements[i]._unit.toStandardUnit(); if (!cvtr.isLinear()) throw new ConversionException(_elements[i]._unit + " is non-linear, cannot convert"); if (_elements[i]._root != 1) throw new ConversionException(_elements[i]._unit + " holds a base unit with fractional exponent"); int pow = _elements[i]._pow; if (pow < 0) { // Negative power. pow = -pow; cvtr = cvtr.inverse(); } for (int j = 0; j < pow; j++) { converter = converter.concatenate(cvtr); } } return converter; }
private static UnitConverter transformOf(Unit<?> baseUnits) { if (baseUnits instanceof BaseUnit) return Dimension.getModel().getTransform((BaseUnit<?>) baseUnits); // Product of units. ProductUnit<?> productUnit = (ProductUnit<?>) baseUnits; UnitConverter converter = UnitConverter.IDENTITY; for (int i = 0; i < productUnit.getUnitCount(); i++) { Unit<?> unit = productUnit.getUnit(i); UnitConverter cvtr = transformOf(unit); if (!cvtr.isLinear()) throw new ConversionException(baseUnits + " is non-linear, cannot convert"); if (productUnit.getUnitRoot(i) != 1) throw new ConversionException(productUnit + " holds a base unit with fractional exponent"); int pow = productUnit.getUnitPow(i); if (pow < 0) { // Negative power. pow = -pow; cvtr = cvtr.inverse(); } for (int j = 0; j < pow; j++) { converter = converter.concatenate(cvtr); } } return converter; }
@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof AddConverter) { double offset = _offset + ((AddConverter)converter)._offset; return valueOf(offset); } else { return super.concatenate(converter); } }