001package ca.uhn.fhir.util; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2023 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.HashSet; 026import java.util.List; 027import java.util.Set; 028 029public class DatatypeUtil { 030 031 /** 032 * Convert a list of FHIR String objects to a set of native java Strings 033 */ 034 public static Set<String> toStringSet(List<? extends IPrimitiveType<?>> theStringList) { 035 HashSet<String> retVal = new HashSet<>(); 036 if (theStringList != null) { 037 for (IPrimitiveType<?> string : theStringList) { 038 if (string != null && string.getValue() != null) { 039 retVal.add(string.getValueAsString()); 040 } 041 } 042 } 043 return retVal; 044 } 045 046 /** 047 * Joins a list of strings with a single space (' ') between each string 048 */ 049 public static String joinStringsSpaceSeparated(List<? extends IPrimitiveType<String>> theStrings) { 050 StringBuilder b = new StringBuilder(); 051 for (IPrimitiveType<String> next : theStrings) { 052 if (next.isEmpty()) { 053 continue; 054 } 055 if (b.length() > 0) { 056 b.append(' '); 057 } 058 b.append(next.getValue()); 059 } 060 return b.toString(); 061 } 062 063 /** 064 * Returns {@link IPrimitiveType#getValueAsString()} if <code>thePrimitiveType</code> is 065 * not null, else returns null. 066 */ 067 public static String toStringValue(IPrimitiveType<?> thePrimitiveType) { 068 return thePrimitiveType != null ? thePrimitiveType.getValueAsString() : null; 069 } 070 071 /** 072 * Returns {@link IPrimitiveType#getValue()} if <code>thePrimitiveType</code> is 073 * not null, else returns null. 074 */ 075 public static Boolean toBooleanValue(IPrimitiveType<Boolean> thePrimitiveType) { 076 return thePrimitiveType != null ? thePrimitiveType.getValue() : null; 077 } 078}