    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.beanutils.converters;
    018
    019/**
    020 * {@link org.apache.commons.beanutils.Converter} implementation that handles conversion
    021 * to and from <strong>java.lang.Character</strong> objects.
    022 * <p>
    023 * Can be configured to either return a <em>default value</em> or throw a
    024 * <code>ConversionException</code> if a conversion error occurs.
    025 *
    026 * @since 1.3
    027 */
    028public final class CharacterConverter extends AbstractConverter {
    029
    030    /**
    031     * Construct a <strong>java.lang.Character</strong> <em>Converter</em> that throws
    032     * a <code>ConversionException</code> if an error occurs.
    033     */
    034    public CharacterConverter() {
    035    }
    036
    037    /**
    038     * Construct a <strong>java.lang.Character</strong> <em>Converter</em> that returns
    039     * a default value if an error occurs.
    040     *
    041     * @param defaultValue The default value to be returned
    042     * if the value to be converted is missing or an error
    043     * occurs converting the value.
    044     */
    045    public CharacterConverter(final Object defaultValue) {
    046        super(defaultValue);
    047    }
    048
    049    /**
    050     * <p>Convert a java.lang.Class or object into a String.</p>
    051     *
    052     * @param value The input value to be converted
    053     * @return the converted String value.
    054     * @since 1.8.0
    055     */
    056    @Override
    057    protected String convertToString(final Object value) {
    058        final String strValue = value.toString();
    059        return strValue.length() == 0 ? "" : strValue.substring(0, 1);
    060    }
    061
    062    /**
    063     * <p>Convert the input object into a java.lang.Character.</p>
    064     *
    065     * @param <T> Target type of the conversion.
    066     * @param type Data type to which this value should be converted.
    067     * @param value The input value to be converted.
    068     * @return The converted value.
    069     * @throws Exception if conversion cannot be performed successfully
    070     * @since 1.8.0
    071     */
    072    @Override
    073    protected <T> T convertToType(final Class<T> type, final Object value) throws Exception {
    074        if (Character.class.equals(type) || Character.TYPE.equals(type)) {
    075            return type.cast(Character.valueOf(value.toString().charAt(0)));
    076        }
    077
    078        throw conversionException(type, value);
    079    }
    080
    081    /**
    082     * Return the default type this <code>Converter</code> handles.
    083     *
    084     * @return The default type this <code>Converter</code> handles.
    085     * @since 1.8.0
    086     */
    087    @Override
    088    protected Class<?> getDefaultType() {
    089        return Character.class;
    090    }
    091
    092}