001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2025 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.rest.param;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.model.api.IQueryParameterType;
024import ca.uhn.fhir.model.primitive.StringDt;
025import ca.uhn.fhir.model.primitive.UriDt;
026import org.apache.commons.lang3.builder.ToStringBuilder;
027import org.apache.commons.lang3.builder.ToStringStyle;
028
029import java.util.Objects;
030
031import static org.apache.commons.lang3.StringUtils.defaultString;
032
033public class UriParam extends BaseParam implements IQueryParameterType {
034
035        private UriParamQualifierEnum myQualifier;
036        private String myValue;
037
038        /**
039         * Constructor
040         */
041        public UriParam() {
042                super();
043        }
044
045        public UriParam(String theValue) {
046                setValue(theValue);
047        }
048
049        @Override
050        String doGetQueryParameterQualifier() {
051                return myQualifier != null ? myQualifier.getValue() : null;
052        }
053
054        @Override
055        String doGetValueAsQueryToken() {
056                return ParameterUtil.escape(myValue);
057        }
058
059        @Override
060        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
061                myQualifier = UriParamQualifierEnum.forValue(theQualifier);
062                myValue = ParameterUtil.unescape(theValue);
063        }
064
065        /**
066         * Gets the qualifier for this param (may be <code>null</code> and generally will be)
067         */
068        public UriParamQualifierEnum getQualifier() {
069                return myQualifier;
070        }
071
072        /**
073         * Sets the qualifier for this param (may be <code>null</code> and generally will be)
074         *
075         * @return Returns a reference to <code>this</code> for easy method chanining
076         */
077        public UriParam setQualifier(UriParamQualifierEnum theQualifier) {
078                myQualifier = theQualifier;
079                return this;
080        }
081
082        public String getValue() {
083                return myValue;
084        }
085
086        /**
087         * Sets the value for this param
088         *
089         * @return Returns a reference to <code>this</code> for easy method chanining
090         */
091        public UriParam setValue(String theValue) {
092                myValue = theValue;
093                return this;
094        }
095
096        public StringDt getValueAsStringDt() {
097                return new StringDt(myValue);
098        }
099
100        public UriDt getValueAsUriDt() {
101                return new UriDt(myValue);
102        }
103
104        public String getValueNotNull() {
105                return defaultString(myValue);
106        }
107
108        @Override
109        public String toString() {
110                ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
111                builder.append("value", getValue());
112                return builder.toString();
113        }
114
115        @Override
116        public boolean equals(Object theO) {
117                if (!(theO instanceof UriParam uriParam)) {
118                        return false;
119                }
120                if (!super.equals(theO)) {
121                        return false;
122                }
123                return myQualifier == uriParam.myQualifier && Objects.equals(myValue, uriParam.myValue);
124        }
125
126        @Override
127        public int hashCode() {
128                return Objects.hash(super.hashCode(), myQualifier, myValue);
129        }
130}