001package ca.uhn.fhir.model.primitive;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
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
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.i18n.Msg;
025import ca.uhn.fhir.model.api.BasePrimitive;
026import ca.uhn.fhir.model.api.IQueryParameterType;
027import ca.uhn.fhir.model.api.annotation.DatatypeDef;
028import ca.uhn.fhir.model.api.annotation.SimpleSetter;
029import ca.uhn.fhir.rest.param.StringParam;
030import org.apache.commons.lang3.StringUtils;
031
032@DatatypeDef(name = "string")
033public class StringDt extends BasePrimitive<String> implements IQueryParameterType {
034
035        /**
036         * Create a new String
037         */
038        public StringDt() {
039                super();
040        }
041
042        /**
043         * Create a new String
044         */
045        @SimpleSetter
046        public StringDt(@SimpleSetter.Parameter(name = "theString") String theValue) {
047                setValue(theValue);
048        }
049
050        public String getValueNotNull() {
051                return StringUtils.defaultString(getValue());
052        }
053
054        /**
055         * Returns the value of this string, or <code>null</code>
056         */
057        @Override
058        public String toString() {
059                return getValue();
060        }
061
062        @Override
063        public int hashCode() {
064                final int prime = 31;
065                int result = 1;
066                result = prime * result + ((getValue() == null) ? 0 : getValue().hashCode());
067                return result;
068        }
069
070        @Override
071        public boolean equals(Object obj) {
072                if (this == obj)
073                        return true;
074                if (obj == null)
075                        return false;
076                if (getClass() != obj.getClass())
077                        return false;
078                StringDt other = (StringDt) obj;
079                if (getValue() == null) {
080                        if (other.getValue() != null)
081                                return false;
082                } else if (!getValue().equals(other.getValue()))
083                        return false;
084                return true;
085        }
086
087        /**
088         * {@inheritDoc}
089         */
090        @Override
091        public void setValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
092                setValue(theValue);
093        }
094
095        /**
096         * {@inheritDoc}
097         */
098        @Override
099        public String getValueAsQueryToken(FhirContext theContext) {
100                return getValue();
101        }
102
103        /**
104         * Returns <code>true</code> if this datatype has no extensions, and has either a <code>null</code> value or an empty ("") value.
105         */
106        @Override
107        public boolean isEmpty() {
108                boolean retVal = super.isBaseEmpty() && StringUtils.isBlank(getValue());
109                return retVal;
110        }
111
112        @Override
113        public String getQueryParameterQualifier() {
114                return null;
115        }
116
117        @Override
118        protected String parse(String theValue) {
119                return theValue;
120        }
121
122        @Override
123        protected String encode(String theValue) {
124                return theValue;
125        }
126
127        /**
128         * <b>Not supported!</b>
129         * 
130         * @deprecated get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you
131         * need this functionality
132         */
133        @Deprecated
134        @Override
135        public Boolean getMissing() {
136                return null;
137        }
138
139        /**
140         * <b>Not supported!</b>
141         * 
142         * @deprecated get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you
143         * need this functionality
144         */
145        @Deprecated
146        @Override
147        public IQueryParameterType setMissing(Boolean theMissing) {
148                throw new UnsupportedOperationException(Msg.code(1874) + "get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you need this functionality");
149        }
150
151}