001package ca.uhn.fhir.rest.gclient;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 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.rest.api.Constants;
024import org.hl7.fhir.instance.model.api.IPrimitiveType;
025
026import java.util.Arrays;
027import java.util.List;
028
029/**
030 * 
031 * @author james
032 *
033 */
034public class  StringClientParam extends BaseClientParam implements IParam {
035
036        private final String myParamName;
037
038        public StringClientParam(String theParamName) {
039                myParamName = theParamName;
040        }
041
042        @Override
043        public String getParamName() {
044                return myParamName;
045        }
046
047        /**
048         * The string matches the given value (servers will often, but are not required to) implement this as a left match,
049         * meaning that a value of "smi" would match "smi" and "smith".
050         */
051        public IStringMatch matches() {
052                return new StringMatches();
053        }
054
055        /**
056         * The string matches exactly the given value
057         */
058        public IStringMatch matchesExactly() {
059                return new StringExactly();
060        }
061
062        /**
063         * The string contains given value
064         */
065        public IStringMatch contains() {
066                return new StringContains();
067        }
068
069        public interface IStringMatch {
070
071                /**
072                 * Requests that resources be returned which match the given value
073                 */
074                ICriterion<StringClientParam> value(String theValue);
075
076                /**
077                 * Requests that resources be returned which match ANY of the given values (this is an OR search, not an AND search). Note that to
078                 * specify an AND search, simply add a subsequent {@link IQuery#where(ICriterion) where} criteria with the same
079                 * parameter.
080                 */
081                ICriterion<StringClientParam> values(List<String> theValues);
082
083                /**
084                 * Requests that resources be returned which match the given value
085                 */
086                ICriterion<StringClientParam> value(IPrimitiveType<String> theValue);
087
088                /**
089                 * Requests that resources be returned which match ANY of the given values (this is an OR search, not an AND search). Note that to
090                 * specify an AND search, simply add a subsequent {@link IQuery#where(ICriterion) where} criteria with the same
091                 * parameter.
092                 */
093                ICriterion<?> values(String... theValues);
094
095        }
096
097        private class StringExactly implements IStringMatch {
098                @Override
099                public ICriterion<StringClientParam> value(String theValue) {
100                        return new StringCriterion<>(getParamName() + Constants.PARAMQUALIFIER_STRING_EXACT, theValue);
101                }
102
103                @Override
104                public ICriterion<StringClientParam> value(IPrimitiveType<String> theValue) {
105                        return new StringCriterion<>(getParamName() + Constants.PARAMQUALIFIER_STRING_EXACT, theValue.getValue());
106                }
107
108                @Override
109                public ICriterion<StringClientParam> values(List<String> theValue) {
110                        return new StringCriterion<>(getParamName() + Constants.PARAMQUALIFIER_STRING_EXACT, theValue);
111                }
112
113                @Override
114                public ICriterion<?> values(String... theValues) {
115                        return new StringCriterion<StringClientParam>(getParamName() + Constants.PARAMQUALIFIER_STRING_EXACT, Arrays.asList(theValues));
116                }
117        }
118
119        private class StringContains implements IStringMatch {
120                @Override
121                public ICriterion<StringClientParam> value(String theValue) {
122                        return new StringCriterion<>(getParamName() + Constants.PARAMQUALIFIER_STRING_CONTAINS, theValue);
123                }
124
125                @Override
126                public ICriterion<StringClientParam> value(IPrimitiveType<String> theValue) {
127                        return new StringCriterion<>(getParamName() + Constants.PARAMQUALIFIER_STRING_CONTAINS, theValue.getValue());
128                }
129
130                @Override
131                public ICriterion<StringClientParam> values(List<String> theValue) {
132                        return new StringCriterion<>(getParamName() + Constants.PARAMQUALIFIER_STRING_CONTAINS, theValue);
133                }
134
135                @Override
136                public ICriterion<?> values(String... theValues) {
137                        return new StringCriterion<StringClientParam>(getParamName() + Constants.PARAMQUALIFIER_STRING_CONTAINS, Arrays.asList(theValues));
138                }
139        }
140
141        private class StringMatches implements IStringMatch {
142                @Override
143                public ICriterion<StringClientParam> value(String theValue) {
144                        return new StringCriterion<>(getParamName(), theValue);
145                }
146
147
148                @Override
149                public ICriterion<StringClientParam> value(IPrimitiveType<String> theValue) {
150                        return new StringCriterion<>(getParamName(), theValue.getValue());
151                }
152
153                @Override
154                public ICriterion<StringClientParam> values(List<String> theValue) {
155                        return new StringCriterion<>(getParamName(), theValue);
156                }
157
158                @Override
159                public ICriterion<?> values(String... theValues) {
160                        return new StringCriterion<StringClientParam>(getParamName(), Arrays.asList(theValues));
161                }
162
163        }
164
165}