1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36
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 }