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