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
023 import org.apache.camel.dataformat.bindy.format.BigDecimalFormat;
024 import org.apache.camel.dataformat.bindy.format.BigIntegerFormat;
025 import org.apache.camel.dataformat.bindy.format.ByteFormat;
026 import org.apache.camel.dataformat.bindy.format.BytePatternFormat;
027 import org.apache.camel.dataformat.bindy.format.CharacterFormat;
028 import org.apache.camel.dataformat.bindy.format.DatePatternFormat;
029 import org.apache.camel.dataformat.bindy.format.DoubleFormat;
030 import org.apache.camel.dataformat.bindy.format.DoublePatternFormat;
031 import org.apache.camel.dataformat.bindy.format.FloatFormat;
032 import org.apache.camel.dataformat.bindy.format.FloatPatternFormat;
033 import org.apache.camel.dataformat.bindy.format.IntegerFormat;
034 import org.apache.camel.dataformat.bindy.format.IntegerPatternFormat;
035 import org.apache.camel.dataformat.bindy.format.LongFormat;
036 import org.apache.camel.dataformat.bindy.format.LongPatternFormat;
037 import org.apache.camel.dataformat.bindy.format.ShortFormat;
038 import org.apache.camel.dataformat.bindy.format.ShortPatternFormat;
039 import org.apache.camel.dataformat.bindy.format.StringFormat;
040
041 /**
042 * Factory to return {@link Format} classes for a given type.
043 */
044 public final class FormatFactory {
045
046 private FormatFactory() {
047 }
048
049 /**
050 * Retrieves the format to use for the given type
051 *
052 * @param clazz represents the type of the format (String, Integer, Byte)
053 * @param pattern is the pattern to be used during the formating of the data
054 * @param precision optional scale for BigDecimal parsing.
055 * @return Format the formatter
056 * @throws IllegalArgumentException if not suitable formatter is found
057 */
058 public static Format<?> getFormat(Class<?> clazz, String pattern, int precision) throws Exception {
059 if (clazz == byte.class || clazz == Byte.class) {
060 return pattern != null ? new BytePatternFormat(pattern) : new ByteFormat();
061 } else if (clazz == short.class || clazz == Short.class) {
062 return pattern != null ? new ShortPatternFormat(pattern) : new ShortFormat();
063 } else if (clazz == int.class || clazz == Integer.class) {
064 return pattern != null ? new IntegerPatternFormat(pattern) : new IntegerFormat();
065 } else if (clazz == long.class || clazz == Long.class) {
066 return pattern != null ? new LongPatternFormat(pattern) : new LongFormat();
067 } else if (clazz == float.class || clazz == Float.class) {
068 return pattern != null ? new FloatPatternFormat(pattern) : new FloatFormat();
069 } else if (clazz == double.class || clazz == Double.class) {
070 return pattern != null ? new DoublePatternFormat(pattern) : new DoubleFormat();
071 } else if (clazz == BigDecimal.class) {
072 return new BigDecimalFormat(precision);
073 } else if (clazz == BigInteger.class) {
074 return new BigIntegerFormat();
075 } else if (clazz == String.class) {
076 return new StringFormat();
077 } else if (clazz == Date.class) {
078 return new DatePatternFormat(pattern);
079 } else if (clazz == char.class || clazz == Character.class) {
080 return new CharacterFormat();
081 } else {
082 throw new IllegalArgumentException("Can not find a suitable formatter for the type: " + clazz.getCanonicalName());
083 }
084 }
085
086 }