Tracks
/
FHIR
FHIR
/
Exercises
/
Search a FHIR Bundle (Java)
Search a FHIR Bundle (Java)

Search a FHIR Bundle (Java)

Medium

Instructions

A FHIR Bundle is a container that carries a collection of resources β€” for example the results of a search on a server. This kata is the offline, deterministic version of Batch-1 Day 4, Assignment 1 (searching resources on an OpenMRS FHIR facade): instead of calling a live server, you are handed the Bundle in memory and must implement the search/filter utilities a client would use.

The resource model

A resource is modelled by the immutable helper class BundleSearch.Resource, which is already provided for you:

  • Resource(String type, String id, Map<String,String> fields) β€” construct a resource, e.g. type "Patient", id "1", fields {name: "Smith"}.
  • String type() β€” the resource type, e.g. "Patient".
  • String id() β€” the logical id, e.g. "1".
  • String field(String key) β€” the value of a field, or null if absent.

A Bundle is simply a List<Resource>.

Your task

Implement the four static search methods on BundleSearch:

  • List<Resource> ofType(List<Resource> bundle, String type) β€” every entry whose type equals type, preserving the input order.
  • Optional<Resource> byId(List<Resource> bundle, String type, String id) β€” the first entry matching both type and id, or Optional.empty() if none.
  • List<Resource> where(List<Resource> bundle, String type, String field, String value) β€” every entry of type whose field(field) equals value, in input order.
  • int count(List<Resource> bundle, String type) β€” how many entries have the given type.

Rules

  • Preserve the bundle's input order in every returned list.
  • Matching is exact string equality on type, id, and field values.
  • No match returns an empty list, Optional.empty(), or 0 β€” never null and never an exception.

Source

Batch-1 Day 4 Week 1 Assignment 1 (search resources on an OpenMRS FHIR facade), refactored to an offline FHIR Bundle search kata.
Edit via GitHub The link opens in a new window or tab
FHIR Exercism

Ready to start Search a FHIR Bundle (Java)?

Sign up to Exercism to learn and master FHIR with 9 exercises, and real human mentoring, all for free.