    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 converts an incoming object into a {@link String} object.
    021 * <p>
    022 * Note that ConvertUtils really is designed to do string-&gt;object conversions, and offers very little support for object-&gt;string conversions. The
    023 * ConvertUtils/ConvertUtilsBean methods only select a converter to apply based upon the target type being converted to, and generally assume that the input is
    024 * a string (by calling its toString method if needed).
    025 * <p>
    026 * This class is therefore just a dummy converter that converts its input into a string by calling the input object's toString method and returning that value.
    027 * <p>
    028 * It is possible to replace this converter with something that has a big if/else statement that selects behavior based on the real type of the object being
    029 * converted (or possibly has a map of converters, and looks them up based on the class of the input object). However this is not part of the existing
    030 * ConvertUtils framework.
    031 *
    032 *
    033 * @since 1.3
    034 */
    035public final class StringConverter extends AbstractConverter<String> {
    036
    037    /**
    038     * Constructs a <strong>java.lang.String</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
    039     */
    040    public StringConverter() {
    041    }
    042
    043    /**
    044     * Constructs a <strong>java.lang.String</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 StringConverter(final String defaultValue) {
    049        super(defaultValue);
    050    }
    051
    052    /**
    053     * Convert the specified input object into an output object of the specified type.
    054     *
    055     * @param <T>   Target type of the conversion.
    056     * @param type  Data type to which this value should be converted.
    057     * @param value The input value to be converted.
    058     * @return The converted value.
    059     * @throws Throwable if an error occurs converting to the specified type
    060     * @since 1.8.0
    061     */
    062    @Override
    063    protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
    064        // We have to support Object, too, because this class is sometimes
    065        // used for a standard to Object conversion
    066        if (String.class.equals(type) || Object.class.equals(type)) {
    067            return type.cast(value.toString());
    068        }
    069        throw conversionException(type, value);
    070    }
    071
    072    /**
    073     * Gets the default type this {@code Converter} handles.
    074     *
    075     * @return The default type this {@code Converter} handles.
    076     * @since 1.8.0
    077     */
    078    @Override
    079    protected Class<String> getDefaultType() {
    080        return String.class;
    081    }
    082
    083}