View Javadoc

1   /*
2    * $Id: CollectionDataType.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * A data type that represents a generified collection.  When checked for compatability both the colection type and the
21   * generic item type will be compared.
22   *
23   * @since 3.0
24   */
25  public class CollectionDataType<T> extends SimpleDataType<T>
26  {
27      private Class<? extends Collection> collectionType;
28  
29      /**
30       * Creates an untyped collection data type
31       *
32       * @param collectionType the collection class type
33       */
34      public CollectionDataType(Class<? extends Collection> collectionType)
35      {
36          super(Object.class);
37          this.collectionType = collectionType;
38      }
39  
40      public CollectionDataType(Class<? extends Collection> collectionType, String mimeType)
41      {
42          super(Object.class, mimeType);
43          this.collectionType = collectionType;
44      }
45  
46      public CollectionDataType(Class<? extends Collection> collectionType, Class type, String mimeType)
47      {
48          super(type, mimeType);
49          this.collectionType = collectionType;
50      }
51  
52      public CollectionDataType(Class<? extends Collection> collectionType, Class type)
53      {
54          super(type);
55          this.collectionType = collectionType;
56      }
57  
58      public Class<?> getItemType()
59      {
60          return type;
61      }
62  
63      @Override
64      public Class getType()
65      {
66          return collectionType;
67      }
68  
69      public static CollectionDataType createFromMethodReturn(Method m)
70      {
71          return createFromMethodReturn(m, null);
72      }
73  
74      public static CollectionDataType createFromMethodReturn(Method m, String mimeType)
75      {
76          Class collType = GenericsUtils.getCollectionReturnType(m);
77  
78          if (collType != null)
79          {
80              return new CollectionDataType((Class<? extends Collection>)m.getReturnType(), collType, mimeType);
81          }
82          else
83          {
84              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          return createFromMethodParam(m, paramIndex, null);
91      }
92  
93      public static CollectionDataType createFromMethodParam(Method m, int paramIndex, String mimeType)
94      {
95          Class collType = GenericsUtils.getCollectionParameterType(new MethodParameter(m, paramIndex));
96  
97          if (collType != null)
98          {
99              return new CollectionDataType((Class<? extends Collection>)m.getParameterTypes()[paramIndex], collType, mimeType);
100         }
101         else
102         {
103             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         return GenericsUtils.getCollectionReturnType(m) != null;
110     }
111 
112     public static boolean isParamTypeACollection(Method m, int paramIndex)
113     {
114         return GenericsUtils.getCollectionParameterType(new MethodParameter(m, paramIndex)) != null;
115     }
116 
117     @Override
118     public boolean isCompatibleWith(DataType dataType)
119     {
120         if (dataType instanceof ImmutableDataType)
121         {
122             dataType = ((ImmutableDataType)dataType).getWrappedDataType();
123         }
124         if (!(dataType instanceof CollectionDataType))
125         {
126             return false;
127         }
128 
129         if (!super.isCompatibleWith(dataType))
130         {
131             return false;
132         }
133         CollectionDataType that = (CollectionDataType) dataType;
134 
135         //Untyped compatible collection
136         return that.getItemType() == Object.class || this.getItemType().isAssignableFrom(that.getItemType());
137 
138     }
139 
140     @Override
141     public boolean equals(Object o)
142     {
143         if (this == o)
144         {
145             return true;
146         }
147         if (o == null || getClass() != o.getClass())
148         {
149             return false;
150         }
151 
152         CollectionDataType that = (CollectionDataType) o;
153 
154         if (!getItemType().equals(that.getItemType()))
155         {
156             return false;
157         }
158 
159         if ((mimeType != null ? !mimeType.equals(that.mimeType) : that.mimeType != null) && !ANY_MIME_TYPE.equals(that.mimeType) && !ANY_MIME_TYPE.equals(this.mimeType))
160         {
161             return false;
162         }
163 
164         return getType().equals(that.getType());
165 
166     }
167 
168     @Override
169     public int hashCode()
170     {
171         int result = getType().hashCode();
172         result = 31 * result + getItemType().hashCode();
173         result = 31 * result + (getMimeType() != null ? getMimeType().hashCode() : 0);
174         return result;
175     }
176 
177     @Override
178     public String toString()
179     {
180         return "CollectionDataType{" +
181                 "type=" + getType().getName() +
182                 ", itemType=" + getItemType().getName() +
183                 ", mimeType='" + getMimeType() + '\'' +
184                 '}';
185     }
186 }