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