congrats Icon
New! Tabnine Pro 14-day free trial
Start a free trial
Tabnine Logo
Unit.getConverterToAny
Code IndexAdd Tabnine to your IDE (free)

How to use
getConverterToAny
method
in
javax.measure.Unit

Best Java code snippets using javax.measure.Unit.getConverterToAny (Showing top 20 results out of 315)

origin: geotools/geotools

/**
 * Gets a UnitConverter between two units, wrapping any raised exception in a
 * IllegalArgumentException.
 *
 * @param fromUnit
 * @param toUnit
 * @return
 * @throws IllegalArgumentException if unit1 can't be converter to unit2
 */
public static UnitConverter getConverterToAny(Unit<?> fromUnit, Unit<?> toUnit) {
  try {
    return fromUnit.getConverterToAny(toUnit);
  } catch (UnconvertibleException | IncommensurableException e) {
    throw new IllegalArgumentException("Can't convert to the candidate unit", e);
  }
}
origin: geotools/geotools

@Override
public int hashCode() {
  if (unit instanceof TransformedUnit<?>) {
    Unit<?> systemUnit = unit.getSystemUnit();
    try {
      float factor1 = (float) unit.getConverterToAny(systemUnit).convert(1.0);
      return Objects.hash(systemUnit, Float.floatToIntBits(factor1));
    } catch (UnconvertibleException | IncommensurableException e) {
    } catch (Throwable e) {
    }
  }
  return unit.hashCode();
}
origin: geotools/geotools

longitudeAxis = i;
longitudeConverter = unit.getConverterToAny(NonSI.DEGREE_ANGLE);
latitudeConverter = unit.getConverterToAny(NonSI.DEGREE_ANGLE);
continue;
heightConverter = unit.getConverterToAny(SI.METRE);
continue;
origin: geotools/geotools

append(unit.getConverterToAny(base).convert(1));
origin: eclipse/smarthome

/**
 * Convert this QuantityType to a new {@link QuantityType} using the given target unit.
 *
 * @param targetUnit the unit to which this {@link QuantityType} will be converted to.
 * @return the new {@link QuantityType} in the given {@link Unit} or {@code null} in case of a
 */
@SuppressWarnings("unchecked")
public @Nullable QuantityType<T> toUnit(Unit<?> targetUnit) {
  if (!targetUnit.equals(getUnit())) {
    try {
      UnitConverter uc = getUnit().getConverterToAny(targetUnit);
      Quantity<?> result = Quantities.getQuantity(uc.convert(quantity.getValue()), targetUnit);
      return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit);
    } catch (UnconvertibleException | IncommensurableException e) {
      logger.debug("Unable to convert unit from {} to {}", getUnit(), targetUnit);
      return null;
    }
  }
  return this;
}
origin: geotools/geotools

