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