001package ca.uhn.fhir.rest.param;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import ca.uhn.fhir.model.base.composite.BaseCodingDt;
007import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
008import ca.uhn.fhir.util.CoverageIgnore;
009
010/*
011 * #%L
012 * HAPI FHIR - Core Library
013 * %%
014 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
015 * %%
016 * Licensed under the Apache License, Version 2.0 (the "License");
017 * you may not use this file except in compliance with the License.
018 * You may obtain a copy of the License at
019 *
020 *      http://www.apache.org/licenses/LICENSE-2.0
021 *
022 * Unless required by applicable law or agreed to in writing, software
023 * distributed under the License is distributed on an "AS IS" BASIS,
024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
025 * See the License for the specific language governing permissions and
026 * limitations under the License.
027 * #L%
028 */
029
030/**
031 * This class represents a restful search operation parameter for an "OR list" of tokens (in other words, a 
032 * list which can contain one-or-more tokens, where the server should return results matching any of the tokens)
033 */
034public class TokenOrListParam extends BaseOrListParam<TokenOrListParam, TokenParam> {
035
036        /**
037         * Create a new empty token "OR list"
038         */
039        public TokenOrListParam() {
040        }
041
042        /**
043         * Create a new token "OR list" with a single token, or multiple tokens which have the same system value
044         * 
045         * @param theSystem
046         *            The system to use for the one token to pre-populate in this list
047         * @param theValues
048         *            The values to use for the one token to pre-populate in this list
049         */
050        public TokenOrListParam(String theSystem, String... theValues) {
051                for (String next : theValues) {
052                        add(theSystem, next);
053                }
054        }
055
056        /**
057         * Convenience method which adds a token to this OR list using the system and code from a coding
058         */
059        public void add(BaseCodingDt theCodingDt) {
060                add(new TokenParam(theCodingDt));
061        }
062
063        /**
064         * Convenience method which adds a token to this OR list using the system and value from an identifier
065         */
066        public void add(BaseIdentifierDt theIdentifierDt) {
067                add(new TokenParam(theIdentifierDt));
068        }
069
070        /**
071         * Add a new token to this list
072         *  @param theSystem
073         *            The system to use for the one token to pre-populate in this list
074         */
075        public TokenOrListParam add(String theSystem, String theValue) {
076                add(new TokenParam(theSystem, theValue));
077                return this;
078        }
079
080        /**
081         * Add a new token to this list
082         */
083        public TokenOrListParam add(String theValue) {
084                add(new TokenParam(null, theValue));
085                return this;
086        }
087
088        public List<BaseCodingDt> getListAsCodings() {
089                ArrayList<BaseCodingDt> retVal = new ArrayList<BaseCodingDt>();
090                for (TokenParam next : getValuesAsQueryTokens()) {
091                        InternalCodingDt nextCoding = next.getValueAsCoding();
092                        if (!nextCoding.isEmpty()) {
093                                retVal.add(nextCoding);
094                        }
095                }
096                return retVal;
097        }
098
099        @CoverageIgnore
100        @Override
101        TokenParam newInstance() {
102                return new TokenParam();
103        }
104
105        public boolean doesCodingListMatch(List<? extends BaseCodingDt> theCodings) {
106                List<BaseCodingDt> paramCodings = getListAsCodings();
107                for (BaseCodingDt coding : theCodings) {
108                        for (BaseCodingDt paramCoding : paramCodings) {
109                                if (coding.matchesToken(paramCoding)) {
110                                        return true;
111                                }
112                        }
113                }
114                return false;
115        }
116        
117        @CoverageIgnore
118        @Override
119        public TokenOrListParam addOr(TokenParam theParameter) {
120                add(theParameter);
121                return this;
122        }
123
124}