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
062 } else if (clazz == short.class || clazz == Short.class) {
063 return pattern != null ? new ShortPatternFormat(pattern) : new ShortFormat();
064
065 } else if (clazz == int.class || clazz == Integer.class) {
066 return pattern != null ? new IntegerPatternFormat(pattern) : new IntegerFormat();
067
068 } else if (clazz == long.class || clazz == Long.class) {
069 return pattern != null ? new LongPatternFormat(pattern) : new LongFormat();
070
071 } else if (clazz == float.class || clazz == Float.class) {
072 return pattern != null ? new FloatPatternFormat(pattern) : new FloatFormat();
073
074 } else if (clazz == double.class || clazz == Double.class) {
075 return pattern != null ? new DoublePatternFormat(pattern) : new DoubleFormat();
076
077 } else if (clazz == BigDecimal.class) {
078 return new BigDecimalFormat(precision);
079
080 } else if (clazz == BigInteger.class) {
081 return new BigIntegerFormat();
082
083 } else if (clazz == String.class) {
084 return new StringFormat();
085
086 } else if (clazz == Date.class) {
087 return new DatePatternFormat(pattern);
088
089 } else if (clazz == char.class || clazz == Character.class) {
090 return new CharacterFormat();
091
092 } else {
093 throw new IllegalArgumentException("Can not find a suitable formatter for the type: " + clazz.getCanonicalName());
094 }
095 }
096
097 }