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.binding;
17  
18  import org.jadira.bindings.core.api.Binding;
19  import org.jadira.bindings.core.api.FromUnmarshaller;
20  
21  /**
22   * Binding that supports a from contract but not to
23   * @param <S> Source type for the conversion
24   * @param <T> Target type
25   */
26  public class FromBinding<S, T> implements Binding<S, T> {
27  
28      private final FromUnmarshaller<S, T> unmarshal;
29      
30  	/**
31  	 * Create a new instance of a {@link Binding} wrapping the given {@link FromUnmarshaller} instance
32  	 * @param unmarshal The FromUnmarshaller to wrap
33  	 */
34      public FromBinding(FromUnmarshaller<S, T> unmarshal) {
35          
36          if (unmarshal == null) {
37              throw new IllegalStateException("Unmarshaller may not be null");
38          }
39          
40          this.unmarshal = unmarshal;
41      }
42  
43  	/**
44  	 * {@inheritDoc}
45  	 */
46  	/* @Override */
47      public T marshal(S object) throws UnsupportedOperationException {
48          throw new UnsupportedOperationException("FromBinding does not support marshalling");
49      }
50  
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	/* @Override */
55      public S unmarshal(T inputString) {
56          return unmarshal.unmarshal(inputString);
57      }
58  
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	/* @Override */
63  	public Class<S> getBoundClass() {
64  		return unmarshal.getBoundClass();
65  	}
66  
67  	/**
68  	 * {@inheritDoc}
69  	 */
70  	/* @Override */
71  	public Class<T> getTargetClass() {
72  		return unmarshal.getTargetClass();
73  	}
74  }