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