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 org.seasar.cubby.converter.ConversionHelper; 20 21 /** 22 * 任意のオブジェクトから{@link Character}への変換を行うコンバータです。 23 * <p> 24 * 変換元のオブジェクトの文字列表現の先頭の文字を表す{@link Character}へ変換します。 そうでない場合は <code>null</code> 25 * とします。 26 * </p> 27 * 28 * @author baba 29 */ 30 public class CharacterConverter extends AbstractConverter { 31 32 /** 33 * {@inheritDoc} 34 */ 35 public Class<?> getObjectType() { 36 return Character.class; 37 } 38 39 /** 40 * {@inheritDoc} 41 */ 42 public Object convertToObject(final Object value, 43 final Class<?> objectType, final ConversionHelper helper) { 44 if (value == null) { 45 return null; 46 } 47 return toCharacter(value.toString()); 48 } 49 50 /** 51 * 文字列を{@link Character}に変換します。 52 * 53 * @param value 54 * 文字列 55 * @return 文字列を変換した{@link Character} 56 */ 57 protected Object toCharacter(final String value) { 58 if (value == null || value.length() == 0) { 59 return 0; 60 } 61 return value.charAt(0); 62 } 63 64 /** 65 * {@inheritDoc} 66 */ 67 public String convertToString(final Object value, 68 final ConversionHelper helper) { 69 if (value == null) { 70 return null; 71 } 72 return new String(new char[] { (Character) value }); 73 } 74 75 }