001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 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 java.util.ArrayList;
023import java.util.List;
024
025import ca.uhn.fhir.model.base.composite.BaseCodingDt;
026import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
027import ca.uhn.fhir.util.CoverageIgnore;
028
029/**
030 * This class represents a restful search operation parameter for an "OR list" of tokens (in other words, a 
031 * list which can contain one-or-more tokens, where the server should return results matching any of the tokens)
032 */
033public class TokenOrListParam extends BaseOrListParam<TokenOrListParam, TokenParam> {
034
035        /**
036         * Create a new empty token "OR list"
037         */
038        public TokenOrListParam() {
039        }
040
041        /**
042         * Create a new token "OR list" with a single token, or multiple tokens which have the same system value
043         * 
044         * @param theSystem
045         *            The system to use for the one token to pre-populate in this list
046         * @param theValues
047         *            The values to use for the one token to pre-populate in this list
048         */
049        public TokenOrListParam(String theSystem, String... theValues) {
050                for (String next : theValues) {
051                        add(theSystem, next);
052                }
053        }
054
055        /**
056         * Convenience method which adds a token to this OR list using the system and code from a coding
057         */
058        public void add(BaseCodingDt theCodingDt) {
059                add(new TokenParam(theCodingDt));
060        }
061
062        /**
063         * Convenience method which adds a token to this OR list using the system and value from an identifier
064         */
065        public void add(BaseIdentifierDt theIdentifierDt) {
066                add(new TokenParam(theIdentifierDt));
067        }
068
069        /**
070         * Add a new token to this list
071         *  @param theSystem
072         *            The system to use for the one token to pre-populate in this list
073         */
074        public TokenOrListParam add(String theSystem, String theValue) {
075                add(new TokenParam(theSystem, theValue));
076                return this;
077        }
078
079        /**
080         * Add a new token to this list
081         */
082        public TokenOrListParam add(String theValue) {
083                add(new TokenParam(null, theValue));
084                return this;
085        }
086
087        public List<BaseCodingDt> getListAsCodings() {
088                ArrayList<BaseCodingDt> retVal = new ArrayList<BaseCodingDt>();
089                for (TokenParam next : getValuesAsQueryTokens()) {
090                        InternalCodingDt nextCoding = next.getValueAsCoding();
091                        if (!nextCoding.isEmpty()) {
092                                retVal.add(nextCoding);
093                        }
094                }
095                return retVal;
096        }
097
098        @CoverageIgnore
099        @Override
100        TokenParam newInstance() {
101                return new TokenParam();
102        }
103
104        public boolean doesCodingListMatch(List<? extends BaseCodingDt> theCodings) {
105                List<BaseCodingDt> paramCodings = getListAsCodings();
106                for (BaseCodingDt coding : theCodings) {
107                        for (BaseCodingDt paramCoding : paramCodings) {
108                                if (coding.matchesToken(paramCoding)) {
109                                        return true;
110                                }
111                        }
112                }
113                return false;
114        }
115        
116        @CoverageIgnore
117        @Override
118        public TokenOrListParam addOr(TokenParam theParameter) {
119                add(theParameter);
120                return this;
121        }
122
123}