View Javadoc
1   package ca.uhn.fhir.util;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2019 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import org.hl7.fhir.instance.model.api.IPrimitiveType;
24  
25  import java.util.ArrayList;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Set;
29  
30  public class DatatypeUtil {
31  
32  	/**
33  	 * Convert a list of FHIR String objects to a set of native java Strings
34  	 */
35  	public static Set<String> toStringSet(List<? extends IPrimitiveType<?>> theStringList) {
36  		HashSet<String> retVal = new HashSet<>();
37  		if (theStringList != null) {
38  			for (IPrimitiveType<?> string : theStringList) {
39  				if (string != null && string.getValue() != null) {
40  					retVal.add(string.getValueAsString());
41  				}
42  			}
43  		}
44  		return retVal;
45  	}
46  
47  	/**
48  	 * Joins a list of strings with a single space (' ') between each string
49  	 */
50  	public static String joinStringsSpaceSeparated(List<? extends IPrimitiveType<String>> theStrings) {
51  		StringBuilder b = new StringBuilder();
52  		for (IPrimitiveType<String> next : theStrings) {
53  			if (next.isEmpty()) {
54  				continue;
55  			}
56  			if (b.length() > 0) {
57  				b.append(' ');
58  			}
59  			b.append(next.getValue());
60  		}
61  		return b.toString();
62  	}
63  
64  }