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.joda.convert.StringConverter;
020
021/**
022 * Binding that adapts a Joda Convert {@link StringConverter}
023 * @param <S> The type this Binding converts to and from a String
024 */
025public class JodaConvertStringBinding<S> implements Binding<S, String>, StringConverter<S> {
026
027    private Class<S> boundClass;
028    private StringConverter<S> converter;
029
030    /**
031     * Creates a new instance
032     * @param boundClass The class that can be converted to and from a String
033     * @param converter The StringConverter instance to be wrapped
034     */
035    public JodaConvertStringBinding(Class<S> boundClass, StringConverter<S> converter) {
036        this.boundClass = boundClass;
037        this.converter = converter;
038    }
039
040        /**
041         * {@inheritDoc}
042         */
043        /* @Override */
044    public String marshal(S object) {
045        return converter.convertToString(object);
046    }
047
048        /**
049         * {@inheritDoc}
050         */
051        /* @Override */
052    public S unmarshal(String inputString) {
053        return converter.convertFromString(boundClass, inputString);
054    }
055
056        /**
057         * {@inheritDoc}
058         */
059        /* @Override */
060    public String convertToString(S object) {
061        return converter.convertToString(object);
062    }
063
064        /**
065         * {@inheritDoc}
066         */
067        /* @Override */
068    public S convertFromString(Class<? extends S> boundClass, String inputString) {
069        return converter.convertFromString(boundClass, inputString);
070    }
071
072        /**
073         * {@inheritDoc}
074         */
075        /* @Override */
076        public Class<S> getBoundClass() {
077                return boundClass;
078        }
079
080        /**
081         * {@inheritDoc}
082         */
083        /* @Override */
084        public Class<String> getTargetClass() {
085                return String.class;
086        }
087}