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