001package org.hl7.fhir.r4.utils;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r4
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.hl7.fhir.utilities.Utilities;
028
029
030public class TypesUtilities {
031
032  public enum TypeClassification {
033    PRIMITIVE, DATATYPE, METADATATYPE, SPECIAL;
034
035    public String toDisplay() {
036      switch (this) {
037      case DATATYPE: return "Data Type";
038      case METADATATYPE: return "MetaDataType";
039      case PRIMITIVE: return "Primitive Type";
040      case SPECIAL: return "Special Type";
041      }
042      return "??";
043    }
044  }
045
046  public static class WildcardInformation {
047    private TypeClassification classification;
048    private String typeName;
049    private String comment;
050    public WildcardInformation(String typeName, String comment, TypeClassification classification) {
051      super();
052      this.typeName = typeName;
053      this.comment = comment;
054      this.classification = classification;
055    }
056    public WildcardInformation(String typeName, TypeClassification classification) {
057      super();
058      this.typeName = typeName;
059      this.classification = classification;
060    }
061    public String getTypeName() {
062      return typeName;
063    }
064    public String getComment() {
065      return comment;
066    }
067    public TypeClassification getClassification() {
068      return classification;
069    }
070    
071  }
072  
073  public static List<String> wildcardTypes() {
074    List<String> res = new ArrayList<String>();
075    for (WildcardInformation wi : wildcards())
076      res.add(wi.getTypeName());
077    return res;
078  }
079  
080  // this is the master list for what data types are allowed where the types = *
081  // that this list is incomplete means that the following types cannot have fixed values in a profile:
082  //   Narrative
083  //   Meta
084  //   Any of the IDMP data types
085  // You have to walk into them to profile them.
086  //
087  public static List<WildcardInformation> wildcards() {
088    List<WildcardInformation> res = new ArrayList<WildcardInformation>();
089
090    // primitive types
091    res.add(new WildcardInformation("base64Binary", TypeClassification.PRIMITIVE));
092    res.add(new WildcardInformation("boolean", TypeClassification.PRIMITIVE));
093    res.add(new WildcardInformation("canonical", TypeClassification.PRIMITIVE));
094    res.add(new WildcardInformation("code", "(only if the extension definition provides a <a href=\"terminologies.html#code\">fixed</a> binding to a suitable set of codes)", TypeClassification.PRIMITIVE));
095    res.add(new WildcardInformation("date", TypeClassification.PRIMITIVE));
096    res.add(new WildcardInformation("dateTime", TypeClassification.PRIMITIVE));
097    res.add(new WildcardInformation("decimal", TypeClassification.PRIMITIVE));
098    res.add(new WildcardInformation("id", TypeClassification.PRIMITIVE));
099    res.add(new WildcardInformation("instant", TypeClassification.PRIMITIVE));
100    res.add(new WildcardInformation("integer", TypeClassification.PRIMITIVE));
101    res.add(new WildcardInformation("markdown", TypeClassification.PRIMITIVE));
102    res.add(new WildcardInformation("oid", TypeClassification.PRIMITIVE));
103    res.add(new WildcardInformation("positiveInt", TypeClassification.PRIMITIVE));
104    res.add(new WildcardInformation("string", TypeClassification.PRIMITIVE));
105    res.add(new WildcardInformation("time", TypeClassification.PRIMITIVE));
106    res.add(new WildcardInformation("unsignedInt", TypeClassification.PRIMITIVE));
107    res.add(new WildcardInformation("uri", TypeClassification.PRIMITIVE));
108    res.add(new WildcardInformation("url", TypeClassification.PRIMITIVE));
109    res.add(new WildcardInformation("uuid", TypeClassification.PRIMITIVE));
110
111    // Complex general purpose data types
112    res.add(new WildcardInformation("Address", TypeClassification.DATATYPE));
113    res.add(new WildcardInformation("Age", TypeClassification.DATATYPE));
114    res.add(new WildcardInformation("Annotation", TypeClassification.DATATYPE));
115    res.add(new WildcardInformation("Attachment", TypeClassification.DATATYPE));
116    res.add(new WildcardInformation("CodeableConcept", TypeClassification.DATATYPE));
117    res.add(new WildcardInformation("Coding", TypeClassification.DATATYPE));
118    res.add(new WildcardInformation("ContactPoint", TypeClassification.DATATYPE));
119    res.add(new WildcardInformation("Count", TypeClassification.DATATYPE));
120    res.add(new WildcardInformation("Distance", TypeClassification.DATATYPE));
121    res.add(new WildcardInformation("Duration", TypeClassification.DATATYPE));
122    res.add(new WildcardInformation("HumanName", TypeClassification.DATATYPE));
123    res.add(new WildcardInformation("Identifier", TypeClassification.DATATYPE));
124    res.add(new WildcardInformation("Money", TypeClassification.DATATYPE));
125    res.add(new WildcardInformation("Period", TypeClassification.DATATYPE));
126    res.add(new WildcardInformation("Quantity", TypeClassification.DATATYPE));
127    res.add(new WildcardInformation("Range", TypeClassification.DATATYPE));
128    res.add(new WildcardInformation("Ratio", TypeClassification.DATATYPE));
129    res.add(new WildcardInformation("Reference", " - a reference to another resource", TypeClassification.DATATYPE));
130    res.add(new WildcardInformation("SampledData", TypeClassification.DATATYPE));
131    res.add(new WildcardInformation("Signature", TypeClassification.DATATYPE));
132    res.add(new WildcardInformation("Timing", TypeClassification.DATATYPE));
133    
134    // metadata types
135    res.add(new WildcardInformation("ContactDetail", TypeClassification.METADATATYPE));
136    res.add(new WildcardInformation("Contributor", TypeClassification.METADATATYPE));
137    res.add(new WildcardInformation("DataRequirement", TypeClassification.METADATATYPE));
138    res.add(new WildcardInformation("Expression", TypeClassification.METADATATYPE));
139    res.add(new WildcardInformation("ParameterDefinition", TypeClassification.METADATATYPE));
140    res.add(new WildcardInformation("RelatedArtifact", TypeClassification.METADATATYPE));
141    res.add(new WildcardInformation("TriggerDefinition", TypeClassification.METADATATYPE));
142    res.add(new WildcardInformation("UsageContext", TypeClassification.METADATATYPE));
143    
144    // special cases
145    res.add(new WildcardInformation("Dosage", TypeClassification.SPECIAL));
146    res.add(new WildcardInformation("Meta", TypeClassification.SPECIAL));
147    return res;
148  }
149
150  public static boolean isPrimitive(String code) {
151    return Utilities.existsInList(code, "boolean", "integer", "string", "decimal", "uri", "url", "canonical", "base64Binary", "instant", "date", "dateTime", "time", "code", "oid", "id", "markdown", "unsignedInt", "positiveInt", "xhtml");
152  }
153}
154