View Javadoc
1   /*
2    *  Copyright 2010, 2011 Chris Pheby
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.jadira.bindings.core.general.unmarshaller;
17  
18  import org.jadira.bindings.core.api.Converter;
19  import org.jadira.bindings.core.api.FromUnmarshaller;
20  
21  /**
22   * Unmarshaller supports constructor parameterised by a target object
23   * @param <S> Source type for the conversion
24   * @param <T> Target type
25   */
26  public final class ConverterFromUnmarshaller<S, T> implements FromUnmarshaller<S, T> {
27  
28      private final Converter<T,S> converter;
29      
30  	/**
31  	 * Create a new instance for the given converter
32  	 * @param converter The converter to be wrapped
33  	 */
34      public ConverterFromUnmarshaller(Converter<T,S> converter) {
35          
36      	if (converter == null) {
37      		throw new IllegalStateException("converter must not be null");
38      	}
39          this.converter = converter;        
40      }
41  
42  	/**
43  	 * {@inheritDoc}
44  	 */
45  	/* @Override */
46      public S unmarshal(T object) {
47      	
48      	return converter.convert(object);
49      }
50      
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	/* @Override */
55      public Class<S> getBoundClass() {
56          return converter.getOutputClass();
57      }
58      
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	/* @Override */
63      public Class<T> getTargetClass() {
64          return converter.getInputClass();
65      }
66  }