001package ca.uhn.fhir.model.api;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 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 ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.model.primitive.IdDt;
025import ca.uhn.fhir.parser.DataFormatException;
026import ca.uhn.fhir.util.CoverageIgnore;
027
028public abstract class BaseIdentifiableElement extends BaseElement implements IIdentifiableElement {
029
030        private static final long serialVersionUID = -7816838417076777914L;
031        private String myElementSpecificId;
032
033        @Override
034        public String getElementSpecificId() {
035                return myElementSpecificId;
036        }
037
038        /**
039         * @deprecated Use {@link #getElementSpecificId()} instead. This method will be removed because it is easily
040         *             confused with other ID methods (such as patient#getIdentifier)
041         */
042        @CoverageIgnore
043        @Deprecated
044        @Override
045        public IdDt getId() {
046                if (myElementSpecificId == null) {
047                        return new LockedId();
048                }
049                return new LockedId(myElementSpecificId);
050        }
051
052        @Override
053        public void setElementSpecificId(String theElementSpecificId) {
054                myElementSpecificId = theElementSpecificId;
055        }
056
057        /**
058         * @deprecated Use {@link #setElementSpecificId(String)} instead. This method will be removed because it is easily
059         *             confused with other ID methods (such as patient#getIdentifier)
060         */
061        @CoverageIgnore
062        @Deprecated
063        @Override
064        public void setId(IdDt theId) {
065                if (theId == null) {
066                        myElementSpecificId = null;
067                } else {
068                        myElementSpecificId = theId.getValue();
069                }
070        }
071
072        /**
073         * @deprecated Use {@link #setElementSpecificId(String)} instead. This method will be removed because it is easily
074         *             confused with other ID methods (such as patient#getIdentifier)
075         */
076        @CoverageIgnore
077        @Override
078        @Deprecated
079        public void setId(String theId) {
080                myElementSpecificId = theId;
081        }
082
083        @CoverageIgnore
084        private static class LockedId extends IdDt {
085
086                @CoverageIgnore
087                public LockedId() {
088                }
089
090                @CoverageIgnore
091                public LockedId(String theElementSpecificId) {
092                        super(theElementSpecificId);
093                }
094
095                @Override
096                @CoverageIgnore
097                public IdDt setValue(String theValue) throws DataFormatException {
098                        throw new UnsupportedOperationException(Msg.code(1899) + "Use IElement#setElementSpecificId(String) to set the element ID for an element");
099                }
100
101                @Override
102                @CoverageIgnore
103                public void setValueAsString(String theValue) throws DataFormatException {
104                        throw new UnsupportedOperationException(Msg.code(1900) + "Use IElement#setElementSpecificId(String) to set the element ID for an element");
105                }
106
107        }
108
109}