1 package ca.uhn.fhir.model.primitive;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
40
41 public UriDt() {
42
43 }
44
45
46
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
100
101
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
146
147
148
149
150
151
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 }