代码之家  ›  专栏  ›  技术社区  ›  granadaCoder

向IAnyResource添加“标记”

  •  0
  • granadaCoder  · 技术社区  · 5 年前

    如何向IAnyResource添加“标记”?

    import org.hl7.fhir.instance.model.api.IAnyResource;
    import org.hl7.fhir.instance.model.api.IBaseCoding;
    import org.hl7.fhir.r4.model.Coding;
    
    import java.util.ArrayList;
    import java.util.List;
    
        public IAnyResource tagAnIAnyResource(IAnyResource anyRes) {
            IAnyResource returnItem = null;
    
            if (null != anyRes) {
                returnItem = anyRes;
    
    
                List<? extends IBaseCoding> temp = anyRes.getMeta().getTag();
                String tempReport = temp.getClass().getSimpleName();
    
                List<IBaseCoding> tagList = new ArrayList<>();
    
                IBaseCoding dogA = new Coding().setSystem(null).setCode("Dog").setDisplay("Puppies");
    
                /* below does not work :< */
                anyRes.getMeta().getTag().add(dogA);
    
    
                tagList.add(dogA);
                // Add this twice
                tagList.add(new Coding().setSystem("http://foo").setCode("Cat").setDisplay("Kittens"));
                tagList.add(new Coding().setSystem("http://foo").setCode("Cat").setDisplay("Kittens"));
    
    
    
                /* below does not work :< */
                anyRes.getMeta().getTag().addAll(tagList);
                
                /* out of desperation */
                List<? extends IBaseCoding> castList = (List<? extends IBaseCoding>) tagList;
                /* below does not work :< */
                anyRes.getMeta().getTag().addAll(castList);
    
            }
    
            return returnItem;
    
        }
    

    Error:(35, 43) java: incompatible types: org.hl7.fhir.instance.model.api.IBaseCoding cannot be converted to capture#1 of ? extends org.hl7.fhir.instance.model.api.IBaseCoding
    
    Error:(46, 46) java: incompatible types: java.util.List<org.hl7.fhir.instance.model.api.IBaseCoding> cannot be converted to java.util.Collection<? extends capture#2 of ? extends org.hl7.fhir.instance.model.api.IBaseCoding>
    
    Error:(51, 46) java: incompatible types: java.util.List<capture#3 of ? extends org.hl7.fhir.instance.model.api.IBaseCoding> cannot be converted to java.util.Collection<? extends capture#4 of ? extends org.hl7.fhir.instance.model.api.IBaseCoding>
    

    代码样本(编码为r4患者)主要来自

    https://github.com/jamesagnew/hapi-fhir/blob/master/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4Test.java#L3652L3657

    /**
     * An IBaseResource that has a FHIR version of DSTU3 or higher
     */
    public interface IAnyResource extends IBaseResource {
    

    或者这是某种java类型的擦除问题?

    0 回复  |  直到 5 年前
        1
  •  0
  •   granadaCoder    5 年前

    似乎是Java类型的擦除巫毒。

    见下文。

    如果对differentBaseObjectHolder的三个版本进行注释/取消注释,则可以看到问题。

            /*
            * 
            * import org.hl7.fhir.instance.model.api.IAnyResource;
            * import org.hl7.fhir.instance.model.api.IBaseCoding;
            * import org.hl7.fhir.r4.model.Coding;
            * import org.hl7.fhir.r4.model.DomainResource;
            * import org.hl7.fhir.r4.model.Identifier;
            * import org.hl7.fhir.r4.model.Patient;
            * 
            * */
    
            org.hl7.fhir.r4.model.Patient patOne = new org.hl7.fhir.r4.model.Patient();
            List<Identifier> identifierList = new ArrayList<>();
            Identifier identifier = new Identifier();
            identifier.setSystem("MySystem1");
            identifier.setValue("MyValue1");
            identifierList.add(identifier);
            patOne.setIdentifier(identifierList);
    
    
            //below works fine
            org.hl7.fhir.r4.model.Patient differentBaseObjectHolder = patOne;
    
            //below works fine
            //org.hl7.fhir.r4.model.DomainResource differentBaseObjectHolder = (DomainResource) patOne;
    
            //Java Type Erasure Generics kick in.  Does not work.  Java Generics are horrible
            //org.hl7.fhir.instance.model.api.IAnyResource differentBaseObjectHolder = patOne;
    
            List<? extends IBaseCoding> temp = differentBaseObjectHolder.getMeta().getTag();
            String tempReport = temp.getClass().getSimpleName();
    
            List<Coding> tagList = new ArrayList<>();
    
            Coding dogA = new Coding().setSystem(null).setCode("Dog").setDisplay("Puppies");
    
            differentBaseObjectHolder.getMeta().getTag().add(dogA);
    
            tagList.add(dogA);
            // Add this twice
            tagList.add(new Coding().setSystem("http://foo").setCode("Cat").setDisplay("Kittens"));
            tagList.add(new Coding().setSystem("http://foo").setCode("Cat").setDisplay("Kittens"));
    
            differentBaseObjectHolder.getMeta().getTag().addAll(tagList);
    
            List<? extends Coding> castList = (List<? extends Coding>) tagList;
            /* below does not work :< */
            differentBaseObjectHolder.getMeta().getTag().addAll(castList);
    

    =====

    hapiFhirVersion = '5.2.0'
    

    implementation group: 'ca.uhn.hapi.fhir', name: 'hapi-fhir-structures-r4', version: hapiFhirVersion
    

    }

    推荐文章