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