private void addCompose(
     String theFilter,
     ValueSet theValueSetToPopulate,
     ValueSet theSourceValueSet,
     CodeSystemConcept theConcept) {
   if (isBlank(theFilter)) {
     addCompose(
         theValueSetToPopulate,
         theSourceValueSet.getCodeSystem().getSystem(),
         theConcept.getCode(),
         theConcept.getDisplay());
   } else {
     String filter = theFilter.toLowerCase();
     if (theConcept.getDisplay().toLowerCase().contains(filter)
         || theConcept.getCode().toLowerCase().contains(filter)) {
       addCompose(
           theValueSetToPopulate,
           theSourceValueSet.getCodeSystem().getSystem(),
           theConcept.getCode(),
           theConcept.getDisplay());
     }
   }
   for (CodeSystemConcept nextChild : theConcept.getConcept()) {
     addCompose(theFilter, theValueSetToPopulate, theSourceValueSet, nextChild);
   }
 }
  @Override
  public ValueSet expand(ValueSet source, String theFilter) {
    ValueSet retVal = new ValueSet();
    retVal.setDate(DateTimeDt.withCurrentTime());

    /*
     * Add composed concepts
     */

    for (ComposeInclude nextInclude : source.getCompose().getInclude()) {
      for (ComposeIncludeConcept next : nextInclude.getConcept()) {
        if (isBlank(theFilter)) {
          addCompose(retVal, nextInclude.getSystem(), next.getCode(), next.getDisplay());
        } else {
          String filter = theFilter.toLowerCase();
          if (next.getDisplay().toLowerCase().contains(filter)
              || next.getCode().toLowerCase().contains(filter)) {
            addCompose(retVal, nextInclude.getSystem(), next.getCode(), next.getDisplay());
          }
        }
      }
    }

    /*
     * Add defined concepts
     */

    for (CodeSystemConcept next : source.getCodeSystem().getConcept()) {
      addCompose(theFilter, retVal, source, next);
    }

    return retVal;
  }
 private void addCompose(ValueSet retVal, String theSystem, String theCode, String theDisplay) {
   if (isBlank(theCode)) {
     return;
   }
   ExpansionContains contains = retVal.getExpansion().addContains();
   contains.setSystem(theSystem);
   contains.setCode(theCode);
   contains.setDisplay(theDisplay);
 }
  @Override
  public ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validateCode(
      UriDt theValueSetIdentifier,
      IIdType theId,
      CodeDt theCode,
      UriDt theSystem,
      StringDt theDisplay,
      CodingDt theCoding,
      CodeableConceptDt theCodeableConcept) {
    List<IIdType> valueSetIds;

    boolean haveCodeableConcept =
        theCodeableConcept != null && theCodeableConcept.getCoding().size() > 0;
    boolean haveCoding = theCoding != null && theCoding.isEmpty() == false;
    boolean haveCode = theCode != null && theCode.isEmpty() == false;

    if (!haveCodeableConcept && !haveCoding && !haveCode) {
      throw new InvalidRequestException("No code, coding, or codeableConcept provided to validate");
    }
    if (!(haveCodeableConcept ^ haveCoding ^ haveCode)) {
      throw new InvalidRequestException(
          "$validate-code can only validate (system AND code) OR (coding) OR (codeableConcept)");
    }

    boolean haveIdentifierParam =
        theValueSetIdentifier != null && theValueSetIdentifier.isEmpty() == false;
    if (theId != null) {
      valueSetIds = Collections.singletonList(theId);
    } else if (haveIdentifierParam) {
      Set<Long> ids =
          searchForIds(
              ValueSet.SP_IDENTIFIER, new TokenParam(null, theValueSetIdentifier.getValue()));
      valueSetIds = new ArrayList<IIdType>();
      for (Long next : ids) {
        valueSetIds.add(new IdDt("ValueSet", next));
      }
    } else {
      if (theCode == null || theCode.isEmpty()) {
        throw new InvalidRequestException(
            "Either ValueSet ID or ValueSet identifier or system and code must be provided. Unable to validate.");
      }
      Set<Long> ids =
          searchForIds(
              ValueSet.SP_CODE, new TokenParam(toStringOrNull(theSystem), theCode.getValue()));
      valueSetIds = new ArrayList<IIdType>();
      for (Long next : ids) {
        valueSetIds.add(new IdDt("ValueSet", next));
      }
    }

    for (IIdType nextId : valueSetIds) {
      ValueSet expansion = expand(nextId, null);
      List<ExpansionContains> contains = expansion.getExpansion().getContains();
      ValidateCodeResult result =
          validateCodeIsInContains(
              contains,
              toStringOrNull(theSystem),
              toStringOrNull(theCode),
              theCoding,
              theCodeableConcept);
      if (result != null) {
        if (theDisplay != null
            && isNotBlank(theDisplay.getValue())
            && isNotBlank(result.getDisplay())) {
          if (!theDisplay.getValue().equals(result.getDisplay())) {
            return new ValidateCodeResult(
                false, "Display for code does not match", result.getDisplay());
          }
        }
        return result;
      }
    }

    return new ValidateCodeResult(false, "Code not found", null);
  }