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