001package org.hl7.fhir.r5.utils;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.util.ArrayList;
035import java.util.List;
036
037import org.hl7.fhir.utilities.Utilities;
038import org.hl7.fhir.utilities.VersionUtilities;
039import org.hl7.fhir.utilities.VersionUtilities.VersionURLInfo;
040
041
042public class TypesUtilities {
043
044  public enum TypeClassification {
045    PRIMITIVE, DATATYPE, METADATATYPE, SPECIAL;
046
047    public String toDisplay() {
048      switch (this) {
049      case DATATYPE: return "Data Type";
050      case METADATATYPE: return "MetaDataType";
051      case PRIMITIVE: return "Primitive Type";
052      case SPECIAL: return "Special Type";
053      }
054      return "?tu-class?";
055    }
056  }
057
058  public static class WildcardInformation {
059    private TypeClassification classification;
060    private String typeName;
061    private String comment;
062    public WildcardInformation(String typeName, String comment, TypeClassification classification) {
063      super();
064      this.typeName = typeName;
065      this.comment = comment;
066      this.classification = classification;
067    }
068    public WildcardInformation(String typeName, TypeClassification classification) {
069      super();
070      this.typeName = typeName;
071      this.classification = classification;
072    }
073    public String getTypeName() {
074      return typeName;
075    }
076    public String getComment() {
077      return comment;
078    }
079    public TypeClassification getClassification() {
080      return classification;
081    }
082    
083  }
084  
085  public static List<String> wildcardTypes(String version) {
086    List<String> res = new ArrayList<String>();
087    for (WildcardInformation wi : wildcards(version))
088      res.add(wi.getTypeName());
089    return res;
090  }
091  
092  // this is the master list for what data types are allowed where the types = *
093  // that this list is incomplete means that the following types cannot have fixed values in a profile:
094  //   Narrative
095  //   Meta
096  //   Any of the IDMP data types
097  // You have to walk into them to profile them.
098  //
099  public static List<WildcardInformation> wildcards(String version) {
100    if (version.startsWith("_")) {
101      throw new Error("underscore");
102    }
103    List<WildcardInformation> res = new ArrayList<WildcardInformation>();
104
105    // primitive types
106    res.add(new WildcardInformation("base64Binary", TypeClassification.PRIMITIVE));
107    res.add(new WildcardInformation("boolean", TypeClassification.PRIMITIVE));
108    res.add(new WildcardInformation("canonical", TypeClassification.PRIMITIVE));
109    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));
110    res.add(new WildcardInformation("date", TypeClassification.PRIMITIVE));
111    res.add(new WildcardInformation("dateTime", TypeClassification.PRIMITIVE));
112    res.add(new WildcardInformation("decimal", TypeClassification.PRIMITIVE));
113    res.add(new WildcardInformation("id", TypeClassification.PRIMITIVE));
114    res.add(new WildcardInformation("instant", TypeClassification.PRIMITIVE));
115    res.add(new WildcardInformation("integer", TypeClassification.PRIMITIVE));
116    if (!VersionUtilities.isR4BVer(version)) {
117      res.add(new WildcardInformation("integer64", TypeClassification.PRIMITIVE));
118    }
119    res.add(new WildcardInformation("markdown", TypeClassification.PRIMITIVE));
120    res.add(new WildcardInformation("oid", TypeClassification.PRIMITIVE));
121    res.add(new WildcardInformation("positiveInt", TypeClassification.PRIMITIVE));
122    res.add(new WildcardInformation("string", TypeClassification.PRIMITIVE));
123    res.add(new WildcardInformation("time", TypeClassification.PRIMITIVE));
124    res.add(new WildcardInformation("unsignedInt", TypeClassification.PRIMITIVE));
125    res.add(new WildcardInformation("uri", TypeClassification.PRIMITIVE));
126    res.add(new WildcardInformation("url", TypeClassification.PRIMITIVE));
127    res.add(new WildcardInformation("uuid", TypeClassification.PRIMITIVE));
128
129    // Complex general purpose data types
130    res.add(new WildcardInformation("Address", TypeClassification.DATATYPE));
131    res.add(new WildcardInformation("Age", TypeClassification.DATATYPE));
132    res.add(new WildcardInformation("Annotation", TypeClassification.DATATYPE));
133    res.add(new WildcardInformation("Attachment", TypeClassification.DATATYPE));
134    res.add(new WildcardInformation("CodeableConcept", TypeClassification.DATATYPE));
135    res.add(new WildcardInformation("CodeableReference", TypeClassification.DATATYPE));
136    res.add(new WildcardInformation("Coding", TypeClassification.DATATYPE));
137    res.add(new WildcardInformation("ContactPoint", TypeClassification.DATATYPE));
138    res.add(new WildcardInformation("Count", TypeClassification.DATATYPE));
139    res.add(new WildcardInformation("Distance", TypeClassification.DATATYPE));
140    res.add(new WildcardInformation("Duration", TypeClassification.DATATYPE));
141    res.add(new WildcardInformation("HumanName", TypeClassification.DATATYPE));
142    res.add(new WildcardInformation("Identifier", TypeClassification.DATATYPE));
143    res.add(new WildcardInformation("Money", TypeClassification.DATATYPE));
144    res.add(new WildcardInformation("Period", TypeClassification.DATATYPE));
145    res.add(new WildcardInformation("Quantity", TypeClassification.DATATYPE));
146    res.add(new WildcardInformation("Range", TypeClassification.DATATYPE));
147    res.add(new WildcardInformation("Ratio", TypeClassification.DATATYPE));
148    res.add(new WildcardInformation("RatioRange", TypeClassification.DATATYPE));
149    res.add(new WildcardInformation("Reference", " - a reference to another resource", TypeClassification.DATATYPE));
150    res.add(new WildcardInformation("SampledData", TypeClassification.DATATYPE));
151    res.add(new WildcardInformation("Signature", TypeClassification.DATATYPE));
152    res.add(new WildcardInformation("Timing", TypeClassification.DATATYPE));
153    
154    // metadata types
155    res.add(new WildcardInformation("ContactDetail", TypeClassification.METADATATYPE));
156    res.add(new WildcardInformation("Contributor", TypeClassification.METADATATYPE));
157    res.add(new WildcardInformation("DataRequirement", TypeClassification.METADATATYPE));
158    res.add(new WildcardInformation("Expression", TypeClassification.METADATATYPE));
159    res.add(new WildcardInformation("ParameterDefinition", TypeClassification.METADATATYPE));
160    res.add(new WildcardInformation("RelatedArtifact", TypeClassification.METADATATYPE));
161    res.add(new WildcardInformation("TriggerDefinition", TypeClassification.METADATATYPE));
162    res.add(new WildcardInformation("UsageContext", TypeClassification.METADATATYPE));
163    
164    // special cases
165    res.add(new WildcardInformation("Dosage", TypeClassification.SPECIAL));
166    res.add(new WildcardInformation("Meta", TypeClassification.SPECIAL));
167    return res;
168  }
169
170  public static boolean isPrimitive(String code) {
171    return Utilities.existsInList(code, "boolean", "integer", "integer64", "string", "decimal", "uri", "url", "canonical", "base64Binary", "instant", "date", "dateTime", "time", "code", "oid", "id", "markdown", "unsignedInt", "positiveInt", "xhtml");
172  }
173}