use of org.hl7.fhir.r4.model.DecimalType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opMod.
private List<Base> opMod(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
if (left.size() == 0 || right.size() == 0) {
return new ArrayList<Base>();
}
if (left.size() > 1) {
throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "mod");
}
if (!left.get(0).isPrimitive()) {
throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "mod", left.get(0).fhirType());
}
if (right.size() > 1) {
throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "mod");
}
if (!right.get(0).isPrimitive()) {
throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "mod", right.get(0).fhirType());
}
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("integer") && r.hasType("integer")) {
int modulus = Integer.parseInt(r.primitiveValue());
if (modulus != 0) {
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) % modulus));
}
} else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer")) {
Decimal d1;
try {
d1 = new Decimal(l.primitiveValue());
Decimal d2 = new Decimal(r.primitiveValue());
result.add(new DecimalType(d1.modulo(d2).asDecimal()));
} catch (UcumException e) {
throw new PathEngineException(e);
}
} else {
throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "mod", left.get(0).fhirType(), right.get(0).fhirType());
}
return result;
}
use of org.hl7.fhir.r4.model.DecimalType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opDivideBy.
private List<Base> opDivideBy(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
if (left.size() == 0 || right.size() == 0) {
return new ArrayList<Base>();
}
if (left.size() > 1) {
throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "/");
}
if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity)) {
throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "/", left.get(0).fhirType());
}
if (right.size() > 1) {
throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "/");
}
if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity)) {
throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "/", right.get(0).fhirType());
}
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("integer", "decimal", "unsignedInt", "positiveInt") && r.hasType("integer", "decimal", "unsignedInt", "positiveInt")) {
Decimal d1;
try {
d1 = new Decimal(l.primitiveValue());
Decimal d2 = new Decimal(r.primitiveValue());
result.add(new DecimalType(d1.divide(d2).asDecimal()));
} catch (UcumException e) {
// just return nothing
}
} else if (l instanceof Quantity && r instanceof Quantity && worker.getUcumService() != null) {
Pair pl = qtyToPair((Quantity) l);
Pair pr = qtyToPair((Quantity) r);
Pair p;
try {
p = worker.getUcumService().divideBy(pl, pr);
result.add(pairToQty(p));
} catch (UcumException e) {
// just return nothing
}
} else {
throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "/", left.get(0).fhirType(), right.get(0).fhirType());
}
return result;
}
use of org.hl7.fhir.r4.model.DecimalType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method qtyEqual.
private Boolean qtyEqual(Quantity left, Quantity right) {
if (!left.hasValue() && !right.hasValue()) {
return true;
}
if (!left.hasValue() || !right.hasValue()) {
return null;
}
if (worker.getUcumService() != null) {
Pair dl = qtyToCanonicalPair(left);
Pair dr = qtyToCanonicalPair(right);
if (dl != null && dr != null) {
if (dl.getCode().equals(dr.getCode())) {
return doEquals(new DecimalType(dl.getValue().asDecimal()), new DecimalType(dr.getValue().asDecimal()));
} else {
return false;
}
}
}
if (left.hasCode() || right.hasCode()) {
if (!(left.hasCode() && right.hasCode()) || !left.getCode().equals(right.getCode())) {
return null;
}
} else if (!left.hasUnit() || right.hasUnit()) {
if (!(left.hasUnit() && right.hasUnit()) || !left.getUnit().equals(right.getUnit())) {
return null;
}
}
return doEquals(new DecimalType(left.getValue()), new DecimalType(right.getValue()));
}
use of org.hl7.fhir.r4.model.DecimalType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method funcRound.
private List<Base> funcRound(ExecutionContext context, List<Base> focus, ExpressionNode expr) {
if (focus.size() != 1) {
throw makeException(expr, I18nConstants.FHIRPATH_FOCUS_PLURAL, "round", focus.size());
}
Base base = focus.get(0);
List<Base> result = new ArrayList<Base>();
if (base.hasType("integer", "decimal", "unsignedInt", "positiveInt")) {
int i = 0;
if (expr.getParameters().size() == 1) {
List<Base> n1 = execute(context, focus, expr.getParameters().get(0), true);
if (n1.size() != 1) {
throw makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, "power", "0", "Multiple Values", "integer");
}
i = Integer.parseInt(n1.get(0).primitiveValue());
}
BigDecimal d = new BigDecimal(base.primitiveValue());
result.add(new DecimalType(d.setScale(i, RoundingMode.HALF_UP)));
} else {
makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, "round", "(focus)", base.fhirType(), "integer or decimal");
}
return result;
}
use of org.hl7.fhir.r4.model.DecimalType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method funcExp.
private List<Base> funcExp(ExecutionContext context, List<Base> focus, ExpressionNode expr) {
if (focus.size() != 1) {
throw makeException(expr, I18nConstants.FHIRPATH_FOCUS_PLURAL, "exp", focus.size());
}
Base base = focus.get(0);
List<Base> result = new ArrayList<Base>();
if (base.hasType("integer", "decimal", "unsignedInt", "positiveInt")) {
Double d = Double.parseDouble(base.primitiveValue());
try {
result.add(new DecimalType(Math.exp(d)));
} catch (Exception e) {
// just return nothing
}
} else {
makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, "exp", "(focus)", base.fhirType(), "integer or decimal");
}
return result;
}
Aggregations