View Javadoc
1   /*
2    * Copyright 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.platform.camel.core.adapter;
17  
18  import org.apache.camel.Exchange;
19  import org.apache.camel.Expression;
20  import org.openehealth.ipf.commons.core.modules.api.Predicate;
21  
22  /**
23   * Adapts a {@link Predicate}.
24   * 
25   * @author Martin Krasser
26   */
27  public class PredicateAdapter extends AdapterSupport implements org.apache.camel.Predicate {
28  
29      private final Predicate predicate;
30      
31      /**
32       * Creates a new {@link PredicateAdapter} and sets the delegate
33       * {@link Predicate}.
34       * 
35       * @param predicate
36       *            a predicate.
37       */
38      public PredicateAdapter(Predicate predicate) {
39          this.predicate = predicate;
40      }
41      
42      @Override // make use of co-variant return types to support DSL 
43      public PredicateAdapter input(Expression inputExpression) {
44          return (PredicateAdapter)super.input(inputExpression);
45      }
46  
47      @Override // make use of co-variant return types to support DSL 
48      public PredicateAdapter params(Expression paramsExpression) {
49          return (PredicateAdapter)super.params(paramsExpression);
50      }
51  
52      @Override // make use of co-variant return types to support DSL 
53      public PredicateAdapter staticParams(Object... params) {
54          return (PredicateAdapter)super.staticParams(params);
55      }
56  
57      /**
58       * Delegates matching to the delegate {@link Predicate} applying input- and
59       * params {@link Expression}s.
60       * 
61       * @param exchange message exchange.
62       * 
63       * @see #input(Expression)
64       * @see #params(Expression)
65       * @see #staticParams(Object...)
66       */
67      @Override
68      public boolean matches(Exchange exchange) {
69          Object input = adaptInput(exchange);
70          Object params = adaptParams(exchange);
71          if (params == null) {
72              return predicate.matches(input, (Object[])null);
73          } else if (params.getClass().isArray()) {
74              return predicate.matches(input, (Object[])params);
75          } else {
76              return predicate.matches(input, params);
77          }
78      }
79  
80      /* (non-Javadoc)
81       * @see org.apache.camel.Predicate#assertMatches(java.lang.String, java.lang.Object)
82       */
83      public void assertMatches(String text, Exchange exchange) throws AssertionError {
84          if (!matches(exchange)) {
85              throw new AssertionError(text);
86          }
87          
88      }
89  
90  }