001/*
002 *  Copyright 2010, 2011 Chris Pheby
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.jadira.bindings.core.general.binding;
017
018import org.jadira.bindings.core.api.Binding;
019import org.jadira.bindings.core.api.FromUnmarshaller;
020import org.jadira.bindings.core.api.ToMarshaller;
021
022/**
023 * Binding that supports a to contract, and from contract
024 * @param <S> Source type for the conversion
025 * @param <T> Target type
026 */
027public class CompositeBinding<S, T> implements Binding<S, T> {
028
029    private final ToMarshaller<S, T> marshal;
030    
031    private final FromUnmarshaller<S, T> unmarshal;
032    
033        /**
034         * Create a new instance of a {@link Binding} wrapping the given {@link ToMarshaller} and {@link FromUnmarshaller} instances
035         * @param marshal The ToMarshaller to wrap
036         * @param unmarshal The FromUnmarshaller to wrap
037         */
038    public CompositeBinding(ToMarshaller<S, T> marshal, FromUnmarshaller<S, T> unmarshal) {
039        
040        if (unmarshal == null) {
041            throw new IllegalStateException("Unmarshaller may not be null");
042        }
043        if (marshal == null) {
044            throw new IllegalStateException("Marshaller may not be null");
045        }
046        
047        this.unmarshal = unmarshal;
048        this.marshal = marshal;
049    }
050
051        /**
052         * {@inheritDoc}
053         */
054        /* @Override */
055    public T marshal(S object) {
056        return marshal.marshal(object);
057    }
058
059        /**
060         * {@inheritDoc}
061         */
062        /* @Override */
063    public S unmarshal(T inputString) {
064        return unmarshal.unmarshal(inputString);
065    }
066
067        /**
068         * {@inheritDoc}
069         */
070        /* @Override */
071        public Class<S> getBoundClass() {
072                return unmarshal.getBoundClass();
073        }
074
075        /**
076         * {@inheritDoc}
077         */
078        /* @Override */
079        public Class<T> getTargetClass() {
080                return unmarshal.getTargetClass();
081        }    
082}