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.closures;
17  
18  import groovy.lang.Closure;
19  
20  import org.openehealth.ipf.commons.core.modules.api.Transmogrifier;
21  
22  /**
23   * A transmogrifier that delegates to a {@link Closure}.
24   * 
25   * @author Martin Krasser
26   */
27  public class DelegatingTransmogrifier extends ClosureAdapter implements Transmogrifier<Object, Object> {
28  
29      public DelegatingTransmogrifier(Closure closure) {
30          super(closure);
31      }
32  
33      @Override
34      public Object zap(Object object, Object... params) {
35          if (getClosure().getParameterTypes().length == 2) {
36              return call(object, prepare(params));
37          } else {
38              return call(object);
39          }
40      }
41  
42      protected static Object prepare(Object... params) {
43          if (params == null) {
44              return null;
45          }
46          if (params.length == 1) {
47              return params[0];
48          }
49          return params;
50      }
51  }