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