View Javadoc
1   package ca.uhn.fhir.model.api;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  import static org.apache.commons.lang3.StringUtils.isNotBlank;
23  
24  import java.net.URI;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.commons.lang3.builder.ToStringBuilder;
28  import org.apache.commons.lang3.builder.ToStringStyle;
29  import org.hl7.fhir.instance.model.api.IBaseCoding;
30  
31  /**
32   * A single tag
33   * <p>
34   * Note on equality- When computing hashCode or equals values for this class, only the 
35   * {@link #getScheme() scheme} and 
36   * </p>
37   */
38  public class Tag extends BaseElement implements IElement, IBaseCoding {
39  	
40  	private static final long serialVersionUID = 1L;
41  	
42  	public static final String ATTR_LABEL = "label";
43  	public static final String ATTR_SCHEME = "scheme";
44  	public static final String ATTR_TERM = "term";
45  
46  	/**
47  	 * Convenience constant containing the "http://hl7.org/fhir/tag" scheme value
48  	 */
49  	public static final String HL7_ORG_FHIR_TAG = "http://hl7.org/fhir/tag";
50  	/**
51  	 * Convenience constant containing the "http://hl7.org/fhir/tag/profile" scheme value
52  	 */
53  	public static final String HL7_ORG_PROFILE_TAG = "http://hl7.org/fhir/tag/profile";
54  	/**
55  	 * Convenience constant containing the "http://hl7.org/fhir/tag/security" scheme value
56  	 */
57  	public static final String HL7_ORG_SECURITY_TAG = "http://hl7.org/fhir/tag/security";
58  
59  	private String myLabel;
60  	private String myScheme;
61  	private String myTerm;
62  
63  	public Tag() {
64  	}
65  
66  	/**
67  	 * @deprecated There is no reason to create a tag with a term and not a scheme, so this constructor will be removed
68  	 */
69  	@Deprecated
70  	public Tag(String theTerm) {
71  		this((String) null, theTerm, null);
72  	}
73  
74  	public Tag(String theScheme, String theTerm) {
75  		myScheme = theScheme;
76  		myTerm = theTerm;
77  	}
78  
79  	public Tag(String theScheme, String theTerm, String theLabel) {
80  		myTerm = theTerm;
81  		myLabel = theLabel;
82  		myScheme = theScheme;
83  	}
84  
85  	public Tag(URI theScheme, URI theTerm, String theLabel) {
86  		if (theScheme != null) {
87  			myScheme = theScheme.toASCIIString();
88  		}
89  		if (theTerm != null) {
90  			myTerm = theTerm.toASCIIString();
91  		}
92  		myLabel = theLabel;
93  	}
94  
95  
96  	public String getLabel() {
97  		return myLabel;
98  	}
99  
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 }