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.unmarshaller;
017
018import org.jadira.bindings.core.api.Converter;
019import org.jadira.bindings.core.api.FromUnmarshaller;
020
021/**
022 * Unmarshaller supports constructor parameterised by a target object
023 * @param <S> Source type for the conversion
024 * @param <T> Target type
025 */
026public final class ConverterFromUnmarshaller<S, T> implements FromUnmarshaller<S, T> {
027
028    private final Converter<T,S> converter;
029    
030        /**
031         * Create a new instance for the given converter
032         * @param converter The converter to be wrapped
033         */
034    public ConverterFromUnmarshaller(Converter<T,S> converter) {
035        
036        if (converter == null) {
037                throw new IllegalStateException("converter must not be null");
038        }
039        this.converter = converter;        
040    }
041
042        /**
043         * {@inheritDoc}
044         */
045        /* @Override */
046    public S unmarshal(T object) {
047        
048        return converter.convert(object);
049    }
050    
051        /**
052         * {@inheritDoc}
053         */
054        /* @Override */
055    public Class<S> getBoundClass() {
056        return converter.getOutputClass();
057    }
058    
059        /**
060         * {@inheritDoc}
061         */
062        /* @Override */
063    public Class<T> getTargetClass() {
064        return converter.getInputClass();
065    }
066}