View Javadoc
1   /*
2    * Copyright 2013 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  
17  package com.orangesignal.csv.bean;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.text.Format;
21  
22  /**
23   * {@link Format} に関するユーティリティを提供します。
24   * 
25   * @author Koji Sugisawa
26   * @since 1.4.0
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  }