1
2
3
4
5
6
7
8
9
10 package org.mule.transformer.types;
11
12 import org.mule.api.MuleRuntimeException;
13 import org.mule.api.transformer.DataType;
14
15 import javax.activation.MimeType;
16 import javax.activation.MimeTypeParseException;
17
18 import org.apache.commons.beanutils.MethodUtils;
19
20
21
22
23
24
25
26 public class SimpleDataType<T> implements DataType<T>, Cloneable
27 {
28 protected Class<?> type;
29 protected String mimeType = ANY_MIME_TYPE;
30 protected String encoding;
31
32 public SimpleDataType(Class<?> type, String mimeType)
33 {
34 this.type = type;
35 if (mimeType == null)
36 {
37 this.mimeType = ANY_MIME_TYPE;
38 }
39 else
40 {
41 try
42 {
43 MimeType mt = new MimeType(mimeType);
44 this.mimeType = mt.getPrimaryType() + "/" + mt.getSubType();
45 if (mt.getParameter("charset") != null)
46 {
47 encoding = mt.getParameter("charset");
48 }
49 }
50 catch (MimeTypeParseException e)
51 {
52
53 throw new MuleRuntimeException(e);
54 }
55 }
56 }
57
58 public SimpleDataType(Class type)
59 {
60 this.type = type;
61 }
62
63 public Class getType()
64 {
65 return type;
66 }
67
68 public String getMimeType()
69 {
70 return mimeType;
71 }
72
73 public void setMimeType(String mimeType)
74 {
75 this.mimeType = (mimeType == null ? ANY_MIME_TYPE : mimeType);
76 }
77
78 public String getEncoding()
79 {
80 return encoding;
81 }
82
83 public void setEncoding(String encoding)
84 {
85 this.encoding = encoding;
86 }
87
88 public boolean isCompatibleWith(DataType dataType)
89 {
90 if (dataType instanceof ImmutableDataType)
91 {
92 dataType = ((ImmutableDataType) dataType).getWrappedDataType();
93 }
94 if (this == dataType)
95 {
96 return true;
97 }
98 if (dataType == null)
99 {
100 return false;
101 }
102
103 SimpleDataType that = (SimpleDataType) dataType;
104
105
106 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))
107 {
108 return false;
109 }
110
111 if (this.getMimeType() != null && !this.getMimeType().equals(that.getMimeType()) && !ANY_MIME_TYPE.equals(that.getMimeType()) && !ANY_MIME_TYPE.equals(this.getMimeType()))
112 {
113 return false;
114 }
115
116 if (!fromPrimitive(this.getType()).isAssignableFrom(fromPrimitive(that.getType())))
117 {
118 return false;
119 }
120
121 return true;
122 }
123
124
125 private Class<?> fromPrimitive(Class<?> type)
126 {
127 Class<?> primitiveWrapper = MethodUtils.getPrimitiveWrapper(type);
128 if (primitiveWrapper != null)
129 {
130 return primitiveWrapper;
131 }
132 else
133 {
134 return type;
135 }
136 }
137
138 @Override
139 public boolean equals(Object o)
140 {
141 if (this == o)
142 {
143 return true;
144 }
145 if (o == null || getClass() != o.getClass())
146 {
147 return false;
148 }
149
150 SimpleDataType that = (SimpleDataType) o;
151
152 if (!type.equals(that.type))
153 {
154 return false;
155 }
156
157
158 if ((this.mimeType == null && that.mimeType != null || that.mimeType == null && this.mimeType != null) && !ANY_MIME_TYPE.equals(that.mimeType))
159 {
160 return false;
161 }
162
163 if (this.mimeType != null && !mimeType.equals(that.mimeType) && !ANY_MIME_TYPE.equals(that.mimeType))
164 {
165 return false;
166 }
167
168 return true;
169 }
170
171 @Override
172 public int hashCode()
173 {
174 int result = type.hashCode();
175 result = 31 * result + (encoding != null ? encoding.hashCode() : 0);
176 result = 31 * result + (mimeType != null ? mimeType.hashCode() : 0);
177 return result;
178 }
179
180
181 @Override
182 public String toString()
183 {
184 return "SimpleDataType{" +
185 "type=" + type.getName() +
186 ", mimeType='" + mimeType + '\'' +
187 '}';
188 }
189
190 public DataType cloneDataType()
191 {
192 try
193 {
194 return (DataType) clone();
195 }
196 catch (CloneNotSupportedException e)
197 {
198
199 throw new IllegalStateException(e);
200 }
201 }
202 }