001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.dataformat.bindy;
018    
019    import java.math.BigDecimal;
020    import java.math.BigInteger;
021    import java.util.Date;
022    import java.util.Locale;
023    
024    import org.apache.camel.dataformat.bindy.format.BigDecimalFormat;
025    import org.apache.camel.dataformat.bindy.format.BigIntegerFormat;
026    import org.apache.camel.dataformat.bindy.format.ByteFormat;
027    import org.apache.camel.dataformat.bindy.format.BytePatternFormat;
028    import org.apache.camel.dataformat.bindy.format.CharacterFormat;
029    import org.apache.camel.dataformat.bindy.format.DatePatternFormat;
030    import org.apache.camel.dataformat.bindy.format.DoubleFormat;
031    import org.apache.camel.dataformat.bindy.format.DoublePatternFormat;
032    import org.apache.camel.dataformat.bindy.format.FloatFormat;
033    import org.apache.camel.dataformat.bindy.format.FloatPatternFormat;
034    import org.apache.camel.dataformat.bindy.format.IntegerFormat;
035    import org.apache.camel.dataformat.bindy.format.IntegerPatternFormat;
036    import org.apache.camel.dataformat.bindy.format.LongFormat;
037    import org.apache.camel.dataformat.bindy.format.LongPatternFormat;
038    import org.apache.camel.dataformat.bindy.format.ShortFormat;
039    import org.apache.camel.dataformat.bindy.format.ShortPatternFormat;
040    import org.apache.camel.dataformat.bindy.format.StringFormat;
041    
042    /**
043     * Factory to return {@link Format} classes for a given type.
044     */
045    public final class FormatFactory {
046    
047        private FormatFactory() {
048        }
049    
050        /**
051         * Retrieves the format to use for the given type
052         * 
053         * @param clazz represents the type of the format (String, Integer, Byte)
054         * @param pattern is the pattern to be used during the formating of the data
055         * @param locale optional locale for NumberFormat and DateFormat parsing.
056         * @param precision optional scale for BigDecimal parsing.
057         * @return Format the formatter
058         * @throws IllegalArgumentException if not suitable formatter is found
059         */
060        public static Format<?> getFormat(Class<?> clazz, String pattern, String locale, int precision) throws Exception {
061            if (clazz == byte.class || clazz == Byte.class) {
062                return pattern != null ? new BytePatternFormat(pattern, getLocale(locale)) : new ByteFormat();
063    
064            } else if (clazz == short.class || clazz == Short.class) {
065                return pattern != null ? new ShortPatternFormat(pattern, getLocale(locale)) : new ShortFormat();
066    
067            } else if (clazz == int.class || clazz == Integer.class) {
068                return pattern != null ? new IntegerPatternFormat(pattern, getLocale(locale)) : new IntegerFormat();
069    
070            } else if (clazz == long.class || clazz == Long.class) {
071                return pattern != null ? new LongPatternFormat(pattern, getLocale(locale)) : new LongFormat();
072    
073            } else if (clazz == float.class || clazz == Float.class) {
074                return pattern != null ? new FloatPatternFormat(pattern, getLocale(locale)) : new FloatFormat();
075    
076            } else if (clazz == double.class || clazz == Double.class) {
077                return pattern != null ? new DoublePatternFormat(pattern, getLocale(locale)) : new DoubleFormat();
078    
079            } else if (clazz == BigDecimal.class) {
080                return new BigDecimalFormat(precision);
081    
082            } else if (clazz == BigInteger.class) {
083                return new BigIntegerFormat();
084    
085            } else if (clazz == String.class) {
086                return new StringFormat();
087    
088            } else if (clazz == Date.class) {
089                return new DatePatternFormat(pattern, getLocale(locale));
090    
091            } else if (clazz == char.class || clazz == Character.class) {
092                return new CharacterFormat();
093    
094            } else {
095                throw new IllegalArgumentException("Can not find a suitable formatter for the type: " + clazz.getCanonicalName());
096            }
097        }
098    
099        private static Locale getLocale(String locale) {
100            if (locale != null && !(locale.length() == 0)) {
101                String[] result = locale.split("-");
102                if (result.length <= 2) {
103                    return result.length == 1 ? new Locale(result[0]) : new Locale(result[0], result[1]);
104                }
105            }
106            return null;
107        }
108    
109    }