Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SimpleDataType |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | |
3 | * The software in this package is published under the terms of the CPAL v1.0 | |
4 | * license, a copy of which has been included with this distribution in the | |
5 | * LICENSE.txt file. | |
6 | */ | |
7 | package org.mule.transformer.types; | |
8 | ||
9 | import org.mule.api.MuleRuntimeException; | |
10 | import org.mule.api.transformer.DataType; | |
11 | ||
12 | import javax.activation.MimeType; | |
13 | import javax.activation.MimeTypeParseException; | |
14 | ||
15 | import org.apache.commons.beanutils.MethodUtils; | |
16 | ||
17 | /** | |
18 | * A data type that simply wraps a Java type. This type also allows a mime type to be associated | |
19 | * with the Java type. | |
20 | * | |
21 | * @since 3.0 | |
22 | */ | |
23 | public class SimpleDataType<T> implements DataType<T>, Cloneable | |
24 | { | |
25 | protected Class<?> type; | |
26 | 0 | protected String mimeType = ANY_MIME_TYPE; |
27 | protected String encoding; | |
28 | ||
29 | public SimpleDataType(Class<?> type, String mimeType) | |
30 | 0 | { |
31 | 0 | this.type = type; |
32 | 0 | if (mimeType == null) |
33 | { | |
34 | 0 | this.mimeType = ANY_MIME_TYPE; |
35 | } | |
36 | else | |
37 | { | |
38 | try | |
39 | { | |
40 | 0 | MimeType mt = new MimeType(mimeType); |
41 | 0 | this.mimeType = mt.getPrimaryType() + "/" + mt.getSubType(); |
42 | 0 | if (mt.getParameter("charset") != null) |
43 | { | |
44 | 0 | encoding = mt.getParameter("charset"); |
45 | } | |
46 | } | |
47 | 0 | catch (MimeTypeParseException e) |
48 | { | |
49 | //TODO, this should really get thrown | |
50 | 0 | throw new MuleRuntimeException(e); |
51 | 0 | } |
52 | } | |
53 | 0 | } |
54 | ||
55 | public SimpleDataType(Class type) | |
56 | 0 | { |
57 | 0 | this.type = type; |
58 | 0 | } |
59 | ||
60 | public Class getType() | |
61 | { | |
62 | 0 | return type; |
63 | } | |
64 | ||
65 | public String getMimeType() | |
66 | { | |
67 | 0 | return mimeType; |
68 | } | |
69 | ||
70 | public void setMimeType(String mimeType) | |
71 | { | |
72 | 0 | this.mimeType = (mimeType == null ? ANY_MIME_TYPE : mimeType); |
73 | 0 | } |
74 | ||
75 | public String getEncoding() | |
76 | { | |
77 | 0 | return encoding; |
78 | } | |
79 | ||
80 | public void setEncoding(String encoding) | |
81 | { | |
82 | 0 | this.encoding = encoding; |
83 | 0 | } |
84 | ||
85 | public boolean isCompatibleWith(DataType dataType) | |
86 | { | |
87 | 0 | if (dataType instanceof ImmutableDataType) |
88 | { | |
89 | 0 | dataType = ((ImmutableDataType) dataType).getWrappedDataType(); |
90 | } | |
91 | 0 | if (this == dataType) |
92 | { | |
93 | 0 | return true; |
94 | } | |
95 | 0 | if (dataType == null) |
96 | { | |
97 | 0 | return false; |
98 | } | |
99 | ||
100 | 0 | SimpleDataType that = (SimpleDataType) dataType; |
101 | ||
102 | //ANY_MIME_TYPE will match to a null or non-null value for MimeType | |
103 | 0 | if ((this.getMimeType() == null && that.getMimeType() != null || that.getMimeType() == null && this.getMimeType() != null) && !ANY_MIME_TYPE.equals(this.mimeType) && !ANY_MIME_TYPE.equals(that.mimeType)) |
104 | { | |
105 | 0 | return false; |
106 | } | |
107 | ||
108 | 0 | if (this.getMimeType() != null && !this.getMimeType().equals(that.getMimeType()) && !ANY_MIME_TYPE.equals(that.getMimeType()) && !ANY_MIME_TYPE.equals(this.getMimeType())) |
109 | { | |
110 | 0 | return false; |
111 | } | |
112 | ||
113 | 0 | if (!fromPrimitive(this.getType()).isAssignableFrom(fromPrimitive(that.getType()))) |
114 | { | |
115 | 0 | return false; |
116 | } | |
117 | ||
118 | 0 | return true; |
119 | } | |
120 | ||
121 | ||
122 | private Class<?> fromPrimitive(Class<?> type) | |
123 | { | |
124 | 0 | Class<?> primitiveWrapper = MethodUtils.getPrimitiveWrapper(type); |
125 | 0 | if (primitiveWrapper != null) |
126 | { | |
127 | 0 | return primitiveWrapper; |
128 | } | |
129 | else | |
130 | { | |
131 | 0 | return type; |
132 | } | |
133 | } | |
134 | ||
135 | @Override | |
136 | public boolean equals(Object o) | |
137 | { | |
138 | 0 | if (this == o) |
139 | { | |
140 | 0 | return true; |
141 | } | |
142 | 0 | if (o == null || getClass() != o.getClass()) |
143 | { | |
144 | 0 | return false; |
145 | } | |
146 | ||
147 | 0 | SimpleDataType that = (SimpleDataType) o; |
148 | ||
149 | 0 | if (!type.equals(that.type)) |
150 | { | |
151 | 0 | return false; |
152 | } | |
153 | ||
154 | //ANY_MIME_TYPE will match to a null or non-null value for MimeType | |
155 | 0 | if ((this.mimeType == null && that.mimeType != null || that.mimeType == null && this.mimeType != null) && !ANY_MIME_TYPE.equals(that.mimeType)) |
156 | { | |
157 | 0 | return false; |
158 | } | |
159 | ||
160 | 0 | if (this.mimeType != null && !mimeType.equals(that.mimeType) && !ANY_MIME_TYPE.equals(that.mimeType)) |
161 | { | |
162 | 0 | return false; |
163 | } | |
164 | ||
165 | 0 | return true; |
166 | } | |
167 | ||
168 | @Override | |
169 | public int hashCode() | |
170 | { | |
171 | 0 | int result = type.hashCode(); |
172 | 0 | result = 31 * result + (encoding != null ? encoding.hashCode() : 0); |
173 | 0 | result = 31 * result + (mimeType != null ? mimeType.hashCode() : 0); |
174 | 0 | return result; |
175 | } | |
176 | ||
177 | ||
178 | @Override | |
179 | public String toString() | |
180 | { | |
181 | 0 | return "SimpleDataType{" + |
182 | "type=" + type.getName() + | |
183 | ", mimeType='" + mimeType + '\'' + | |
184 | '}'; | |
185 | } | |
186 | ||
187 | public DataType cloneDataType() | |
188 | { | |
189 | try | |
190 | { | |
191 | 0 | return (DataType) clone(); |
192 | } | |
193 | 0 | catch (CloneNotSupportedException e) |
194 | { | |
195 | // This cannot happen, because we implement Cloneable | |
196 | 0 | throw new IllegalStateException(e); |
197 | } | |
198 | } | |
199 | } |