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