001/*
002Copyright (c) 2011+, HL7, Inc
003All rights reserved.
004
005Redistribution and use in source and binary forms, with or without modification, 
006are permitted provided that the following conditions are met:
007
008 * Redistributions of source code must retain the above copyright notice, this 
009   list of conditions and the following disclaimer.
010 * Redistributions in binary form must reproduce the above copyright notice, 
011   this list of conditions and the following disclaimer in the documentation 
012   and/or other materials provided with the distribution.
013 * Neither the name of HL7 nor the names of its contributors may be used to 
014   endorse or promote products derived from this software without specific 
015   prior written permission.
016
017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
026POSSIBILITY OF SUCH DAMAGE.
027
028*/
029package org.hl7.fhir.r4.model;
030
031import org.apache.commons.lang3.StringUtils;
032import org.hl7.fhir.exceptions.FHIRException;
033import org.hl7.fhir.r4.utils.ToolingExtensions;
034
035import ca.uhn.fhir.model.api.annotation.DatatypeDef;
036
037
038/**
039 * Primitive type "string" in FHIR - any sequence of unicode characters less than 1MB in length
040 */
041@DatatypeDef(name = "string")
042public class StringType extends PrimitiveType<String> {
043
044        private static final long serialVersionUID = 3L;
045
046        /**
047         * Create a new String
048         */
049        public StringType() {
050                super();
051        }
052
053        /**
054         * Create a new String
055         */
056        public StringType(String theValue) {
057                setValue(theValue);
058        }
059
060        /**
061         * Returns the value of this StringType, or an empty string ("") if the
062         * value is <code>null</code>. This method is provided as a convenience to
063         * users of this API.
064         */
065        public String getValueNotNull() {
066                return StringUtils.defaultString(getValue());
067        }
068
069        /**
070         * Returns the value of this string, or <code>null</code> if no value
071         * is present
072         */
073        @Override
074        public String toString() {
075                return getValue();
076        }
077
078        @Override
079        protected String parse(String theValue) {
080                return theValue;
081        }
082
083        @Override
084        protected String encode(String theValue) {
085                return theValue;
086        }
087
088        @Override
089        public StringType copy() {
090                StringType ret = new StringType(getValue());
091    copyValues(ret);
092    return ret;
093        }
094
095        public String fhirType() {
096                return "string";
097        }
098
099  public String getTranslation(String l) throws FHIRException {
100    for (Extension e : getExtension()) {
101      if (e.getUrl().equals(ToolingExtensions.EXT_TRANSLATION)) {
102        String lang = ToolingExtensions.readStringExtension(e, "lang");
103        if (lang.equals(l))
104          return e.getExtensionString("content");
105      }
106    }
107    return null;
108  }
109
110}