Java Code Examples for javax.measure.unit.Unit#isCompatible()

The following examples show how to use javax.measure.unit.Unit#isCompatible() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Intervals.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
/*********************************************************
 * FormulaTermInterval-specific methods
 *********************************************************/

private Pair<Unit, Integer> compareUnits(TermField[] terms)
{
    Unit unit = terms[0].getParser().getUnit();
    int compatibleNumber = 0;
    for (TermField t : terms)
    {
        final Unit tp = t.getParser().getUnit();
        if (unit != null && tp != null && unit.isCompatible(tp))
        {
            compatibleNumber++;
        }
        if (unit == null)
        {
            unit = tp;
        }
    }
    return new Pair<>(unit == null ? null : unit.getStandardUnit(), compatibleNumber);
}
 
Example 2
Source File: NumberAmountConvertor.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
AmountWrapper getInternalAmount(String description) {
  List<Double> extractNumbers = CategoryMapperUtil.extractNumbers(description);
  Unit<?> unit = CategoryMapperUtil.findDurationUnit(description);
  Collections.sort(extractNumbers);

  if (unit != null && unit.isCompatible(STANDARD_PER_WEEK_UNIT)) {
    if (extractNumbers.size() == 1) {
      return AmountWrapper.create(
          Amount.valueOf(extractNumbers.get(0), unit).to(STANDARD_PER_WEEK_UNIT));
    } else if (extractNumbers.size() > 1) {
      return AmountWrapper.create(
          Amount.rangeOf(
                  extractNumbers.get(0), extractNumbers.get(extractNumbers.size() - 1), unit)
              .to(STANDARD_PER_WEEK_UNIT));
    } else {
      return AmountWrapper.create(
          Amount.valueOf(DEFAULT_NUMBER, unit).to(STANDARD_PER_WEEK_UNIT));
    }
  }

  return null;
}
 
Example 3
Source File: CalculatedValue.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
public void convertUnit(@NonNull Unit sourceUnit, @NonNull Unit targetUnit)
{
    if (valueType == ValueType.INVALID)
    {
        return;
    }
    if (sourceUnit.isCompatible(targetUnit))
    {
        try
        {
            final Measure realV = DecimalMeasure.valueOf(real, sourceUnit);
            real = realV.doubleValue(targetUnit);
            if (isComplex())
            {
                final Measure imaginaryV = DecimalMeasure.valueOf(imaginary, sourceUnit);
                imaginary = imaginaryV.doubleValue(targetUnit);
            }
            unit = (targetUnit.toString() == null || targetUnit.toString().isEmpty()) ? null : targetUnit;
        }
        catch (Exception ex)
        {
            invalidate(ErrorType.INCOMPATIBLE_UNIT);
        }
    }
    else
    {
        invalidate(ErrorType.INCOMPATIBLE_UNIT);
    }
}
 
Example 4
Source File: SeveralTimesConvertor.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: MagmaUnitConverter.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String convertUnit(Unit<? extends Quantity> standardUnit, Unit<? extends Quantity> unit) {
  if (standardUnit != null && unit != null) {
    StringBuilder conversionScript = new StringBuilder();

    for (String standardUnitName : findCompositeUnitNames(standardUnit.toString())) {
      for (String customeUnitName : findCompositeUnitNames(unit.toString())) {
        Unit<?> unit1 = Unit.valueOf(standardUnitName);

        Unit<?> unit2 = Unit.valueOf(customeUnitName);

        if (unit1 != null && unit2 != null && unit1.isCompatible(unit2) && !unit1.equals(unit2)) {
          Amount<?> value2 = Amount.valueOf(1, unit2);
          Amount<?> value1 = value2.to(unit1);
          double estimatedValue1 = value1.getEstimatedValue();
          double estimatedValue2 = value2.getEstimatedValue();

          if (estimatedValue1 > estimatedValue2) {
            conversionScript
                .append(".times(")
                .append(value1.divide(value2).getEstimatedValue())
                .append(")");
          } else {
            conversionScript
                .append(".div(")
                .append(value2.divide(value1).getEstimatedValue())
                .append(")");
          }
        }

        if (conversionScript.length() > 0) {
          return conversionScript.toString();
        }
      }
    }
  }

  return StringUtils.EMPTY;
}