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.routing.filters;
8   
9   import static org.mule.util.ClassUtils.equal;
10  import static org.mule.util.ClassUtils.hash;
11  
12  import org.mule.api.MuleMessage;
13  import org.mule.api.routing.filter.Filter;
14  import org.mule.util.ClassUtils;
15  
16  /**
17   * <code>PayloadTypeFilter</code> filters based on the type of the object received.
18   */
19  
20  public class PayloadTypeFilter implements Filter
21  {
22      private Class expectedType;
23  
24      public PayloadTypeFilter()
25      {
26          super();
27      }
28  
29      public PayloadTypeFilter(String expectedType) throws ClassNotFoundException
30      {
31          this(ClassUtils.loadClass(expectedType, PayloadTypeFilter.class));
32      }
33  
34      public PayloadTypeFilter(Class expectedType)
35      {
36          this.expectedType = expectedType;
37      }
38  
39      public boolean accept(MuleMessage message)
40      {
41          return (expectedType != null ? expectedType.isAssignableFrom(message.getPayload().getClass()) : false);
42      }
43  
44      public Class getExpectedType()
45      {
46          return expectedType;
47      }
48  
49      public void setExpectedType(Class expectedType)
50      {
51          this.expectedType = expectedType;
52      }
53      
54      public boolean equals(Object obj)
55      {
56          if (this == obj) return true;
57          if (obj == null || getClass() != obj.getClass()) return false;
58  
59          final PayloadTypeFilter other = (PayloadTypeFilter) obj;
60          return equal(expectedType, other.expectedType);
61      }
62  
63      public int hashCode()
64      {
65          return hash(new Object[]{this.getClass(), expectedType});
66      }
67  }