001package ca.uhn.fhir.rest.param;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 University Health Network
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 static org.apache.commons.lang3.StringUtils.isNotBlank;
024
025import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum;
026import ca.uhn.fhir.util.CoverageIgnore;
027
028@SuppressWarnings("deprecation")
029public abstract class BaseParamWithPrefix<T extends BaseParam> extends BaseParam {
030
031        private ParamPrefixEnum myPrefix;
032
033        /**
034         * Constructor
035         */
036        // Default since this is internal
037        BaseParamWithPrefix() {
038                super();
039        }
040
041        /**
042         * Eg. if this is invoked with "gt2012-11-02", sets the prefix to GREATER_THAN and returns "2012-11-02"
043         */
044        String extractPrefixAndReturnRest(String theString) {
045                int offset = 0;
046                while (true) {
047                        if (theString.length() == offset || Character.isDigit(theString.charAt(offset))) {
048                                break;
049                        }
050                        offset++;
051                }
052
053                String prefix = theString.substring(0, offset);
054                myPrefix = ParamPrefixEnum.forValue(prefix);
055                if (myPrefix == null) {
056                        myPrefix = ParamPrefixEnum.forDstu1Value(prefix);
057                }
058
059                return theString.substring(offset);
060        }
061
062        /**
063         * @deprecated Use {@link #getPrefix() instead}
064         */
065        @Deprecated
066        public QuantityCompararatorEnum getComparator() {
067                ParamPrefixEnum prefix = getPrefix();
068                if (prefix == null) {
069                        return null;
070                }
071                
072                return QuantityCompararatorEnum.forCode(prefix.getDstu1Value());
073        }
074
075        /**
076         * Returns the prefix used by this parameter (e.g. "<code>gt</code>", or "<code>eq</code>")
077         */
078        public ParamPrefixEnum getPrefix() {
079                return myPrefix;
080        }
081
082        /**
083         * @deprecated Use {@link #setPrefix(ParamPrefixEnum)} instead
084         */
085        @SuppressWarnings("unchecked")
086        @CoverageIgnore
087        @Deprecated
088        public T setComparator(QuantityCompararatorEnum theComparator) {
089                if (theComparator != null) {
090                        myPrefix = ParamPrefixEnum.forDstu1Value(theComparator.getCode());
091                } else {
092                        myPrefix = null;
093                }
094                return (T) this;
095        }
096
097        /**
098         * @deprecated Use {@link #setPrefix(ParamPrefixEnum)} instead
099         */
100        @SuppressWarnings("unchecked")
101        @CoverageIgnore
102        @Deprecated
103        public T setComparator(String theComparator) {
104                if (isNotBlank(theComparator)) {
105                        myPrefix = ParamPrefixEnum.forDstu1Value(theComparator);
106                } else {
107                        myPrefix = null;
108                }
109                return (T) this;
110        }
111
112        /**
113         * Sets the prefix used by this parameter (e.g. "<code>gt</code>", or "<code>eq</code>")
114         */
115        @SuppressWarnings("unchecked")
116        public T setPrefix(ParamPrefixEnum thePrefix) {
117                myPrefix = thePrefix;
118                return (T) this;
119        }
120
121}