001package org.hl7.fhir.r4.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;
038
039
040public class TypesUtilities {
041
042  public enum TypeClassification {
043    PRIMITIVE, DATATYPE, METADATATYPE, SPECIAL;
044
045    public String toDisplay() {
046      switch (this) {
047      case DATATYPE: return "Data Type";
048      case METADATATYPE: return "MetaDataType";
049      case PRIMITIVE: return "Primitive Type";
050      case SPECIAL: return "Special Type";
051      }
052      return "??";
053    }
054  }
055
056  public static class WildcardInformation {
057    private TypeClassification classification;
058    private String typeName;
059    private String comment;
060    public WildcardInformation(String typeName, String comment, TypeClassification classification) {
061      super();
062      this.typeName = typeName;
063      this.comment = comment;
064      this.classification = classification;
065    }
066    public WildcardInformation(String typeName, TypeClassification classification) {
067      super();
068      this.typeName = typeName;
069      this.classification = classification;
070    }
071    public String getTypeName() {
072      return typeName;
073    }
074    public String getComment() {
075      return comment;
076    }
077    public TypeClassification getClassification() {
078      return classification;
079    }
080    
081  }
082  
083  public static List<String> wildcardTypes() {
084    List<String> res = new ArrayList<String>();
085    for (WildcardInformation wi : wildcards())
086      res.add(wi.getTypeName());
087    return res;
088  }
089  
090  // this is the master list for what data types are allowed where the types = *
091  // that this list is incomplete means that the following types cannot have fixed values in a profile:
092  //   Narrative
093  //   Meta
094  //   Any of the IDMP data types
095  // You have to walk into them to profile them.
096  //
097  public static List<WildcardInformation> wildcards() {
098    List<WildcardInformation> res = new ArrayList<WildcardInformation>();
099
100    // primitive types
101    res.add(new WildcardInformation("base64Binary", TypeClassification.PRIMITIVE));
102    res.add(new WildcardInformation("boolean", TypeClassification.PRIMITIVE));
103    res.add(new WildcardInformation("canonical", TypeClassification.PRIMITIVE));
104    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));
105    res.add(new WildcardInformation("date", TypeClassification.PRIMITIVE));
106    res.add(new WildcardInformation("dateTime", TypeClassification.PRIMITIVE));
107    res.add(new WildcardInformation("decimal", TypeClassification.PRIMITIVE));
108    res.add(new WildcardInformation("id", TypeClassification.PRIMITIVE));
109    res.add(new WildcardInformation("instant", TypeClassification.PRIMITIVE));
110    res.add(new WildcardInformation("integer", TypeClassification.PRIMITIVE));
111    res.add(new WildcardInformation("markdown", TypeClassification.PRIMITIVE));
112    res.add(new WildcardInformation("oid", TypeClassification.PRIMITIVE));
113    res.add(new WildcardInformation("positiveInt", TypeClassification.PRIMITIVE));
114    res.add(new WildcardInformation("string", TypeClassification.PRIMITIVE));
115    res.add(new WildcardInformation("time", TypeClassification.PRIMITIVE));
116    res.add(new WildcardInformation("unsignedInt", TypeClassification.PRIMITIVE));
117    res.add(new WildcardInformation("uri", TypeClassification.PRIMITIVE));
118    res.add(new WildcardInformation("url", TypeClassification.PRIMITIVE));
119    res.add(new WildcardInformation("uuid", TypeClassification.PRIMITIVE));
120
121    // Complex general purpose data types
122    res.add(new WildcardInformation("Address", TypeClassification.DATATYPE));
123    res.add(new WildcardInformation("Age", TypeClassification.DATATYPE));
124    res.add(new WildcardInformation("Annotation", TypeClassification.DATATYPE));
125    res.add(new WildcardInformation("Attachment", TypeClassification.DATATYPE));
126    res.add(new WildcardInformation("CodeableConcept", TypeClassification.DATATYPE));
127    res.add(new WildcardInformation("Coding", TypeClassification.DATATYPE));
128    res.add(new WildcardInformation("ContactPoint", TypeClassification.DATATYPE));
129    res.add(new WildcardInformation("Count", TypeClassification.DATATYPE));
130    res.add(new WildcardInformation("Distance", TypeClassification.DATATYPE));
131    res.add(new WildcardInformation("Duration", TypeClassification.DATATYPE));
132    res.add(new WildcardInformation("HumanName", TypeClassification.DATATYPE));
133    res.add(new WildcardInformation("Identifier", TypeClassification.DATATYPE));
134    res.add(new WildcardInformation("Money", TypeClassification.DATATYPE));
135    res.add(new WildcardInformation("Period", TypeClassification.DATATYPE));
136    res.add(new WildcardInformation("Quantity", TypeClassification.DATATYPE));
137    res.add(new WildcardInformation("Range", TypeClassification.DATATYPE));
138    res.add(new WildcardInformation("Ratio", TypeClassification.DATATYPE));
139    res.add(new WildcardInformation("Reference", " - a reference to another resource", TypeClassification.DATATYPE));
140    res.add(new WildcardInformation("SampledData", TypeClassification.DATATYPE));
141    res.add(new WildcardInformation("Signature", TypeClassification.DATATYPE));
142    res.add(new WildcardInformation("Timing", TypeClassification.DATATYPE));
143    
144    // metadata types
145    res.add(new WildcardInformation("ContactDetail", TypeClassification.METADATATYPE));
146    res.add(new WildcardInformation("Contributor", TypeClassification.METADATATYPE));
147    res.add(new WildcardInformation("DataRequirement", TypeClassification.METADATATYPE));
148    res.add(new WildcardInformation("Expression", TypeClassification.METADATATYPE));
149    res.add(new WildcardInformation("ParameterDefinition", TypeClassification.METADATATYPE));
150    res.add(new WildcardInformation("RelatedArtifact", TypeClassification.METADATATYPE));
151    res.add(new WildcardInformation("TriggerDefinition", TypeClassification.METADATATYPE));
152    res.add(new WildcardInformation("UsageContext", TypeClassification.METADATATYPE));
153    
154    // special cases
155    res.add(new WildcardInformation("Dosage", TypeClassification.SPECIAL));
156    res.add(new WildcardInformation("Meta", TypeClassification.SPECIAL));
157    return res;
158  }
159
160  public static boolean isPrimitive(String code) {
161    return Utilities.existsInList(code, "boolean", "integer", "string", "decimal", "uri", "url", "canonical", "base64Binary", "instant", "date", "dateTime", "time", "code", "oid", "id", "markdown", "unsignedInt", "positiveInt", "xhtml");
162  }
163}