/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.dataformat.bindy;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

import org.apache.camel.dataformat.bindy.format.BigDecimalFormat;
import org.apache.camel.dataformat.bindy.format.BigIntegerFormat;
import org.apache.camel.dataformat.bindy.format.ByteFormat;
import org.apache.camel.dataformat.bindy.format.BytePatternFormat;
import org.apache.camel.dataformat.bindy.format.CharacterFormat;
import org.apache.camel.dataformat.bindy.format.DatePatternFormat;
import org.apache.camel.dataformat.bindy.format.DoubleFormat;
import org.apache.camel.dataformat.bindy.format.DoublePatternFormat;
import org.apache.camel.dataformat.bindy.format.FloatFormat;
import org.apache.camel.dataformat.bindy.format.FloatPatternFormat;
import org.apache.camel.dataformat.bindy.format.IntegerFormat;
import org.apache.camel.dataformat.bindy.format.IntegerPatternFormat;
import org.apache.camel.dataformat.bindy.format.LongFormat;
import org.apache.camel.dataformat.bindy.format.LongPatternFormat;
import org.apache.camel.dataformat.bindy.format.ShortFormat;
import org.apache.camel.dataformat.bindy.format.ShortPatternFormat;
import org.apache.camel.dataformat.bindy.format.StringFormat;

/**
 * Factory to return {@link Format} classes for a given type.
 */
public final class FormatFactory {

    private FormatFactory() {
    }

    /**
     * Retrieves the format to use for the given type
     * 
     * @param clazz represents the type of the format (String, Integer, Byte)
     * @param pattern is the pattern to be used during the formating of the data
     * @param precision optional scale for BigDecimal parsing.
     * @return Format the formatter
     * @throws IllegalArgumentException if not suitable formatter is found
     */
    public static Format<?> getFormat(Class<?> clazz, String pattern, int precision) throws Exception {
        if (clazz == byte.class || clazz == Byte.class) {
            return pattern != null ? new BytePatternFormat(pattern) : new ByteFormat();

        } else if (clazz == short.class || clazz == Short.class) {
            return pattern != null ? new ShortPatternFormat(pattern) : new ShortFormat();

        } else if (clazz == int.class || clazz == Integer.class) {
            return pattern != null ? new IntegerPatternFormat(pattern) : new IntegerFormat();

        } else if (clazz == long.class || clazz == Long.class) {
            return pattern != null ? new LongPatternFormat(pattern) : new LongFormat();

        } else if (clazz == float.class || clazz == Float.class) {
            return pattern != null ? new FloatPatternFormat(pattern) : new FloatFormat();

        } else if (clazz == double.class || clazz == Double.class) {
            return pattern != null ? new DoublePatternFormat(pattern) : new DoubleFormat();

        } else if (clazz == BigDecimal.class) {
            return new BigDecimalFormat(precision);

        } else if (clazz == BigInteger.class) {
            return new BigIntegerFormat();

        } else if (clazz == String.class) {
            return new StringFormat();

        } else if (clazz == Date.class) {
            return new DatePatternFormat(pattern);

        } else if (clazz == char.class || clazz == Character.class) {
            return new CharacterFormat();

        } else {
            throw new IllegalArgumentException("Can not find a suitable formatter for the type: " + clazz.getCanonicalName());
        }
    }

}
