1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv.bean;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.text.Format;
21
22
23
24
25
26
27
28 abstract class FormatUtils {
29
30
31
32
33 protected FormatUtils() {
34 }
35
36 public static Format mergeFormatPattern(final Format format, final Format... formats) {
37 final StringBuilder buf = new StringBuilder();
38 buf.append(getFormatPattern(format));
39 for (final Format fmt : formats) {
40 buf.append(getFormatPattern(fmt));
41 }
42
43 final Format result = (Format) format.clone();
44 try {
45 result.getClass().getMethod("applyPattern", String.class).invoke(result, buf.toString());
46 } catch (final NoSuchMethodException e) {
47 throw new IllegalStateException(e.getMessage(), e);
48 } catch (final IllegalAccessException e) {
49 throw new IllegalStateException(e.getMessage(), e);
50 } catch (final InvocationTargetException e) {
51 throw new IllegalStateException(e.getMessage(), e);
52 }
53 return result;
54 }
55
56 private static String getFormatPattern(final Format format) {
57 try {
58 return (String) format.getClass().getMethod("toPattern").invoke(format);
59 } catch (final NoSuchMethodException e) {
60 throw new IllegalStateException(e.getMessage(), e);
61 } catch (final IllegalAccessException e) {
62 throw new IllegalStateException(e.getMessage(), e);
63 } catch (final InvocationTargetException e) {
64 throw new IllegalStateException(e.getMessage(), e);
65 }
66 }
67
68 }