View Javadoc

1   /*
2    * $Id: SimpleDataType.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.MuleRuntimeException;
13  import org.mule.api.transformer.DataType;
14  
15  import javax.activation.MimeType;
16  import javax.activation.MimeTypeParseException;
17  
18  /**
19   * A data type that simply wraps a Java type.  This type also allows a mime type to be associated
20   * with the Java type.
21   *
22   * @since 3.0
23   */
24  public class SimpleDataType<T> implements DataType<T>, Cloneable
25  {
26      protected Class<?> type;
27      protected String mimeType = ANY_MIME_TYPE;
28      protected String encoding;
29  
30      public SimpleDataType(Class<?> type, String mimeType)
31      {
32          this.type = type;
33          if (mimeType == null)
34          {
35              this.mimeType = ANY_MIME_TYPE;
36          }
37          else
38          {
39              try
40              {
41                  MimeType mt = new MimeType(mimeType);
42                  this.mimeType = mt.getPrimaryType() + "/" + mt.getSubType();
43                  if (mt.getParameter("charset") != null)
44                  {
45                      encoding = mt.getParameter("charset");
46                  }
47              }
48              catch (MimeTypeParseException e)
49              {
50                  //TODO, this should really get thrown
51                  throw new MuleRuntimeException(e);
52              }
53          }
54      }
55  
56      public SimpleDataType(Class type)
57      {
58          this.type = type;
59      }
60  
61      public Class getType()
62      {
63          return type;
64      }
65  
66      public String getMimeType()
67      {
68          return mimeType;
69      }
70  
71      public void setMimeType(String mimeType)
72      {
73          this.mimeType = (mimeType == null ? ANY_MIME_TYPE : mimeType);
74      }
75  
76      public String getEncoding()
77      {
78          return encoding;
79      }
80  
81      public void setEncoding(String encoding)
82      {
83          this.encoding = encoding;
84      }
85  
86      public boolean isCompatibleWith(DataType dataType)
87      {
88          if (dataType instanceof ImmutableDataType)
89          {
90              dataType = ((ImmutableDataType) dataType).getWrappedDataType();
91          }
92          if (this == dataType)
93          {
94              return true;
95          }
96          if (dataType == null)
97          {
98              return false;
99          }
100 
101         SimpleDataType that = (SimpleDataType) dataType;
102 
103         //ANY_MIME_TYPE will match to a null or non-null value for MimeType        
104         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))
105         {
106             return false;
107         }
108 
109         if (this.getMimeType() != null && !this.getMimeType().equals(that.getMimeType()) && !ANY_MIME_TYPE.equals(that.getMimeType()) && !ANY_MIME_TYPE.equals(this.getMimeType()))
110         {
111             return false;
112         }
113 
114         if (!this.getType().isAssignableFrom(that.getType()))
115         {
116             return false;
117         }
118 
119         return true;
120     }
121 
122     @Override
123     public boolean equals(Object o)
124     {
125         if (this == o)
126         {
127             return true;
128         }
129         if (o == null || getClass() != o.getClass())
130         {
131             return false;
132         }
133 
134         SimpleDataType that = (SimpleDataType) o;
135 
136         if (!type.equals(that.type))
137         {
138             return false;
139         }
140 
141         //ANY_MIME_TYPE will match to a null or non-null value for MimeType
142         if ((this.mimeType == null && that.mimeType != null || that.mimeType == null && this.mimeType != null) && !ANY_MIME_TYPE.equals(that.mimeType))
143         {
144             return false;
145         }
146 
147         if (this.mimeType != null && !mimeType.equals(that.mimeType) && !ANY_MIME_TYPE.equals(that.mimeType))
148         {
149             return false;
150         }
151 
152         return true;
153     }
154 
155     @Override
156     public int hashCode()
157     {
158         int result = type.hashCode();
159         result = 31 * result + (mimeType != null ? mimeType.hashCode() : 0);
160         return result;
161     }
162 
163     @Override
164     public String toString()
165     {
166         return "SimpleDataType{" +
167                 "type=" + type.getName() +
168                 ", mimeType='" + mimeType + '\'' +
169                 '}';
170     }
171 
172     public DataType cloneDataType()
173     {
174         try
175         {
176             return (DataType) clone();
177         }
178         catch (CloneNotSupportedException e)
179         {
180             // This cannot happen, because we implement Cloneable
181             throw new IllegalStateException(e);
182         }
183     }
184 }