1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transformer.types; |
8 | |
|
9 | |
import org.mule.api.transformer.DataType; |
10 | |
import org.mule.util.generics.GenericsUtils; |
11 | |
import org.mule.util.generics.MethodParameter; |
12 | |
|
13 | |
import java.lang.reflect.Method; |
14 | |
import java.util.Collection; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public class CollectionDataType<T> extends SimpleDataType<T> |
23 | |
{ |
24 | |
private Class<? extends Collection> collectionType; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public CollectionDataType(Class<? extends Collection> collectionType) |
32 | |
{ |
33 | 0 | super(Object.class); |
34 | 0 | this.collectionType = collectionType; |
35 | 0 | } |
36 | |
|
37 | |
public CollectionDataType(Class<? extends Collection> collectionType, String mimeType) |
38 | |
{ |
39 | 0 | super(Object.class, mimeType); |
40 | 0 | this.collectionType = collectionType; |
41 | 0 | } |
42 | |
|
43 | |
public CollectionDataType(Class<? extends Collection> collectionType, Class type, String mimeType) |
44 | |
{ |
45 | 0 | super(type, mimeType); |
46 | 0 | this.collectionType = collectionType; |
47 | 0 | } |
48 | |
|
49 | |
public CollectionDataType(Class<? extends Collection> collectionType, Class type) |
50 | |
{ |
51 | 0 | super(type); |
52 | 0 | this.collectionType = collectionType; |
53 | 0 | } |
54 | |
|
55 | |
public Class<?> getItemType() |
56 | |
{ |
57 | 0 | return type; |
58 | |
} |
59 | |
|
60 | |
@Override |
61 | |
public Class getType() |
62 | |
{ |
63 | 0 | return collectionType; |
64 | |
} |
65 | |
|
66 | |
public static CollectionDataType createFromMethodReturn(Method m) |
67 | |
{ |
68 | 0 | return createFromMethodReturn(m, null); |
69 | |
} |
70 | |
|
71 | |
public static CollectionDataType createFromMethodReturn(Method m, String mimeType) |
72 | |
{ |
73 | 0 | Class collType = GenericsUtils.getCollectionReturnType(m); |
74 | |
|
75 | 0 | if (collType != null) |
76 | |
{ |
77 | 0 | return new CollectionDataType((Class<? extends Collection>)m.getReturnType(), collType, mimeType); |
78 | |
} |
79 | |
else |
80 | |
{ |
81 | 0 | throw new IllegalArgumentException("Return type for method is not a generic type collection. " + m); |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
public static CollectionDataType createFromMethodParam(Method m, int paramIndex) |
86 | |
{ |
87 | 0 | return createFromMethodParam(m, paramIndex, null); |
88 | |
} |
89 | |
|
90 | |
public static CollectionDataType createFromMethodParam(Method m, int paramIndex, String mimeType) |
91 | |
{ |
92 | 0 | Class collType = GenericsUtils.getCollectionParameterType(new MethodParameter(m, paramIndex)); |
93 | |
|
94 | 0 | if (collType != null) |
95 | |
{ |
96 | 0 | return new CollectionDataType((Class<? extends Collection>)m.getParameterTypes()[paramIndex], collType, mimeType); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | throw new IllegalArgumentException("Parameter type (index: " + paramIndex + ") for method is not a generic type collection. " + m); |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
public static boolean isReturnTypeACollection(Method m) |
105 | |
{ |
106 | 0 | return GenericsUtils.getCollectionReturnType(m) != null; |
107 | |
} |
108 | |
|
109 | |
public static boolean isParamTypeACollection(Method m, int paramIndex) |
110 | |
{ |
111 | 0 | return GenericsUtils.getCollectionParameterType(new MethodParameter(m, paramIndex)) != null; |
112 | |
} |
113 | |
|
114 | |
@Override |
115 | |
public boolean isCompatibleWith(DataType dataType) |
116 | |
{ |
117 | 0 | if (dataType instanceof ImmutableDataType) |
118 | |
{ |
119 | 0 | dataType = ((ImmutableDataType)dataType).getWrappedDataType(); |
120 | |
} |
121 | 0 | if (!(dataType instanceof CollectionDataType)) |
122 | |
{ |
123 | 0 | return false; |
124 | |
} |
125 | |
|
126 | 0 | if (!super.isCompatibleWith(dataType)) |
127 | |
{ |
128 | 0 | return false; |
129 | |
} |
130 | 0 | CollectionDataType that = (CollectionDataType) dataType; |
131 | |
|
132 | |
|
133 | 0 | return that.getItemType() == Object.class || this.getItemType().isAssignableFrom(that.getItemType()); |
134 | |
|
135 | |
} |
136 | |
|
137 | |
@Override |
138 | |
public boolean equals(Object o) |
139 | |
{ |
140 | 0 | if (this == o) |
141 | |
{ |
142 | 0 | return true; |
143 | |
} |
144 | 0 | if (o == null || getClass() != o.getClass()) |
145 | |
{ |
146 | 0 | return false; |
147 | |
} |
148 | |
|
149 | 0 | CollectionDataType that = (CollectionDataType) o; |
150 | |
|
151 | 0 | if (!getItemType().equals(that.getItemType())) |
152 | |
{ |
153 | 0 | return false; |
154 | |
} |
155 | |
|
156 | 0 | if ((mimeType != null ? !mimeType.equals(that.mimeType) : that.mimeType != null) && !ANY_MIME_TYPE.equals(that.mimeType) && !ANY_MIME_TYPE.equals(this.mimeType)) |
157 | |
{ |
158 | 0 | return false; |
159 | |
} |
160 | |
|
161 | 0 | return getType().equals(that.getType()); |
162 | |
|
163 | |
} |
164 | |
|
165 | |
@Override |
166 | |
public int hashCode() |
167 | |
{ |
168 | 0 | int result = getType().hashCode(); |
169 | 0 | result = 31 * result + getItemType().hashCode(); |
170 | 0 | result = 31 * result + (getMimeType() != null ? getMimeType().hashCode() : 0); |
171 | 0 | result = 31 * result + (getEncoding() != null ? getEncoding().hashCode() : 0); |
172 | 0 | return result; |
173 | |
} |
174 | |
|
175 | |
@Override |
176 | |
public String toString() |
177 | |
{ |
178 | 0 | return "CollectionDataType{" + |
179 | |
"type=" + getType().getName() + |
180 | |
", itemType=" + getItemType().getName() + |
181 | |
", mimeType='" + getMimeType() + '\'' + |
182 | |
'}'; |
183 | |
} |
184 | |
} |