001package ca.uhn.fhir.model.api; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2016 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; 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 { 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 062 /** 063 * @deprecated Tags will become immutable in a future release, so this constructor should not be used. 064 */ 065 @Deprecated 066 public Tag() { 067 } 068 069 /** 070 * @deprecated There is no reason to create a tag with a term and not a scheme, so this constructor will be removed 071 */ 072 @Deprecated 073 public Tag(String theTerm) { 074 this((String) null, theTerm, null); 075 } 076 077 public Tag(String theScheme, String theTerm) { 078 myScheme = theScheme; 079 myTerm = theTerm; 080 } 081 082 public Tag(String theScheme, String theTerm, String theLabel) { 083 myTerm = theTerm; 084 myLabel = theLabel; 085 myScheme = theScheme; 086 } 087 088 public Tag(URI theScheme, URI theTerm, String theLabel) { 089 if (theScheme != null) { 090 myScheme = theScheme.toASCIIString(); 091 } 092 if (theTerm != null) { 093 myTerm = theTerm.toASCIIString(); 094 } 095 myLabel = theLabel; 096 } 097 098 099 public String getLabel() { 100 return myLabel; 101 } 102 103 public String getScheme() { 104 return myScheme; 105 } 106 107 public String getTerm() { 108 return myTerm; 109 } 110 111 @Override 112 public boolean equals(Object obj) { 113 if (this == obj) 114 return true; 115 if (obj == null) 116 return false; 117 if (getClass() != obj.getClass()) 118 return false; 119 Tag other = (Tag) obj; 120 if (myScheme == null) { 121 if (other.myScheme != null) 122 return false; 123 } else if (!myScheme.equals(other.myScheme)) 124 return false; 125 if (myTerm == null) { 126 if (other.myTerm != null) 127 return false; 128 } else if (!myTerm.equals(other.myTerm)) 129 return false; 130 return true; 131 } 132 133 @Override 134 public int hashCode() { 135 final int prime = 31; 136 int result = 1; 137 result = prime * result + ((myScheme == null) ? 0 : myScheme.hashCode()); 138 result = prime * result + ((myTerm == null) ? 0 : myTerm.hashCode()); 139 return result; 140 } 141 142 /** 143 * Returns <code>true</code> if either scheme or term is populated. 144 */ 145 @Override 146 public boolean isEmpty() { 147 return StringUtils.isBlank(myScheme) && StringUtils.isBlank(myTerm); 148 } 149 150 /** 151 * Sets the label and returns a reference to this tag 152 * 153 * @deprecated Tags will become immutable in a future release of HAPI in order to facilitate 154 * ensuring that the TagList acts as an unordered set. Use the constructor to set this field when creating a new 155 * tag object 156 */ 157 @Deprecated 158 public Tag setLabel(String theLabel) { 159 myLabel = theLabel; 160 return this; 161 } 162 163 /** 164 * Sets the scheme and returns a reference to this tag 165 * 166 * @deprecated Tags will become immutable in a future release of HAPI in order to facilitate 167 * ensuring that the TagList acts as an unordered set. Use the constructor to set this field when creating a new 168 * tag object 169 */ 170 @Deprecated 171 public Tag setScheme(String theScheme) { 172 myScheme = theScheme; 173 return this; 174 } 175 176 /** 177 * Sets the term and returns a reference to this tag 178 * 179 * @deprecated Tags will become immutable in a future release of HAPI in order to facilitate 180 * ensuring that the TagList acts as an unordered set. Use the constructor to set this field when creating a new 181 * tag object 182 */ 183 @Deprecated 184 public Tag setTerm(String theTerm) { 185 myTerm = theTerm; 186 return this; 187 } 188 189 public String toHeaderValue() { 190 StringBuilder b = new StringBuilder(); 191 b.append(this.getTerm()); 192 if (isNotBlank(this.getLabel())) { 193 b.append("; label=\"").append(this.getLabel()).append('"'); 194 } 195 if (isNotBlank(this.getScheme())) { 196 b.append("; scheme=\"").append(this.getScheme()).append('"'); 197 } 198 return b.toString(); 199 } 200 201 @Override 202 public String toString() { 203 ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); 204 b.append("Scheme", myScheme); 205 b.append("Term", myTerm); 206 b.append("Label", myLabel); 207 return b.toString(); 208 } 209 210}