View Javadoc

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