001/*-
002 * #%L
003 * HAPI FHIR Storage api
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 ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.model.api.IElement;
024import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
025import ca.uhn.fhir.model.primitive.StringDt;
026import ca.uhn.fhir.model.primitive.UriDt;
027import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
028import org.apache.commons.lang3.builder.EqualsBuilder;
029import org.apache.commons.lang3.builder.HashCodeBuilder;
030import org.hl7.fhir.instance.model.api.IBase;
031
032import java.util.List;
033
034/**
035 * Version independent identifier
036 */
037public class CanonicalIdentifier extends BaseIdentifierDt {
038        UriDt mySystem;
039        StringDt myValue;
040
041        /**
042         * Constructor
043         */
044        public CanonicalIdentifier() {
045                super();
046        }
047
048        /**
049         * Constructor
050         */
051        public CanonicalIdentifier(String theSystem, String theValue) {
052                setSystem(theSystem);
053                setValue(theValue);
054        }
055
056        @Override
057        public UriDt getSystemElement() {
058                return mySystem;
059        }
060
061        @Override
062        public StringDt getValueElement() {
063                return myValue;
064        }
065
066        @Override
067        public CanonicalIdentifier setSystem(String theUri) {
068                mySystem = new UriDt((theUri));
069                return this;
070        }
071
072        @Override
073        public CanonicalIdentifier setValue(String theString) {
074                myValue = new StringDt(theString);
075                return this;
076        }
077
078        @Override
079        public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) {
080                throw new UnsupportedOperationException(Msg.code(1488));
081        }
082
083        @Override
084        public boolean isEmpty() {
085                if (mySystem != null && !mySystem.isEmpty()) {
086                        return false;
087                }
088                if (myValue != null && !myValue.isEmpty()) {
089                        return false;
090                }
091                return true;
092        }
093
094        @Override
095        public boolean equals(Object theO) {
096                if (this == theO) return true;
097
098                if (theO == null || getClass() != theO.getClass()) return false;
099
100                CanonicalIdentifier that = (CanonicalIdentifier) theO;
101
102                return new EqualsBuilder()
103                                .append(mySystem, that.mySystem)
104                                .append(myValue, that.myValue)
105                                .isEquals();
106        }
107
108        @Override
109        public int hashCode() {
110                return new HashCodeBuilder(17, 37).append(mySystem).append(myValue).toHashCode();
111        }
112
113        public static CanonicalIdentifier fromIdentifier(IBase theIdentifier) {
114                CanonicalIdentifier retval = new CanonicalIdentifier();
115
116                // TODO add other fields like "use" etc
117                if (theIdentifier instanceof org.hl7.fhir.dstu3.model.Identifier) {
118                        org.hl7.fhir.dstu3.model.Identifier ident = (org.hl7.fhir.dstu3.model.Identifier) theIdentifier;
119                        retval.setSystem(ident.getSystem()).setValue(ident.getValue());
120                } else if (theIdentifier instanceof org.hl7.fhir.r4.model.Identifier) {
121                        org.hl7.fhir.r4.model.Identifier ident = (org.hl7.fhir.r4.model.Identifier) theIdentifier;
122                        retval.setSystem(ident.getSystem()).setValue(ident.getValue());
123                } else if (theIdentifier instanceof org.hl7.fhir.r5.model.Identifier) {
124                        org.hl7.fhir.r5.model.Identifier ident = (org.hl7.fhir.r5.model.Identifier) theIdentifier;
125                        retval.setSystem(ident.getSystem()).setValue(ident.getValue());
126                } else {
127                        throw new InternalErrorException(Msg.code(1486) + "Expected 'Identifier' type but was '"
128                                        + theIdentifier.getClass().getName() + "'");
129                }
130                return retval;
131        }
132}