View Javadoc

1   /*
2    * $Id: ImmutableDataType.java 20616 2010-12-10 16:11:21Z 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  
11  package org.mule.transformer.types;
12  
13  import org.mule.api.transformer.DataType;
14  
15  /** A wrapper for a datatype that ensures it cannot be changed.  Get a non-immutable version by calling
16   * cloneDataType().
17    */
18  class ImmutableDataType<T> implements DataType<T>
19  {
20      private DataType<T> theDataType;
21  
22      /**
23       * Wrap a DataType with immutability.
24       */
25      public ImmutableDataType(DataType<T> theDataType)
26      {
27          this.theDataType = theDataType;
28      }
29  
30  
31      // These simply delegate
32      public Class<?> getType()
33      {
34          return theDataType.getType();
35      }
36  
37      public String getMimeType()
38      {
39          return theDataType.getMimeType();
40      }
41  
42      public String getEncoding()
43      {
44          return theDataType.getEncoding();
45      }
46  
47      public boolean isCompatibleWith(DataType dataType)
48      {
49          return theDataType.isCompatibleWith(dataType);
50      }
51  
52      public DataType cloneDataType()
53      {
54          return theDataType.cloneDataType();
55      }
56  
57      @Override
58      public String toString()
59      {
60          return theDataType.toString();    
61      }
62  
63      // These are illegal
64      public void setEncoding(String encoding)
65      {
66          attemptToMutate();
67      }
68  
69      public void setMimeType(String mimeType)
70      {
71          attemptToMutate();
72      }
73  
74      protected DataType<T> getWrappedDataType()
75      {
76          if (theDataType instanceof ImmutableDataType)
77          {
78              return ((ImmutableDataType)theDataType).getWrappedDataType();
79          }
80          else
81          {
82              return theDataType;
83          }
84      }
85  
86      private void attemptToMutate()
87      {
88          throw new UnsupportedOperationException("Attempt to change immutable DataType " + theDataType);
89      }
90      
91      @Override
92      public int hashCode()
93      {
94          return theDataType.hashCode();
95      }
96  }