UnitConverter fromDegrees;
try {
  fromDegrees = NonSI.DEGREE_ANGLE.getConverterToAny(unit);
} catch (UnconvertibleException | IncommensurableException e) {
  throw new IllegalArgumentException(
origin: geotools/geotools

  converter = units.getConverterToAny(targetUnits);
} catch (UnconvertibleException | IncommensurableException e) {
  throw new IllegalArgumentException(e);
origin: geotools/geotools

for (int i = 0; i < converters.length; i++) {
  try {
    converters[i] = getAxis(i).getUnit().getConverterToAny(unit);
  } catch (UnconvertibleException
      | IndexOutOfBoundsException
origin: org.apache.sis.storage/sis-netcdf

/**
 * Returns the converter from the given source unit (which may be {@code null}) to the
 * given target unit, or {@code null} if none or incompatible.
 */
private UnitConverter getConverterTo(final Unit<?> source, final Unit<?> target) {
  if (source != null) try {
    return source.getConverterToAny(target);
  } catch (IncommensurableException e) {
    warning(e);
  }
  return null;
}
origin: opengeospatial/geoapi

/**
 * Converts the given angle from degrees to the angular units used by this axis.
 *
 * @param  angle  the angle to convert.
 * @return the converted angle.
 * @throws IllegalStateException if this axis does not use angular units.
 */
private double toAngularUnit(final double angle) throws IllegalStateException {
  try {
    return DEGREE.getConverterToAny(unit).convert(angle);
  } catch (IncommensurableException e) {
    throw new IllegalStateException(e);
  }
}
origin: opengeospatial/geoapi

/**
 * Sets the parameter to the given value, after conversion to metres or decimal degrees.
 */
@Override
public void setValue(double value, final Unit<?> unit) {
  final Unit<?> standardUnit = getStandardUnit(unit);
  if (standardUnit != null) try {
    value = unit.getConverterToAny(standardUnit).convert(value);
  } catch (IncommensurableException e) {
    throw new IllegalArgumentException(e);              // Should never happen actually.
  }
  this.value = value;
}
origin: Geomatys/geotoolkit

/**
 * Workaround for RFE #4093999 ("Relax constraint on placement of this()/super()
 * call in constructors").
 */
private static UnitConverter getConverter(final Unit<?> source) throws IncommensurableException {
  if (source == null) {
    throw new IncommensurableException(Errors.format(Errors.Keys.NoUnit));
  }
  return source.getConverterToAny(Units.MILLISECOND);
}
origin: opengeospatial/geoapi

/**
 * Gets the unit given by {@link #getIdentifiedObject()},
 * then creates and returns the converter from that unit to the base unit.
 *
 * @throws FactoryException if an error occurred while creating the unit from the EPSG code.
 */
private UnitConverter createConverter() throws FactoryException {
  final Unit<?> unit = getIdentifiedObject();
  assertNotNull("Unit", unit);
  final UnitConverter converter;
  try {
    converter = unit.getConverterToAny(baseUnit);
  } catch (IncommensurableException e) {
    throw new AssertionError("Can not convert “" + name + "” from “" + unit + "” to “" + baseUnit + "”.", e);
  }
  return converter;
}
origin: opengeospatial/geoapi

/**
 * Returns the numeric value of the operation parameter in the specified unit of measure.
 * This convenience method applies unit conversion from metres or decimal degrees as needed.
 *
 * @param  unit  the unit of measure for the value to be returned.
 * @return the numeric value represented by this parameter after conversion to {@code unit}.
 */
@Override
public double doubleValue(final Unit<?> unit) {
  double c = value;
  final Unit<?> standardUnit = getStandardUnit(unit);
  if (standardUnit != null) try {
    c = standardUnit.getConverterToAny(unit).convert(c);
  } catch (IncommensurableException e) {
    throw new IllegalArgumentException(e);              // Should never happen actually.
  }
  return c;
}
origin: org.apache.sis.core/sis-referencing

/**
 * Returns the converter to be used by {@link #doubleValue(Unit)} and {@link #doubleValueList(Unit)}.
 */
private UnitConverter getConverterTo(final Unit<?> unit) {
  final Unit<?> source = getUnit();
  if (source == null) {
    throw new IllegalStateException(Resources.format(Resources.Keys.UnitlessParameter_1, Verifier.getDisplayName(descriptor)));
  }
  ensureNonNull("unit", unit);
  final short expectedID = Verifier.getUnitMessageID(source);
  if (Verifier.getUnitMessageID(unit) != expectedID) {
    throw new IllegalArgumentException(Errors.format(expectedID, unit));
  }
  try {
    return source.getConverterToAny(unit);
  } catch (IncommensurableException e) {
    throw new IllegalArgumentException(Errors.format(Errors.Keys.IncompatibleUnits_2, source, unit), e);
  }
}
origin: apache/sis

/**
 * Returns the converter to be used by {@link #doubleValue(Unit)} and {@link #doubleValueList(Unit)}.
 */
private UnitConverter getConverterTo(final Unit<?> unit) {
  final Unit<?> source = getUnit();
  if (source == null) {
    throw new IllegalStateException(Resources.format(Resources.Keys.UnitlessParameter_1, Verifier.getDisplayName(descriptor)));
  }
  ensureNonNull("unit", unit);
  final short expectedID = Verifier.getUnitMessageID(source);
  if (Verifier.getUnitMessageID(unit) != expectedID) {
    throw new IllegalArgumentException(Errors.format(expectedID, unit));
  }
  try {
    return source.getConverterToAny(unit);
  } catch (IncommensurableException e) {
    throw new IllegalArgumentException(Errors.format(Errors.Keys.IncompatibleUnits_2, source, unit), e);
  }
}
origin: apache/sis

/**
 * Verifies the conversion factory of {@link Units#PSU}.
 *
 * @see <a href="https://issues.apache.org/jira/browse/SIS-413">SIS-413</a>
 *
 * @throws IncommensurableException if the conversion can not be applied.
 */
@Test
public void testSalinityConversionFactor() throws IncommensurableException {
  assertEquals(0.001, PSU.getConverterToAny(UNITY)  .convert(1), STRICT);
  assertEquals(0.1,   PSU.getConverterToAny(PERCENT).convert(1), STRICT);
}
origin: tec.uom/uom-se

@Override
public ComparableQuantity<?> multiply(Quantity<?> multiplier) {
 if (getUnit().equals(multiplier.getUnit())) {
  return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
 }
 Unit<?> mulUnit = getUnit().multiply(multiplier.getUnit());
 UnitConverter conv;
 try {
  conv = getUnit().getConverterToAny(mulUnit);
  return TimeQuantities.getQuantity(value * conv.convert(multiplier.getValue()).intValue(), timeUnit);
 } catch (UnconvertibleException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
 } catch (IncommensurableException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
 }
}
origin: tec.units/indriya

@Override
public ComparableQuantity<?> multiply(Quantity<?> multiplier) {
 if (getUnit().equals(multiplier.getUnit())) {
  return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
 }
 Unit<?> mulUnit = getUnit().multiply(multiplier.getUnit());
 UnitConverter conv;
 try {
  conv = getUnit().getConverterToAny(mulUnit);
  return TimeQuantities.getQuantity(value * conv.convert(multiplier.getValue()).intValue(), timeUnit);
 } catch (UnconvertibleException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
 } catch (IncommensurableException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
 }
}
origin: apache/sis

/**
 * Verifies that the given units derived from litres ({@code u1}) is equivalent to the given units derived
 * from cubic metres ({@code u2}). The conversion between those two units is expected to be identity.
 */
private static void assertEquivalent(final String s1, final Unit<Volume> u1,
                   final String s2, final Unit<Volume> u2)
    throws IncommensurableException
{
  assertEquals("unit1.symbol", s1, u1.getSymbol());
  assertEquals("unit2.symbol", s2, u2.getSymbol());
  assertTrue("getConverterTo(…).isIdentity", u1.getConverterTo(u2).isIdentity());
  assertTrue("getConverterTo(…).isIdentity", u2.getConverterTo(u1).isIdentity());
  assertTrue("getConverterTo(…).isIdentity", u1.getConverterToAny(u2).isIdentity());
  assertTrue("getConverterTo(…).isIdentity", u2.getConverterToAny(u1).isIdentity());
}
javax.measureUnitgetConverterToAny

Popular methods of Unit

  • getConverterTo
  • toString
  • multiply
  • getSystemUnit
  • asType
  • isCompatible
  • divide
  • getSymbol
  • getDimension
  • transform
  • getName
  • pow
  • getName,
  • pow,
  • root,
  • shift,
  • getBaseUnits,
  • inverse,
  • alternate

Popular in Java

  • Parsing JSON documents to java classes using gson
  • compareTo (BigDecimal)
  • setContentView (Activity)
  • getSystemService (Context)
  • Kernel (java.awt.image)
  • ArrayList (java.util)
    ArrayList is an implementation of List, backed by an array. All optional operations including adding
  • Servlet (javax.servlet)
    Defines methods that all servlets must implement. A servlet is a small Java program that runs within
  • ServletException (javax.servlet)
    Defines a general exception a servlet can throw when it encounters difficulty.
  • FileUtils (org.apache.commons.io)
    General file manipulation utilities. Facilities are provided in the following areas: * writing to a
  • Join (org.hibernate.mapping)
  • Top 17 Free Sublime Text Plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now