As far as I'm concerned, HAPI on FHIR is THE way to go when working with FHIR Resources in Java. However, one of challenges I've had with HAPI is having to write conditional code based on the type of resource I'm working with when I'm handling similar fields, for example setPatient/getPatient/hasPatient or setSubject/getSubject/hasSubject. A number of FHIR Resources have these common setter/getter patterns, and not just with references. Another example is for primitive fields, such as dateTime.
This makes writing automation code somewhat difficult.
I'd love to see some interfaces defined for common 5W patterns that are commonly used on FHIR Resources be introduced that encapsulate the set/get/has methods. These interfaces could be named using the pattern: HasX or HasXList, where X is the field name. Some examples of these follow:
public interface HasSubject {
HasSubject setSubject(Reference subject);
Reference getSubject();
boolean hasSubject();
}
public interface HasOrganizationList {
HasOrganizationList addOrganization(Reference);
Reference addOrganization();
List<Reference> getOrganization();
HasOrganizationList setOrganization(List<Reference> orgList);
Reference getOrganizationFirstRep();
}
This would mean that I could use:
if (x instanceof HasSubject hs) {
hs.setSubject(patient);
} else if (x instanceof HasPatient hp) {
hp.setPatient(patient);
}
Which would make certain automation related code a lot easier.
You might need to make the interfaces use a template parameter for the type to address certain differences in name use, e.g., HasEffectiveTime<DateTimeType> or HasEffectiveTime<InstantType>.
