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.ToMarshaller;
020
021/**
022 * Binding that supports a to contract
023 * @param <T> the type of the class for the conversion
024 */
025public class ToBinding<S, T> implements Binding<S, T> {
026
027    private final ToMarshaller<S, T> marshal;
028    
029        /**
030         * Create a new instance of a {@link Binding} wrapping the given {@link ToMarshaller}
031         * @param marshal The ToMarshaller to wrap
032         */
033    public ToBinding(ToMarshaller<S, T> marshal) {
034        
035        if (marshal == null) {
036            throw new IllegalStateException("Marshaller may not be null");
037        }
038        
039        this.marshal = marshal;
040    }
041
042        /**
043         * {@inheritDoc}
044         */
045        /* @Override */
046    public T marshal(S object) {
047        return marshal.marshal(object);
048    }
049
050        /**
051         * {@inheritDoc}
052         */
053        /* @Override */
054    public S unmarshal(T inputString) {
055        throw new UnsupportedOperationException("ToBinding does not support unmarshalling");
056    }
057
058        /**
059         * {@inheritDoc}
060         */
061        /* @Override */
062        public Class<S> getBoundClass() {
063                return marshal.getBoundClass();
064        }
065
066        /**
067         * {@inheritDoc}
068         */
069        /* @Override */
070        public Class<T> getTargetClass() {
071                return marshal.getTargetClass();
072        }
073}