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 java.util.Collections;
026import java.util.HashMap;
027import java.util.Map;
028
029import ca.uhn.fhir.context.FhirContext;
030import ca.uhn.fhir.context.FhirVersionEnum;
031
032/**
033 * Comparator/qualifier for values used in REST params, such as {@link DateParam}, {@link NumberParam}, and
034 * {@link QuantityParam}
035 * 
036 * @since 1.5
037 */
038public enum ParamPrefixEnum {
039
040        /**
041         * Code Value: <b>eq</b>
042         *
043         * The actual value is equal to the given value
044         */
045        APPROXIMATE("~", "ap"),
046
047        /**
048         * Code Value: <b>eb</b>
049         *
050         * The range of the search value does overlap not with the range of the target value, and the range above the search value contains the range of the target value
051         */
052        ENDS_BEFORE("", "eb"),
053
054        /**
055         * Code Value: <b>eq</b>
056         *
057         * The actual value is equal to the given value
058         */
059        EQUAL("", "eq"),
060
061        /**
062         * Code Value: <b>gt</b>
063         *
064         * The actual value is greater than the given value.
065         */
066        GREATERTHAN(">", "gt"),
067
068        /**
069         * Code Value: <b>ge</b>
070         *
071         * The actual value is greater than or equal to the given value.
072         */
073        GREATERTHAN_OR_EQUALS(">=", "ge"),
074
075        /**
076         * Code Value: <b>lt</b>
077         *
078         * The actual value is less than the given value.
079         */
080        LESSTHAN("<", "lt"),
081
082        /**
083         * Code Value: <b>le</b>
084         *
085         * The actual value is less than or equal to the given value.
086         */
087        LESSTHAN_OR_EQUALS("<=", "le"), 
088        
089        /**
090         * Code Value: <b>ne</b>
091         *
092         * The actual value is not equal to the given value
093         */
094        NOT_EQUAL("", "ne"),
095        
096        /**
097         * Code Value: <b>sa</b>
098         *
099         * The range of the search value does not overlap with the range of the target value, and the range below the search value contains the range of the target value
100         */
101        STARTS_AFTER("", "sa");
102        
103        private static final Map<String, ParamPrefixEnum> DSTU1_TO_PREFIX;
104        private static final Map<String, ParamPrefixEnum> VALUE_TO_PREFIX;
105
106        static {
107                HashMap<String, ParamPrefixEnum> valueToPrefix = new HashMap<String, ParamPrefixEnum>();
108                HashMap<String, ParamPrefixEnum> dstu1ToPrefix = new HashMap<String, ParamPrefixEnum>();
109                for (ParamPrefixEnum next : values()) {
110                        valueToPrefix.put(next.getValue(), next);
111                        if (isNotBlank(next.getDstu1Value())) {
112                                dstu1ToPrefix.put(next.getDstu1Value(), next);
113                        }
114                }
115
116                VALUE_TO_PREFIX = Collections.unmodifiableMap(valueToPrefix);
117                DSTU1_TO_PREFIX = Collections.unmodifiableMap(dstu1ToPrefix);
118        }
119
120        private final String myDstu1Value;
121
122        private final String myValue;
123
124        private ParamPrefixEnum(String theDstu1Value, String theValue) {
125                myDstu1Value = theDstu1Value;
126                myValue = theValue;
127        }
128
129        /**
130         * Returns the DSTU1 value, e.g. <code>&lt;</code> or <code>~</code>
131         */
132        public String getDstu1Value() {
133                return myDstu1Value;
134        }
135
136        /**
137         * Returns the DSTU2+ value, e.g. <code>lt</code> or <code>eq</code>
138         */
139        public String getValue() {
140                return myValue;
141        }
142
143        /**
144         * Returns the appropriate value for the given context, based on the
145         * FHIR version supported by the context  
146         */
147        public String getValueForContext(FhirContext theContext) {
148                if (theContext.getVersion().getVersion() == FhirVersionEnum.DSTU1) {
149                        return getDstu1Value();
150                }
151                return getValue();
152        }
153
154        /**
155         * Returns the prefix associated with a given DSTU1 value (e.g. <code>&lt;</code> or <code>~</code>)
156         * 
157         * @param theValue
158         *           e.g. <code>&lt;</code> or <code>~</code>
159         * @return The prefix, or <code>null</code> if no prefix matches the value
160         */
161        public static ParamPrefixEnum forDstu1Value(String theValue) {
162                return DSTU1_TO_PREFIX.get(theValue);
163        }
164
165        /**
166         * Returns the prefix associated with a given DSTU2+ value (e.g. <code>lt</code> or <code>eq</code>)
167         * 
168         * @param theValue
169         *           e.g. <code>&lt;</code> or <code>~</code>
170         * @return The prefix, or <code>null</code> if no prefix matches the value
171         */
172        public static ParamPrefixEnum forValue(String theValue) {
173                return VALUE_TO_PREFIX.get(theValue);
174        }
175}