View Javadoc
1   package ca.uhn.fhir.rest.param;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import ca.uhn.fhir.model.base.composite.BaseCodingDt;
7   import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
8   import ca.uhn.fhir.util.CoverageIgnore;
9   
10  /*
11   * #%L
12   * HAPI FHIR - Core Library
13   * %%
14   * Copyright (C) 2014 - 2018 University Health Network
15   * %%
16   * Licensed under the Apache License, Version 2.0 (the "License");
17   * you may not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   * 
20   *      http://www.apache.org/licenses/LICENSE-2.0
21   * 
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an "AS IS" BASIS,
24   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License.
27   * #L%
28   */
29  
30  /**
31   * This class represents a restful search operation parameter for an "OR list" of tokens (in other words, a 
32   * list which can contain one-or-more tokens, where the server should return results matching any of the tokens)
33   */
34  public class TokenOrListParam extends BaseOrListParam<TokenOrListParam, TokenParam> {
35  
36  	/**
37  	 * Create a new empty token "OR list"
38  	 */
39  	public TokenOrListParam() {
40  	}
41  
42  	/**
43  	 * Create a new token "OR list" with a single token, or multiple tokens which have the same system value
44  	 * 
45  	 * @param theSystem
46  	 *            The system to use for the one token to pre-populate in this list
47  	 * @param theValues
48  	 *            The values to use for the one token to pre-populate in this list
49  	 */
50  	public TokenOrListParam(String theSystem, String... theValues) {
51  		for (String next : theValues) {
52  			add(theSystem, next);
53  		}
54  	}
55  
56  	/**
57  	 * Convenience method which adds a token to this OR list using the system and code from a coding
58  	 */
59  	public void add(BaseCodingDt theCodingDt) {
60  		add(new TokenParam(theCodingDt));
61  	}
62  
63  	/**
64  	 * Convenience method which adds a token to this OR list using the system and value from an identifier
65  	 */
66  	public void add(BaseIdentifierDt theIdentifierDt) {
67  		add(new TokenParam(theIdentifierDt));
68  	}
69  
70  	/**
71  	 * Add a new token to this list
72  	 *  @param theSystem
73  	 *            The system to use for the one token to pre-populate in this list
74  	 */
75  	public TokenOrListParam add(String theSystem, String theValue) {
76  		add(new TokenParam(theSystem, theValue));
77  		return this;
78  	}
79  
80  	public List<BaseCodingDt> getListAsCodings() {
81  		ArrayList<BaseCodingDt> retVal = new ArrayList<BaseCodingDt>();
82  		for (TokenParam next : getValuesAsQueryTokens()) {
83  			InternalCodingDt nextCoding = next.getValueAsCoding();
84  			if (!nextCoding.isEmpty()) {
85  				retVal.add(nextCoding);
86  			}
87  		}
88  		return retVal;
89  	}
90  
91  	@CoverageIgnore
92  	@Override
93  	TokenParam newInstance() {
94  		return new TokenParam();
95  	}
96  
97  	public boolean doesCodingListMatch(List<? extends BaseCodingDt> theCodings) {
98  		List<BaseCodingDt> paramCodings = getListAsCodings();
99  		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 }