View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer.types;
8   
9   import org.mule.api.transformer.DataType;
10  
11  /** A wrapper for a datatype that ensures it cannot be changed.  Get a non-immutable version by calling
12   * cloneDataType().
13    */
14  class ImmutableDataType<T> implements DataType<T>
15  {
16      private DataType<T> theDataType;
17  
18      /**
19       * Wrap a DataType with immutability.
20       */
21      public ImmutableDataType(DataType<T> theDataType)
22      {
23          this.theDataType = theDataType;
24      }
25  
26  
27      // These simply delegate
28      public Class<?> getType()
29      {
30          return theDataType.getType();
31      }
32  
33      public String getMimeType()
34      {
35          return theDataType.getMimeType();
36      }
37  
38      public String getEncoding()
39      {
40          return theDataType.getEncoding();
41      }
42  
43      public boolean isCompatibleWith(DataType dataType)
44      {
45          return theDataType.isCompatibleWith(dataType);
46      }
47  
48      public DataType cloneDataType()
49      {
50          return theDataType.cloneDataType();
51      }
52  
53      @Override
54      public String toString()
55      {
56          return theDataType.toString();    
57      }
58  
59      // These are illegal
60      public void setEncoding(String encoding)
61      {
62          attemptToMutate();
63      }
64  
65      public void setMimeType(String mimeType)
66      {
67          attemptToMutate();
68      }
69  
70      protected DataType<T> getWrappedDataType()
71      {
72          if (theDataType instanceof ImmutableDataType)
73          {
74              return ((ImmutableDataType)theDataType).getWrappedDataType();
75          }
76          else
77          {
78              return theDataType;
79          }
80      }
81  
82      private void attemptToMutate()
83      {
84          throw new UnsupportedOperationException("Attempt to change immutable DataType " + theDataType);
85      }
86      
87      @Override
88      public int hashCode()
89      {
90          return theDataType.hashCode();
91      }
92  }