001/*
002 *  Copyright 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. This binding reverses the interpretation
024 * of the binding it wraps so that the source and target relationships are reversed.
025 * @param <S> Source type for the conversion
026 * @param <T> Target type
027 */
028public class InverseCompositeBinding<S, T> implements Binding<S, T> {
029
030    private final ToMarshaller<T, S> marshal;
031    
032    private final FromUnmarshaller<T, S> unmarshal;
033    
034    /**
035     * Create a binding that reverses the interpretation of the supplied binding
036     * @param binding The binding to reverse
037     */
038    public InverseCompositeBinding(Binding<T, S> binding) {
039        
040        if (binding == null) {
041            throw new IllegalStateException("Binding may not be null");
042        }
043        
044        this.unmarshal = binding;
045        this.marshal = binding;
046    }    
047
048    /**
049     * Create a binding that reverses the interpretation of the supplied Marshaller and Unmarshaller
050     * @param marshal The Marshaller to be interpreted as an Unmarshaller
051     * @param unmarshal The Unmarshaller to be interpreted as an Marshaller
052     */
053    public InverseCompositeBinding(ToMarshaller<T, S> marshal, FromUnmarshaller<T, S> unmarshal) {
054        
055        if (unmarshal == null) {
056            throw new IllegalStateException("Unmarshaller may not be null");
057        }
058        if (marshal == null) {
059            throw new IllegalStateException("Marshaller may not be null");
060        }
061        
062        this.unmarshal = unmarshal;
063        this.marshal = marshal;
064    }
065
066        /**
067         * {@inheritDoc}
068         */
069        /* @Override */
070    public T marshal(S object) {
071        return unmarshal.unmarshal(object);
072    }
073
074        /**
075         * {@inheritDoc}
076         */
077        /* @Override */
078    public S unmarshal(T input) {
079        return marshal.marshal(input);
080    }
081
082        /**
083         * {@inheritDoc}
084         */
085        /* @Override */
086        public Class<S> getBoundClass() {
087                return marshal.getTargetClass();
088        }
089
090        /**
091         * {@inheritDoc}
092         */
093        /* @Override */
094        public Class<T> getTargetClass() {
095                return marshal.getBoundClass();
096        }
097}