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.converter;
017
018import org.jadira.bindings.core.api.Converter;
019import org.jadira.bindings.core.api.FromUnmarshaller;
020
021/**
022 * A converter that wraps an unmarshaller to convert the unmarshaller for type S into a converter for type T to type S
023 * @param <S> input type
024 * @param <T> output type for the marshaller
025 */
026public class FromUnmarshallerConverter<S,T> implements Converter<T,S> {
027
028        private FromUnmarshaller<S,T> unmarshaller;
029
030        /**
031         * Create a new instance of a {@link Converter} wrapping the given {@link FromUnmarshaller}
032         * @param unmarshaller The FromUnmarshaller to wrap
033         */
034        public FromUnmarshallerConverter(FromUnmarshaller<S,T> unmarshaller) {
035                this.unmarshaller = unmarshaller;
036        }
037        
038        /**
039         * {@inheritDoc}
040         */
041        /* @Override */
042        public S convert(T inputObject) {
043                return unmarshaller.unmarshal(inputObject);
044        }
045        
046        /**
047         * Get the wrapped unmarshaller
048         * @return the wrapped unmarshaller
049         */
050        public FromUnmarshaller<S,T> getUnmarshaller() {
051                return unmarshaller;
052        }
053
054        /**
055         * {@inheritDoc}
056         */
057        /* @Override */
058        public Class<T> getInputClass() {
059                return unmarshaller.getTargetClass();
060        }
061
062        /**
063         * {@inheritDoc}
064         */
065        /* @Override */
066        public Class<S> getOutputClass() {
067                return unmarshaller.getBoundClass();
068        }
069}