001package ca.uhn.fhir.model.api;
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.apache.commons.lang3.StringUtils;
024import org.apache.commons.lang3.builder.ToStringBuilder;
025import org.apache.commons.lang3.builder.ToStringStyle;
026import org.hl7.fhir.instance.model.api.IBaseCoding;
027
028import java.net.URI;
029
030/**
031 * A single tag
032 * <p>
033 * Note on equality- When computing hashCode or equals values for this class, only the 
034 * {@link #getScheme() scheme} and 
035 * </p>
036 */
037public class Tag extends BaseElement implements IElement, IBaseCoding {
038        
039        private static final long serialVersionUID = 1L;
040        
041        public static final String ATTR_LABEL = "label";
042        public static final String ATTR_SCHEME = "scheme";
043        public static final String ATTR_TERM = "term";
044
045        /**
046         * Convenience constant containing the "http://hl7.org/fhir/tag" scheme value
047         */
048        public static final String HL7_ORG_FHIR_TAG = "http://hl7.org/fhir/tag";
049        /**
050         * Convenience constant containing the "http://hl7.org/fhir/tag/profile" scheme value
051         */
052        public static final String HL7_ORG_PROFILE_TAG = "http://hl7.org/fhir/tag/profile";
053        /**
054         * Convenience constant containing the "http://hl7.org/fhir/tag/security" scheme value
055         */
056        public static final String HL7_ORG_SECURITY_TAG = "http://hl7.org/fhir/tag/security";
057
058        private String myLabel;
059        private String myScheme;
060        private String myTerm;
061        private String myVersion;
062        private boolean myUserSelected;
063
064        public Tag() {
065        }
066
067        /**
068         * @deprecated There is no reason to create a tag with a term and not a scheme, so this constructor will be removed
069         */
070        @Deprecated
071        public Tag(String theTerm) {
072                this((String) null, theTerm, null);
073        }
074
075        public Tag(String theScheme, String theTerm) {
076                myScheme = theScheme;
077                myTerm = theTerm;
078        }
079
080        public Tag(String theScheme, String theTerm, String theLabel) {
081                myTerm = theTerm;
082                myLabel = theLabel;
083                myScheme = theScheme;
084        }
085
086        public Tag(URI theScheme, URI theTerm, String theLabel) {
087                if (theScheme != null) {
088                        myScheme = theScheme.toASCIIString();
089                }
090                if (theTerm != null) {
091                        myTerm = theTerm.toASCIIString();
092                }
093                myLabel = theLabel;
094        }
095
096
097        public String getLabel() {
098                return myLabel;
099        }
100
101        public String getScheme() {
102                return myScheme;
103        }
104
105        public String getTerm() {
106                return myTerm;
107        }
108
109        @Override
110        public boolean equals(Object obj) {
111                if (this == obj)
112                        return true;
113                if (obj == null)
114                        return false;
115                if (getClass() != obj.getClass())
116                        return false;
117
118                Tag other = (Tag) obj;
119                if (myScheme == null) {
120                        if (other.myScheme != null)
121                                return false;
122                } else if (!myScheme.equals(other.myScheme))
123                        return false;
124
125                if (myTerm == null) {
126                        if (other.myTerm != null)
127                                return false;
128                } else if (!myTerm.equals(other.myTerm))
129                        return false;
130
131                if (myVersion == null) {
132                        if (other.getVersion() != null)
133                                return false;
134                } else if (!myVersion.equals(other.getVersion()))
135                        return false;
136
137                if (myUserSelected != other.getUserSelected())
138                        return false;
139
140                return true;
141        }
142
143        @Override
144        public int hashCode() {
145                final int prime = 31;
146                int result = 1;
147                result = prime * result + ((myScheme == null) ? 0 : myScheme.hashCode());
148                result = prime * result + ((myTerm == null) ? 0 : myTerm.hashCode());
149                result = prime * result + ((myVersion == null) ? 0 : myVersion.hashCode());
150                result = prime * result + Boolean.hashCode(myUserSelected);
151                return result;
152        }
153
154        /**
155         * Returns <code>true</code> if either scheme or term is populated.
156         */
157        @Override
158        public boolean isEmpty() {
159                return StringUtils.isBlank(myScheme) && StringUtils.isBlank(myTerm);
160        }
161
162        /**
163         * Sets the label and returns a reference to this tag
164         */
165        public Tag setLabel(String theLabel) {
166                myLabel = theLabel;
167                return this;
168        }
169
170        /**
171         * Sets the scheme and returns a reference to this tag
172         */
173        public Tag setScheme(String theScheme) {
174                myScheme = theScheme;
175                return this;
176        }
177
178        /**
179         * Sets the term and returns a reference to this tag
180         */
181        public Tag setTerm(String theTerm) {
182                myTerm = theTerm;
183                return this;
184        }
185
186        @Override
187        public String toString() {
188                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
189                b.append("Scheme", myScheme);
190                b.append("Term", myTerm);
191                b.append("Label", myLabel);
192                b.append("Version", myVersion);
193                b.append("UserSelected", myUserSelected);
194                return b.toString();
195        }
196
197        @Override
198        public String getCode() {
199                return getTerm();
200        }
201
202        @Override
203        public String getDisplay() {
204                return getLabel();
205        }
206
207        @Override
208        public String getSystem() {
209                return getScheme();
210        }
211
212        @Override
213        public IBaseCoding setCode(String theTerm) {
214                setTerm(theTerm);
215                return this;
216        }
217
218        @Override
219        public IBaseCoding setDisplay(String theLabel) {
220                setLabel(theLabel);
221                return this;
222        }
223
224        @Override
225        public IBaseCoding setSystem(String theScheme) {
226                setScheme(theScheme);
227                return this;
228        }
229
230        @Override
231        public String getVersion() { return myVersion; }
232
233        @Override
234        public IBaseCoding setVersion(String theVersion) {
235                myVersion = theVersion;
236                return this;
237        }
238
239        @Override
240        public boolean getUserSelected() { return myUserSelected; }
241
242        @Override
243        public IBaseCoding setUserSelected(boolean theUserSelected) {
244                myUserSelected = theUserSelected;
245                return this;
246        }
247
248}