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