View Javadoc
1   package ca.uhn.fhir.model.primitive;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 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  
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  
26  import org.apache.commons.lang3.StringUtils;
27  
28  import ca.uhn.fhir.model.api.BasePrimitive;
29  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
30  import ca.uhn.fhir.model.api.annotation.SimpleSetter;
31  import ca.uhn.fhir.parser.DataFormatException;
32  
33  @DatatypeDef(name = "uri")
34  public class UriDt extends BasePrimitive<URI> {
35  
36  	private URI myValue;
37  
38  	/**
39  	 * Create a new String
40  	 */
41  	public UriDt() {
42  		// nothing
43  	}
44  
45  	/**
46  	 * Create a new String
47  	 */
48  	@SimpleSetter
49  	public UriDt(@SimpleSetter.Parameter(name = "theUri") String theValue) {
50  		setValueAsString(theValue);
51  	}
52  
53  	@Override
54  	public URI getValue() {
55  		return myValue;
56  	}
57  
58  	@Override
59  	public void setValue(URI theValue) {
60  		myValue = theValue;
61  	}
62  
63  	@Override
64  	public void setValueAsString(String theValue) throws DataFormatException {
65  		if (theValue == null) {
66  			myValue = null;
67  		} else {
68  			try {
69  				myValue = new URI(theValue);
70  			} catch (URISyntaxException e) {
71  				throw new DataFormatException("Unable to parse URI value", e);
72  			}
73  		}
74  	}
75  
76  	@Override
77  	public String getValueAsString() {
78  		if (myValue == null) {
79  			return null;
80  		} else {
81  			return myValue.toASCIIString();
82  		}
83  	}
84  
85  	@Override
86  	public String toString() {
87  		return getValueAsString();
88  	}
89  
90  	@Override
91  	public int hashCode() {
92  		final int prime = 31;
93  		int result = 1;
94  		result = prime * result + ((myValue == null) ? 0 : myValue.hashCode());
95  		return result;
96  	}
97  
98  	/**
99  	 * Compares the given string to the string representation of this URI. In many cases it is preferable to use this
100 	 * instead of the standard {@link #equals(Object)} method, since that method returns <code>false</code> unless it is
101 	 * passed an instance of {@link UriDt}
102 	 */
103 	public boolean equals(String theString) {
104 		return StringUtils.equals(getValueAsString(), theString);
105 	}
106 
107 	@Override
108 	public boolean equals(Object obj) {
109 		if (this == obj)
110 			return true;
111 		if (obj == null)
112 			return false;
113 		if (getClass() != obj.getClass())
114 			return false;
115 
116 		UriDt other = (UriDt) obj;
117 		if (myValue == null && other.myValue == null) {
118 			return true;
119 		}
120 		if (myValue == null || other.myValue == null) {
121 			return false;
122 		}
123 
124 		URI normalize = normalize(myValue);
125 		URI normalize2 = normalize(other.myValue);
126 		return normalize.equals(normalize2);
127 	}
128 
129 	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(UriDt.class);
130 
131 	private URI normalize(URI theValue) {
132 		URI retVal = (theValue.normalize());
133 		String urlString = retVal.toString();
134 		if (urlString.endsWith("/") && urlString.length() > 1) {
135 			try {
136 				retVal = new URI(urlString.substring(0, urlString.length() - 1));
137 			} catch (URISyntaxException e) {
138 				ourLog.debug("Failed to normalize URL '{}', message was: {}", urlString, e.toString());
139 			}
140 		}
141 		return retVal;
142 	}
143 
144 	/**
145 	 * Creates a new UriDt instance which uses the given OID as the content (and prepends "urn:oid:" to the OID string
146 	 * in the value of the newly created UriDt, per the FHIR specification).
147 	 * 
148 	 * @param theOid
149 	 *            The OID to use (<code>null</code> is acceptable and will result in a UriDt instance with a
150 	 *            <code>null</code> value)
151 	 * @return A new UriDt instance
152 	 */
153 	public static UriDt fromOid(String theOid) {
154 		if (theOid == null) {
155 			return new UriDt();
156 		}
157 		return new UriDt("urn:oid:" + theOid);
158 	}
159 
160 }