execute method

  1. @override
List execute(
  1. List results,
  2. Map<String, dynamic> passed
)
override

The iterable, nested function that evaluates the entire FHIRPath expression one object at a time

Implementation

@override
List execute(List results, Map<String, dynamic> passed) {
  final executedBefore = before.execute(results.toList(), passed);
  final executedAfter = after.execute(results.toList(), passed);

  if (executedBefore.isEmpty || executedAfter.isEmpty) {
    return [];
  } else if (executedBefore.length != 1 || executedAfter.length != 1) {
    throw FhirPathEvaluationException(
        'Math Operators require each operand to result in a '
        'single object. The "+" operator was passed the following:\n'
        'Operand 1: $executedBefore\n'
        'Operand 2: $executedAfter',
        operation: '+',
        collection: results);
  } else {
    switch (executedBefore.first.runtimeType) {
      case int:
        {
          if (executedAfter.first is num) {
            return [executedBefore.first + executedAfter.first];
          }
          break;
        }
      case double:
        {
          if (executedAfter.first is num) {
            return [executedBefore.first + executedAfter.first];
          }
          break;
        }
      case ValidatedQuantity:
        {
          if (executedAfter.first is ValidatedQuantity) {
            return [
              (executedBefore.first as ValidatedQuantity) +
                  (executedAfter.first as ValidatedQuantity)
            ];
          } else if (executedAfter.first is FhirDateTimeBase) {
            if ((executedBefore.first as ValidatedQuantity).isDuration) {
              return [
                executedAfter.first +
                    extendedDurationFromValidatedQuantity(
                        executedBefore.first)
              ];
            }
          }
          break;
        }
      case FhirDateTime:
        {
          if (executedAfter.first is ValidatedQuantity &&
              (executedAfter.first as ValidatedQuantity).isDuration) {
            return [
              (executedBefore.first +
                      extendedDurationFromValidatedQuantity(
                          executedAfter.first as ValidatedQuantity))
                  .toString()
            ];
          }
          break;
        }
      case FhirDate:
        {
          if (executedAfter.first is ValidatedQuantity &&
              (executedAfter.first as ValidatedQuantity).isDuration) {
            return [
              (executedBefore.first +
                      extendedDurationFromValidatedQuantity(
                          executedAfter.first as ValidatedQuantity))
                  .toString()
            ];
          }
          break;
        }
      case FhirTime:
        {
          if (executedAfter.first is ValidatedQuantity &&
              (executedAfter.first as ValidatedQuantity).isDuration) {
            return [
              (addToFhirTime(executedBefore.first,
                      (executedAfter.first as ValidatedQuantity)))
                  .toString()
            ];
          }
          break;
        }
      case String:
        {
          if (executedAfter.first is String) {
            return [executedBefore.first + executedAfter.first];
          } else if (executedAfter.first is ValidatedQuantity &&
              (executedAfter.first as ValidatedQuantity).isDuration) {
            if (FhirDateTime(executedBefore.first).isValid) {
              return [
                (FhirDateTime(executedBefore.first) +
                        extendedDurationFromValidatedQuantity(
                            executedAfter.first))
                    .toString()
              ];
            } else if (FhirTime(executedBefore.first).isValid) {
              return [
                addToFhirTime(FhirTime(executedBefore.first),
                        executedAfter.first as ValidatedQuantity)
                    .toString()
              ];
            }
          }
          break;
        }
      default:
        break;
    }
  }
  throw FhirPathEvaluationException(
      'The "+" operator only accepts (FHIR) Integers, '
      'Decimals, Quantities, String or (Dart) int, double, num, '
      'or Strings, but was passed the following:\n'
      'Operand 1: ${executedBefore.first} (${executedBefore.first.runtimeType})\n'
      'Operand 2: ${executedAfter.first} (${executedAfter.first.runtimeType})',
      operation: '+',
      collection: results);
}