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.Constructor;
024    import java.lang.reflect.InvocationTargetException;
025    import java.lang.reflect.Type;
026    import java.lang.reflect.TypeVariable;
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    import org.granite.util.ClassUtil;
031    
032    /**
033     * @author Franck WOLFF
034     *
035     * @see Converter
036     * @see Reverter
037     */
038    public class Converters {
039    
040        /** Array of all configured converters */
041        private final Converter[] converters;
042    
043        /** Array of all configured reverters */
044        private final Reverter[] reverters;
045    
046        /**
047         * Constructs a new Converters instance with the supplied list of converters (possibly reverters).
048         *
049         * @param converterClasses the list of all used converters.
050         * @throws NoSuchMethodException if one of the Converter does not have a constructor with a
051         *          Converters parameter.
052         * @throws IllegalAccessException if something goes wrong when creating an instance of one
053         *          of the supplied Converter classes.
054         * @throws InvocationTargetException if something goes wrong when creating an instance of one
055         *          of the supplied Converter classes.
056         * @throws InstantiationException if something goes wrong when creating an instance of one
057         *          of the supplied Converter classes.
058         */
059        public Converters(List<Class<? extends Converter>> converterClasses)
060            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
061    
062            List<Converter> converters = new ArrayList<Converter>();
063            List<Reverter> reverters = new ArrayList<Reverter>();
064    
065            if (converterClasses != null) {
066                for (Class<? extends Converter> converterClass : converterClasses) {
067                    Constructor<? extends Converter> constructor = converterClass.getConstructor(Converters.class);
068                    Converter converter = constructor.newInstance(this);
069                    converters.add(converter);
070                    if (converter instanceof Reverter)
071                        reverters.add((Reverter)converter);
072                }
073            }
074    
075            this.converters = converters.toArray(new Converter[converters.size()]);
076            this.reverters = reverters.toArray(new Reverter[reverters.size()]);
077        }
078    
079        /**
080         * Returns a suitable converter for supplied parameters or null if no converter
081         * can be found. This method is equivalent to the
082         * {@link Converters#getConverter(Object, Type, boolean)} method with the
083         * throwNotFoundException parameter set to false.
084         *
085         * @param value the value to be converted
086         * @param targetType the type of the converted value
087         * @return a Converter instance or null if no suitable converter can be found
088         */
089        public Converter getConverter(Object value, Type targetType) {
090            return getConverter(value, targetType, false);
091        }
092    
093        /**
094         * Returns a suitable converter for supplied parameters or either returns null if no converter
095         * can be found or throws a {@link NoConverterFoundException}.
096         *
097         * @param value the value to be converted
098         * @param targetType the type of the converted value
099         * @param throwNotFoundException should an exception be thrown if no converter is found?
100         * @return a Converter instance or null if no suitable converter can be found
101         * @throws NoConverterFoundException if the throwNotFoundException parameter is set to true
102         *          and no converter can be found.
103         */
104        public Converter getConverter(Object value, Type targetType, boolean throwNotFoundException)
105            throws NoConverterFoundException {
106            
107            // Small optimization: this avoids to make TypeVariable conversion in all converters...
108            if (targetType instanceof TypeVariable<?>)
109                    targetType = ClassUtil.getBoundType((TypeVariable<?>)targetType);
110            
111            for (Converter converter : converters) {
112                if (converter.canConvert(value, targetType))
113                    return converter;
114            }
115    
116            if (!throwNotFoundException)
117                return null;
118    
119            throw new NoConverterFoundException(value, targetType);
120        }
121    
122        /**
123         * Converts the supplied object to the supplied target type. This method is
124         * a simple shortcut for: <tt>this.getConverter(value, target, true).convert(value, targetType)</tt>.
125         *
126         * @param value the object to be converted.
127         * @param targetType the target type.
128         * @return the converted object.
129         * @throws NoConverterFoundException if no suitable converter can be found.
130         */
131        public Object convert(Object value, Type targetType) throws NoConverterFoundException {
132            return getConverter(value, targetType, true).convert(value, targetType);
133        }
134    
135        /**
136         * Returns true if at least one reverter is configured for this Converters instance.
137         *
138         * @return true if at least one reverter is configured for this Converters instance.
139         */
140        public boolean hasReverters() {
141            return reverters.length > 0;
142        }
143    
144        /**
145         * Revert back to standard, AMF3 known Java type the supplied value. This method iterates
146         * on all configured Reverters and returns the {@link Reverter#revert(Object)} method result
147         * if the {@link Reverter#canRevert(Object)} method returns true for the current Reverter
148         * instance.
149         *
150         * @param value the value to be reverted.
151         * @return the reverted value (same instance if none of the configured reverters apply).
152         */
153        public Object revert(Object value) {
154            for (Reverter reverter : reverters) {
155                if (reverter.canRevert(value))
156                    return reverter.revert(value);
157            }
158            return value;
159        }
160        
161        public Converter[] getConverters() {
162            Converter[] copy = new Converter[converters.length];
163            System.arraycopy(converters, 0, copy, 0, converters.length);
164            return copy;
165        }
166    }