001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.hl7.fhir.instance.model.api.IPrimitiveType;
024
025import java.util.ArrayList;
026import java.util.HashSet;
027import java.util.List;
028import java.util.Set;
029
030public class DatatypeUtil {
031
032        /**
033         * Convert a list of FHIR String objects to a set of native java Strings
034         */
035        public static Set<String> toStringSet(List<? extends IPrimitiveType<?>> theStringList) {
036                HashSet<String> retVal = new HashSet<>();
037                if (theStringList != null) {
038                        for (IPrimitiveType<?> string : theStringList) {
039                                if (string != null && string.getValue() != null) {
040                                        retVal.add(string.getValueAsString());
041                                }
042                        }
043                }
044                return retVal;
045        }
046
047        /**
048         * Joins a list of strings with a single space (' ') between each string
049         */
050        public static String joinStringsSpaceSeparated(List<? extends IPrimitiveType<String>> theStrings) {
051                StringBuilder b = new StringBuilder();
052                for (IPrimitiveType<String> next : theStrings) {
053                        if (next.isEmpty()) {
054                                continue;
055                        }
056                        if (b.length() > 0) {
057                                b.append(' ');
058                        }
059                        b.append(next.getValue());
060                }
061                return b.toString();
062        }
063
064}