View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.converter.impl;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.seasar.cubby.converter.Converter;
23  
24  /**
25   * {@link Converter} の抽象クラスです。
26   * 
27   * @author baba
28   */
29  public abstract class AbstractConverter implements Converter {
30  
31  	/** プリミティブ型の配列クラスとラッパー型の配列クラスのマッピング */
32  	private static final Map<Class<?>, Class<?>> PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY = new HashMap<Class<?>, Class<?>>();
33  	static {
34  		PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.put(boolean[].class, Boolean[].class);
35  		PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.put(char[].class, Character[].class);
36  		PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.put(byte[].class, Byte[].class);
37  		PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.put(short[].class, Short[].class);
38  		PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.put(int[].class, Integer[].class);
39  		PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.put(long[].class, Long[].class);
40  		PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.put(float[].class, Float[].class);
41  		PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.put(double[].class, Double[].class);
42  	}
43  
44  	/**
45  	 * {@inheritDoc}
46  	 */
47  	public boolean canConvert(final Class<?> parameterType,
48  			final Class<?> objectType) {
49  		if (getObjectType().isAssignableFrom(objectType)) {
50  			return true;
51  		}
52  		if (PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY.containsKey(objectType)) {
53  			final Class<?> wrapperArray = PRIMITIVE_ARRAY_TO_WRAPPER_ARRAY
54  					.get(objectType);
55  			return canConvert(parameterType, wrapperArray);
56  		}
57  		return false;
58  	}
59  
60  }