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 - 2018 University Health Network
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        public List<BaseCodingDt> getListAsCodings() {
081                ArrayList<BaseCodingDt> retVal = new ArrayList<BaseCodingDt>();
082                for (TokenParam next : getValuesAsQueryTokens()) {
083                        InternalCodingDt nextCoding = next.getValueAsCoding();
084                        if (!nextCoding.isEmpty()) {
085                                retVal.add(nextCoding);
086                        }
087                }
088                return retVal;
089        }
090
091        @CoverageIgnore
092        @Override
093        TokenParam newInstance() {
094                return new TokenParam();
095        }
096
097        public boolean doesCodingListMatch(List<? extends BaseCodingDt> theCodings) {
098                List<BaseCodingDt> paramCodings = getListAsCodings();
099                for (BaseCodingDt coding : theCodings) {
100                        for (BaseCodingDt paramCoding : paramCodings) {
101                                if (coding.matchesToken(paramCoding)) {
102                                        return true;
103                                }
104                        }
105                }
106                return false;
107        }
108        
109        @CoverageIgnore
110        @Override
111        public TokenOrListParam addOr(TokenParam theParameter) {
112                add(theParameter);
113                return this;
114        }
115
116}