001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 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.model.api;
021
022import org.apache.commons.lang3.StringUtils;
023import org.apache.commons.lang3.builder.ToStringBuilder;
024import org.apache.commons.lang3.builder.ToStringStyle;
025import org.hl7.fhir.instance.model.api.IBaseCoding;
026
027import java.net.URI;
028import java.util.Objects;
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                Tag other = (Tag) obj;
118
119                return
120                        Objects.equals(myScheme, other.myScheme) &&
121                        Objects.equals(myTerm, other.myTerm) &&
122                        Objects.equals(myVersion, other.myVersion) &&
123                        Objects.equals(myUserSelected, other.myUserSelected);
124        }
125
126        @Override
127        public int hashCode() {
128                final int prime = 31;
129                int result = 1;
130                result = prime * result + Objects.hashCode(myScheme);
131                result = prime * result + Objects.hashCode(myTerm);
132                result = prime * result + Objects.hashCode(myVersion);
133                result = prime * result + Objects.hashCode(myUserSelected);
134                return result;
135        }
136
137        /**
138         * Returns <code>true</code> if either scheme or term is populated.
139         */
140        @Override
141        public boolean isEmpty() {
142                return StringUtils.isBlank(myScheme) && StringUtils.isBlank(myTerm);
143        }
144
145        /**
146         * Sets the label and returns a reference to this tag
147         */
148        public Tag setLabel(String theLabel) {
149                myLabel = theLabel;
150                return this;
151        }
152
153        /**
154         * Sets the scheme and returns a reference to this tag
155         */
156        public Tag setScheme(String theScheme) {
157                myScheme = theScheme;
158                return this;
159        }
160
161        /**
162         * Sets the term and returns a reference to this tag
163         */
164        public Tag setTerm(String theTerm) {
165                myTerm = theTerm;
166                return this;
167        }
168
169        @Override
170        public String toString() {
171                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
172                b.append("Scheme", myScheme);
173                b.append("Term", myTerm);
174                b.append("Label", myLabel);
175                b.append("Version", myVersion);
176                b.append("UserSelected", myUserSelected);
177                return b.toString();
178        }
179
180        @Override
181        public String getCode() {
182                return getTerm();
183        }
184
185        @Override
186        public String getDisplay() {
187                return getLabel();
188        }
189
190        @Override
191        public String getSystem() {
192                return getScheme();
193        }
194
195        @Override
196        public IBaseCoding setCode(String theTerm) {
197                setTerm(theTerm);
198                return this;
199        }
200
201        @Override
202        public IBaseCoding setDisplay(String theLabel) {
203                setLabel(theLabel);
204                return this;
205        }
206
207        @Override
208        public IBaseCoding setSystem(String theScheme) {
209                setScheme(theScheme);
210                return this;
211        }
212
213        @Override
214        public String getVersion() { return myVersion; }
215
216        @Override
217        public IBaseCoding setVersion(String theVersion) {
218                myVersion = theVersion;
219                return this;
220        }
221
222        @Override
223        public boolean getUserSelected() { return myUserSelected != null && myUserSelected; }
224
225        public Boolean getUserSelectedBoolean() { return myUserSelected; }
226
227        @Override
228        public IBaseCoding setUserSelected(boolean theUserSelected) {
229                myUserSelected = theUserSelected;
230                return this;
231        }
232
233        public void setUserSelectedBoolean(Boolean theUserSelected) {
234                myUserSelected = theUserSelected;
235        }
236
237}