001/*-
002 * #%L
003 * HAPI FHIR - Master Data Management
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.mdm.util;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.mdm.model.CanonicalEID;
025import ca.uhn.fhir.util.CanonicalIdentifier;
026import org.hl7.fhir.instance.model.api.IBase;
027
028public final class IdentifierUtil {
029
030        private IdentifierUtil() {}
031
032        public static CanonicalIdentifier identifierDtFromIdentifier(IBase theIdentifier) {
033                return ca.uhn.fhir.util.CanonicalIdentifier.fromIdentifier(theIdentifier);
034        }
035
036        /**
037         * Retrieves appropriate FHIR Identifier model instance based on the context version
038         *
039         * @param theFhirContext FHIR context to use for determining the identifier version
040         * @param eid EID to get equivalent FHIR Identifier from
041         * @param <T> Generic Identifier base interface
042         * @return Returns appropriate R5, R4 or DSTU3 Identifier instance
043         */
044        public static <T extends IBase> T toId(FhirContext theFhirContext, CanonicalEID eid) {
045                switch (theFhirContext.getVersion().getVersion()) {
046                        case R5:
047                                return (T) eid.toR5();
048                        case R4:
049                                return (T) eid.toR4();
050                        case DSTU3:
051                                return (T) eid.toDSTU3();
052                }
053                throw new IllegalStateException(Msg.code(1487) + "Unsupported FHIR version "
054                                + theFhirContext.getVersion().getVersion());
055        }
056}