use of org.hl7.fhir.r4.model.IntegerType 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 new PathEngineException("Error performing mod: left operand has more than one value", expr.getStart(), expr.toString());
if (!left.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing mod: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
if (right.size() > 1)
throw new PathEngineException("Error performing mod: right operand has more than one value", expr.getStart(), expr.toString());
if (!right.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing mod: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("integer") && r.hasType("integer"))
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) % Integer.parseInt(r.primitiveValue())));
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 new PathEngineException(String.format("Error performing mod: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()), expr.getStart(), expr.toString());
return result;
}
use of org.hl7.fhir.r4.model.IntegerType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opMinus.
private List<Base> opMinus(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 new PathEngineException("Error performing -: left operand has more than one value", expr.getStart(), expr.toString());
if (!left.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing -: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
if (right.size() > 1)
throw new PathEngineException("Error performing -: right operand has more than one value", expr.getStart(), expr.toString());
if (!right.get(0).isPrimitive() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("Quantity")))
throw new PathEngineException(String.format("Error performing -: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("integer") && r.hasType("integer"))
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) - Integer.parseInt(r.primitiveValue())));
else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer"))
result.add(new DecimalType(new BigDecimal(l.primitiveValue()).subtract(new BigDecimal(r.primitiveValue()))));
else if (l.isDateTime() && r.hasType("Quantity"))
result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, true, expr));
else
throw new PathEngineException(String.format("Error performing -: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()), expr.getStart(), expr.toString());
return result;
}
use of org.hl7.fhir.r4.model.IntegerType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opDiv.
private List<Base> opDiv(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 new PathEngineException("Error performing div: left operand has more than one value", expr.getStart(), expr.toString());
if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity))
throw new PathEngineException(String.format("Error performing div: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
if (right.size() > 1)
throw new PathEngineException("Error performing div: right operand has more than one value", expr.getStart(), expr.toString());
if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity))
throw new PathEngineException(String.format("Error performing div: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("integer") && r.hasType("integer"))
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) / Integer.parseInt(r.primitiveValue())));
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 IntegerType(d1.divInt(d2).asDecimal()));
} catch (UcumException e) {
throw new PathEngineException(e);
}
} else
throw new PathEngineException(String.format("Error performing div: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()), expr.getStart(), expr.toString());
return result;
}
use of org.hl7.fhir.r4.model.IntegerType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opPlus.
private List<Base> opPlus(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 new PathEngineException("Error performing +: left operand has more than one value", expr.getStart(), expr.toString());
if (!left.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
if (right.size() > 1)
throw new PathEngineException("Error performing +: right operand has more than one value", expr.getStart(), expr.toString());
if (!right.get(0).isPrimitive() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("Quantity")))
throw new PathEngineException(String.format("Error performing +: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType(FHIR_TYPES_STRING) && r.hasType(FHIR_TYPES_STRING))
result.add(new StringType(l.primitiveValue() + r.primitiveValue()));
else if (l.hasType("integer") && r.hasType("integer"))
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) + Integer.parseInt(r.primitiveValue())));
else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer"))
result.add(new DecimalType(new BigDecimal(l.primitiveValue()).add(new BigDecimal(r.primitiveValue()))));
else if (l.isDateTime() && r.hasType("Quantity"))
result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, false, expr));
else
throw new PathEngineException(String.format("Error performing +: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()), expr.getStart(), expr.toString());
return result;
}
use of org.hl7.fhir.r4.model.IntegerType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opTimes.
private List<Base> opTimes(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 new PathEngineException("Error performing *: left operand has more than one value", expr.getStart(), expr.toString());
if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity))
throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
if (right.size() > 1)
throw new PathEngineException("Error performing *: right operand has more than one value", expr.getStart(), expr.toString());
if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity))
throw new PathEngineException(String.format("Error performing *: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("integer") && r.hasType("integer"))
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) * Integer.parseInt(r.primitiveValue())));
else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer"))
result.add(new DecimalType(new BigDecimal(l.primitiveValue()).multiply(new BigDecimal(r.primitiveValue()))));
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().multiply(pl, pr);
result.add(pairToQty(p));
} catch (UcumException e) {
throw new PathEngineException(e.getMessage(), expr.getOpStart(), expr.toString(), e);
}
} else
throw new PathEngineException(String.format("Error performing *: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()), expr.getStart(), expr.toString());
return result;
}
Aggregations