Source code
Description: source: package: org.apache.commons.configuration2.convert, interface: ConversionHandler

    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 *     https://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.configuration2.convert;
    018
    019import java.util.Collection;
    020
    021import org.apache.commons.configuration2.ex.ConversionException;
    022import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
    023
    024/**
    025 * <p>
    026 * An interface defining the possible data type conversions supported by the configuration framework.
    027 * </p>
    028 * <p>
    029 * This interface defines a couple of methods related to different kinds of data type conversion:
    030 * </p>
    031 * <ul>
    032 * <li>Conversion to an object of a specific type</li>
    033 * <li>Conversion to an array of a specific type</li>
    034 * <li>Conversion to a collection of a specific type</li>
    035 * </ul>
    036 * <p>
    037 * Data type conversion is related to variable substitution (aka interpolation). Before a value can be converted to a
    038 * target type substitution has to be performed first, and the conversion is done on the resulting value. In order to
    039 * support this, the conversion methods expect a {@link ConfigurationInterpolator} object; {@code Configuration}
    040 * implementations here pass in their associated instance.
    041 * </p>
    042 * <p>
    043 * A {@code Configuration} object is associated with a concrete {@code ConversionHandler} implementation. Whenever a
    044 * data type conversion is required it delegates to this handler. By providing a custom {@code ConversionHandler}
    045 * object, the type conversion performed by the configuration object can be adapted.
    046 * </p>
    047 *
    048 * @since 2.0
    049 */
    050public interface ConversionHandler {
    051
    052    /**
    053     * Converts a single object to the specified target type. A concrete implementation has to attempt a conversion. If this
    054     * is not possible, a {@link ConversionException} is thrown. It is up to a concrete implementation how <strong>null</strong>
    055     * values are handled; a default strategy would be to return <strong>null</strong> if the source object is <strong>null</strong>.
    056     *
    057     * @param <T> the type of the desired result
    058     * @param src the object to be converted
    059     * @param targetCls the target class of the conversion
    060     * @param ci an object for performing variable substitution
    061     * @return the converted object
    062     * @throws ConversionException if the requested conversion is not possible
    063     */
    064    <T> T to(Object src, Class<T> targetCls, ConfigurationInterpolator ci);
    065
    066    /**
    067     * Converts the given object to an array of the specified element type. The object can be a single value (for example a String,
    068     * a primitive, etc.) or a complex object containing multiple values (like a collection or another array). In the latter
    069     * case all elements contained in the complex object are converted to the target type. If the value(s) cannot be
    070     * converted to the desired target class, a {@link ConversionException} is thrown. Note that the result type of this
    071     * method is {@code Object}; because this method can also produce arrays of a primitive type the return type
    072     * {@code Object[]} cannot be used.
    073     *
    074     * @param src the object to be converted
    075     * @param elemClass the element class of the resulting array
    076     * @param ci an object for performing variable substitution
    077     * @return the array with the converted values
    078     * @throws ConversionException if the conversion of an element is not possible
    079     */
    080    Object toArray(Object src, Class<?> elemClass, ConfigurationInterpolator ci);
    081
    082    /**
    083     * Converts the given object to a collection of the specified type. The target collection must be provided (here callers
    084     * have the option to specify different types of collections like lists or sets). All values contained in the specified
    085     * source object (or the source object itself if it is a single value) are converted to the desired target class and
    086     * added to the destination collection. If the conversion of an element is not possible, a {@link ConversionException}
    087     * is thrown.
    088     *
    089     * @param <T> the type of the elements of the destination collection
    090     * @param src the object to be converted
    091     * @param elemClass the element class of the destination collection
    092     * @param ci an object for performing variable substitution
    093     * @param dest the destination collection
    094     */
    095    <T> void toCollection(Object src, Class<T> elemClass, ConfigurationInterpolator ci, Collection<T> dest);
    096}