use of org.hl7.fhir.dstu3.model.BaseDateTimeType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method dateAdd.
private BaseDateTimeType dateAdd(BaseDateTimeType d, Quantity q, boolean negate, ExpressionNode holder) {
BaseDateTimeType result = (BaseDateTimeType) d.copy();
int value = negate ? 0 - q.getValue().intValue() : q.getValue().intValue();
switch(q.hasCode() ? q.getCode() : q.getUnit()) {
case "years":
case "year":
result.add(Calendar.YEAR, value);
break;
case "a":
throw new PathEngineException(String.format("Error in date arithmetic: attempt to add a definite quantity duration time unit %s", q.getCode()));
case "months":
case "month":
result.add(Calendar.MONTH, value);
break;
case "mo":
throw new PathEngineException(String.format("Error in date arithmetic: attempt to add a definite quantity duration time unit %s", q.getCode()), holder.getOpStart(), holder.toString());
case "weeks":
case "week":
case "wk":
result.add(Calendar.DAY_OF_MONTH, value * 7);
break;
case "days":
case "day":
case "d":
result.add(Calendar.DAY_OF_MONTH, value);
break;
case "hours":
case "hour":
case "h":
result.add(Calendar.HOUR, value);
break;
case "minutes":
case "minute":
case "min":
result.add(Calendar.MINUTE, value);
break;
case "seconds":
case "second":
case "s":
result.add(Calendar.SECOND, value);
break;
case "milliseconds":
case "millisecond":
case "ms":
result.add(Calendar.MILLISECOND, value);
break;
default:
throw new PathEngineException(String.format("Error in date arithmetic: unrecognized time unit %s", q.getCode()));
}
return result;
}
use of org.hl7.fhir.dstu3.model.BaseDateTimeType 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 makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "-");
}
if (!left.get(0).isPrimitive() && !left.get(0).hasType("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() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("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") && 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.hasType("decimal", "integer", "Quantity") && r.hasType("Quantity")) {
String s = l.primitiveValue();
if ("0".equals(s)) {
Quantity qty = (Quantity) r;
result.add(qty.copy().setValue(qty.getValue().abs()));
}
} else if (l.isDateTime() && r.hasType("Quantity")) {
result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, true, expr));
} else {
throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "-", left.get(0).fhirType(), right.get(0).fhirType());
}
return result;
}
use of org.hl7.fhir.dstu3.model.BaseDateTimeType 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 makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "+");
}
if (!left.get(0).isPrimitive()) {
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() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("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(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 makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "+", left.get(0).fhirType(), right.get(0).fhirType());
}
return result;
}
use of org.hl7.fhir.dstu3.model.BaseDateTimeType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method dateAdd.
private BaseDateTimeType dateAdd(BaseDateTimeType d, Quantity q, boolean negate, ExpressionNode holder) {
BaseDateTimeType result = (BaseDateTimeType) d.copy();
int value = negate ? 0 - q.getValue().intValue() : q.getValue().intValue();
switch(q.hasCode() ? q.getCode() : q.getUnit()) {
case "years":
case "year":
result.add(Calendar.YEAR, value);
break;
case "a":
throw new PathEngineException(String.format("Error in date arithmetic: attempt to add a definite quantity duration time unit %s", q.getCode()));
case "months":
case "month":
result.add(Calendar.MONTH, value);
break;
case "mo":
throw new PathEngineException(String.format("Error in date arithmetic: attempt to add a definite quantity duration time unit %s", q.getCode()), holder.getOpStart(), holder.toString());
case "weeks":
case "week":
case "wk":
result.add(Calendar.DAY_OF_MONTH, value * 7);
break;
case "days":
case "day":
case "d":
result.add(Calendar.DAY_OF_MONTH, value);
break;
case "hours":
case "hour":
case "h":
result.add(Calendar.HOUR, value);
break;
case "minutes":
case "minute":
case "min":
result.add(Calendar.MINUTE, value);
break;
case "seconds":
case "second":
case "s":
result.add(Calendar.SECOND, value);
break;
case "milliseconds":
case "millisecond":
case "ms":
result.add(Calendar.MILLISECOND, value);
break;
default:
throw new PathEngineException(String.format("Error in date arithmetic: unrecognized time unit %s", q.getCode()));
}
return result;
}
use of org.hl7.fhir.dstu3.model.BaseDateTimeType 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 makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "-");
}
if (!left.get(0).isPrimitive() && !left.get(0).hasType("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() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("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") && 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.hasType("decimal", "integer", "Quantity") && r.hasType("Quantity")) {
String s = l.primitiveValue();
if ("0".equals(s)) {
Quantity qty = (Quantity) r;
result.add(qty.copy().setValue(qty.getValue().abs()));
}
} else if (l.isDateTime() && r.hasType("Quantity")) {
result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, true, expr));
} else {
throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "-", left.get(0).fhirType(), right.get(0).fhirType());
}
return result;
}
Aggregations