View Javadoc

1   /*
2    * Copyright 2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ancientprogramming.fixedformat4j.format.impl;
17  
18  import com.ancientprogramming.fixedformat4j.exception.FixedFormatException;
19  import com.ancientprogramming.fixedformat4j.format.FixedFormatter;
20  import com.ancientprogramming.fixedformat4j.format.FormatContext;
21  import com.ancientprogramming.fixedformat4j.format.FormatInstructions;
22  
23  import java.io.Serializable;
24  import java.math.BigDecimal;
25  import java.util.Date;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  /**
30   * Formatter capable of formatting a bunch of known java standard library classes. So far:
31   * {@link String}, {@link Integer}, {@link Long}, {@link Date},
32   * {@link Character}, {@link Boolean}, {@link Double}, {@link Float} and {@link BigDecimal}
33   *
34   *
35   * @author Jacob von Eyben - http://www.ancientprogramming.com
36   * @since 1.0.0
37   */
38  public class ByTypeFormatter implements FixedFormatter {
39    private FormatContext context;
40  
41    private static final Map<Class<? extends Serializable>, Class<? extends FixedFormatter>> KNOWN_FORMATTERS = new HashMap<Class<? extends Serializable>, Class<? extends FixedFormatter>>();
42  
43    static {
44      KNOWN_FORMATTERS.put(String.class, StringFormatter.class);
45      KNOWN_FORMATTERS.put(int.class, IntegerFormatter.class);
46      KNOWN_FORMATTERS.put(Integer.class, IntegerFormatter.class);
47      KNOWN_FORMATTERS.put(long.class, LongFormatter.class);
48      KNOWN_FORMATTERS.put(Long.class, LongFormatter.class);
49      KNOWN_FORMATTERS.put(Date.class, DateFormatter.class);
50      KNOWN_FORMATTERS.put(Date.class, DateFormatter.class);
51      KNOWN_FORMATTERS.put(char.class, CharacterFormatter.class);
52      KNOWN_FORMATTERS.put(Character.class, CharacterFormatter.class);
53      KNOWN_FORMATTERS.put(boolean.class, BooleanFormatter.class);
54      KNOWN_FORMATTERS.put(Boolean.class, BooleanFormatter.class);
55      KNOWN_FORMATTERS.put(double.class, DoubleFormatter.class);
56      KNOWN_FORMATTERS.put(Double.class, DoubleFormatter.class);
57      KNOWN_FORMATTERS.put(float.class, FloatFormatter.class);
58      KNOWN_FORMATTERS.put(Float.class, FloatFormatter.class);
59      KNOWN_FORMATTERS.put(BigDecimal.class,  BigDecimalFormatter.class);
60    }
61  
62    public ByTypeFormatter(FormatContext context) {
63      this.context = context;
64    }
65  
66  
67    public Object parse(String value, FormatInstructions instructions) {
68      FixedFormatter formatter = actualFormatter(context.getDataType());
69      return formatter.parse(value, instructions);
70    }
71  
72    public String format(Object value, FormatInstructions instructions) {
73      FixedFormatter formatter = actualFormatter(context.getDataType());
74      return formatter.format(value, instructions);
75    }
76  
77    public FixedFormatter actualFormatter(final Class<? extends Object> dataType) {
78      Class<? extends FixedFormatter> formatterClass = KNOWN_FORMATTERS.get(dataType);
79  
80      if (formatterClass != null) {
81        try {
82          return formatterClass.getConstructor().newInstance();
83        } catch (NoSuchMethodException e) {
84          throw new FixedFormatException("Could not create instance of[" + formatterClass.getName() + "] because no default constructor exists");
85        } catch (Exception e) {
86          throw new FixedFormatException("Could not create instance of[" + formatterClass.getName() + "]", e);
87        }
88      } else {
89        throw new FixedFormatException(ByTypeFormatter.class.getName() + " cannot handle datatype[" + dataType.getName() + "]. Provide your own custom FixedFormatter for this datatype.");
90      }
91    }
92  }