@Override public boolean isCompatible(IValueMediator other) { return other instanceof Unit && ((Unit) other)._unit.isCompatible(_unit); }
public boolean validateRenderedUnit() { if (!useRenderedUnit) { renderedUnit = unit; renderedUnitString = UnitUtil.formatLocal(renderedUnit); return true; } // integral unit should have same dimensions as the default integrated unit if (renderedUnit == null) return false; return renderedUnit.isCompatible(unit); }
boolean unitsCompatible(Amount<?> sourceAmount, Amount<?> targetAmount) { return sourceAmount.getUnit().isCompatible(STANDARD_UNIT) && targetAmount.getUnit().isCompatible(STANDARD_UNIT) && sourceAmount.getUnit().isCompatible(targetAmount.getUnit()); }
boolean unitsCompatible(Amount<?> sourceAmount, Amount<?> targetAmount) { return sourceAmount.getUnit().isCompatible(STANDARD_UNIT) && targetAmount.getUnit().isCompatible(STANDARD_UNIT) && sourceAmount.getUnit().isCompatible(targetAmount.getUnit()); }
private static String compare(Unit<?> unit1, Unit<?> unit2) { StringBuilder conversionScript = new StringBuilder(); 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(")"); } } return conversionScript.toString(); }
/** * Validate the Integral Unit * Setting a default if its not enabled * * @return */ public boolean validateIntegralUnit() { if (!useIntegralUnit) { integralUnit = defaultIntegralUnit(); integralUnitString = UnitUtil.formatLocal(integralUnit); return true; } // integral unit should have same dimensions as the default integrated unit if (integralUnit == null) return false; return integralUnit.isCompatible(defaultIntegralUnit()); }
/** * Return True if two units strings are comparable. * * @param units1 * @param units2 * @return true if two units comparable * @throws IllegalArgumentException if units1 or units2 null */ public boolean unitsComparable(String units1, String units2) { if (unitsEquivalent(units1, units2)){ return true; }else{ Unit<?> u1 = Unit.valueOf(units1); Unit<?> u2 = Unit.valueOf(units2); return u1.isCompatible(u2); } }
/** * Return True if two units strings are comparable. * * @param units1 * @param units2 * @return true if two units comparable * @throws IllegalArgumentException if units1 or units2 null */ public boolean unitsComparable(String units1, String units2) { if (unitsEquivalent(units1, units2)){ return true; }else{ Unit<?> u1 = Unit.valueOf(units1); Unit<?> u2 = Unit.valueOf(units2); return u1.isCompatible(u2); } }
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; }
Unit<?> unit2 = Unit.valueOf(units2); if (!unit1.isCompatible(unit2)){ throw new IllegalArgumentException("units '"+units1+"' is not comparable to '"+units2+"'");
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; }
/** * Creates a {@link CartesianCS} for a {@link ProjectedCRS} given the * provided {@link Unit}. * * @todo consider caching this items * @param linearUnit * to be used for building this {@link CartesianCS}. * @return an instance of {@link CartesianCS} using the provided * {@link Unit}, */ private DefaultCartesianCS createProjectedCS(Unit linearUnit) { if (linearUnit == null) throw new NullPointerException( "Error when trying to create a PCS using this linear UoM "); if (!linearUnit.isCompatible(SI.METER)) throw new IllegalArgumentException( "Error when trying to create a PCS using this linear UoM " + linearUnit.toString()); return new DefaultCartesianCS(Vocabulary.formatInternational( VocabularyKeys.PROJECTED).toString(), new DefaultCoordinateSystemAxis(Vocabulary .formatInternational(VocabularyKeys.EASTING), "E", AxisDirection.EAST, linearUnit), new DefaultCoordinateSystemAxis(Vocabulary .formatInternational(VocabularyKeys.NORTHING), "N", AxisDirection.NORTH, linearUnit)); }
if (unit1 != null && unit2 != null && unit1.isCompatible(unit2) && !unit1.equals(unit2)) { Amount<?> value2 = Amount.valueOf(1, unit2); Amount<?> value1 = value2.to(unit1);
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); }
UnitConverter multiplier = UnitConverter.IDENTITY; if(!(from).isCompatible(to) && (from.inverse()).isCompatible(to)){ fromParent = fromParent.inverse(); multiplier = new MultiplyConverter(-1); UnitConverter multiplier = UnitConverter.IDENTITY; if(!(from).isCompatible(to) && (from).isCompatible(to.inverse())){ toParent = toParent.inverse(); multiplier = new MultiplyConverter(-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; } }
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; } }
/********************************************************* * 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); }
public ValueType subtract(CalculatedValue f, CalculatedValue g) { if (unitExists(f, g)) { if (f.unit == null || g.unit == null || !f.unit.isCompatible(g.unit)) { return invalidate(ErrorType.INCOMPATIBLE_UNIT); } unit = f.unit; } else { unit = null; } if (f.isComplex() || g.isComplex()) { return setComplexValue(f.real - g.real, f.imaginary - g.imaginary); } else { return setValue(f.real - g.real); } }
public ValueType add(CalculatedValue f, CalculatedValue g) { if (unitExists(f, g)) { if (f.unit == null || g.unit == null || !f.unit.isCompatible(g.unit)) { return invalidate(ErrorType.INCOMPATIBLE_UNIT); } unit = f.unit; } else { unit = null; } if (f.isComplex() || g.isComplex()) { return setComplexValue(f.real + g.real, f.imaginary + g.imaginary); } else { return setValue(f.real + g.real); } }