001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.beanutils2.converters; 018 019/** 020 * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from <strong>java.lang.Character</strong> objects. 021 * <p> 022 * Can be configured to either return a <em>default value</em> or throw a {@code ConversionException} if a conversion error occurs. 023 * 024 * @since 1.3 025 */ 026public final class CharacterConverter extends AbstractConverter<Character> { 027 028 /** 029 * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs. 030 */ 031 public CharacterConverter() { 032 } 033 034 /** 035 * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that returns a default value if an error occurs. 036 * 037 * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value. 038 */ 039 public CharacterConverter(final char defaultValue) { 040 super(defaultValue); 041 } 042 043 /** 044 * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that returns a default value if an error occurs. 045 * 046 * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value. 047 */ 048 public CharacterConverter(final Character defaultValue) { 049 super(defaultValue); 050 } 051 052 /** 053 * <p> 054 * Converts a java.lang.Class or object into a String. 055 * </p> 056 * 057 * @param value The input value to be converted 058 * @return the converted String value. 059 * @since 1.8.0 060 */ 061 @Override 062 protected String convertToString(final Object value) { 063 final String strValue = value.toString(); 064 return strValue.isEmpty() ? "" : strValue.substring(0, 1); 065 } 066 067 /** 068 * <p> 069 * Converts the input object into a java.lang.Character. 070 * </p> 071 * 072 * @param <T> Target type of the conversion. 073 * @param type Data type to which this value should be converted. 074 * @param value The input value to be converted. 075 * @return The converted value. 076 * @throws Exception if conversion cannot be performed successfully 077 * @since 1.8.0 078 */ 079 @Override 080 protected <T> T convertToType(final Class<T> type, final Object value) throws Exception { 081 if (Character.class.equals(type) || Character.TYPE.equals(type)) { 082 return type.cast(Character.valueOf(value.toString().charAt(0))); 083 } 084 085 throw conversionException(type, value); 086 } 087 088 /** 089 * Gets the default type this {@code Converter} handles. 090 * 091 * @return The default type this {@code Converter} handles. 092 * @since 1.8.0 093 */ 094 @Override 095 protected Class<Character> getDefaultType() { 096 return Character.class; 097 } 098 099}