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