001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.messaging.amf.io.convert;
022    
023    import java.lang.reflect.Type;
024    import java.lang.reflect.TypeVariable;
025    
026    import org.granite.util.ClassUtil;
027    
028    /**
029     * @author Franck WOLFF
030     */
031    public abstract class Converter {
032    
033        protected final Converters converters;
034    
035        /**
036         * Build a new Converter instance.
037         *
038         * @param converters a {@link Converters} instance (must be not null).
039         * @throws NullPointerException (if converters is null).
040         */
041        public Converter(Converters converters) {
042            if (converters == null)
043                throw new NullPointerException("converters parameter cannot be null");
044            this.converters = converters;
045        }
046    
047        /**
048         * Tells if the supplied object may be converted to the supplied target type by
049         * this converter.
050         *
051         * @param value the object to be converted.
052         * @param targetType the target type.
053         * @return true if this converter can convert o to the target type, false otherwise.
054         */
055        public final boolean canConvert(Object value, Type targetType) {
056            if (targetType instanceof TypeVariable<?>)
057                    targetType = ClassUtil.getBoundType((TypeVariable<?>)targetType);
058            return internalCanConvert(value, targetType);
059        }
060        
061        protected abstract boolean internalCanConvert(Object value, Type targetType);
062    
063        /**
064         * Converts the supplied object to the supplied target type.
065         *
066         * @param value the object to be converted.
067         * @param targetType the target type.
068         * @return the converted object.
069         */
070        public final Object convert(Object value, Type targetType) {
071            if (targetType instanceof TypeVariable<?>)
072                    targetType = ClassUtil.getBoundType((TypeVariable<?>)targetType);
073            return internalConvert(value, targetType);
074        }
075    
076        protected abstract Object internalConvert(Object value, Type targetType);
077    }