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 - 2017 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         * 
073         * @param theSystem
074         *            The system to use for the one token to pre-populate in this list
075         * @param theValue
076         *            The value to use for the one token to pre-populate in this list
077         */
078        public void add(String theSystem, String theValue) {
079                add(new TokenParam(theSystem, theValue));
080        }
081
082        public List<BaseCodingDt> getListAsCodings() {
083                ArrayList<BaseCodingDt> retVal = new ArrayList<BaseCodingDt>();
084                for (TokenParam next : getValuesAsQueryTokens()) {
085                        InternalCodingDt nextCoding = next.getValueAsCoding();
086                        if (!nextCoding.isEmpty()) {
087                                retVal.add(nextCoding);
088                        }
089                }
090                return retVal;
091        }
092
093        @CoverageIgnore
094        @Override
095        TokenParam newInstance() {
096                return new TokenParam();
097        }
098
099        public boolean doesCodingListMatch(List<? extends BaseCodingDt> theCodings) {
100                List<BaseCodingDt> paramCodings = getListAsCodings();
101                for (BaseCodingDt coding : theCodings) {
102                        for (BaseCodingDt paramCoding : paramCodings) {
103                                if (coding.matchesToken(paramCoding)) {
104                                        return true;
105                                }
106                        }
107                }
108                return false;
109        }
110        
111        @CoverageIgnore
112        @Override
113        public TokenOrListParam addOr(TokenParam theParameter) {
114                add(theParameter);
115                return this;
116        }
117
118}