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.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     * Converts a single object to the specified target type. A concrete implementation has to attempt a conversion. If this
053     * is not possible, a {@link ConversionException} is thrown. It is up to a concrete implementation how <b>null</b>
054     * values are handled; a default strategy would be to return <b>null</b> if the source object is <b>null</b>.
055     *
056     * @param <T> the type of the desired result
057     * @param src the object to be converted
058     * @param targetCls the target class of the conversion
059     * @param ci an object for performing variable substitution
060     * @return the converted object
061     * @throws ConversionException if the requested conversion is not possible
062     */
063    <T> T to(Object src, Class<T> targetCls, ConfigurationInterpolator ci);
064
065    /**
066     * Converts the given object to an array of the specified element type. The object can be a single value (e.g. a String,
067     * a primitive, etc.) or a complex object containing multiple values (like a collection or another array). In the latter
068     * case all elements contained in the complex object are converted to the target type. If the value(s) cannot be
069     * converted to the desired target class, a {@link ConversionException} is thrown. Note that the result type of this
070     * method is {@code Object}; because this method can also produce arrays of a primitive type the return type
071     * {@code Object[]} cannot be used.
072     *
073     * @param src the object to be converted
074     * @param elemClass the element class of the resulting array
075     * @param ci an object for performing variable substitution
076     * @return the array with the converted values
077     * @throws ConversionException if the conversion of an element is not possible
078     */
079    Object toArray(Object src, Class<?> elemClass, ConfigurationInterpolator ci);
080
081    /**
082     * Converts the given object to a collection of the specified type. The target collection must be provided (here callers
083     * have the option to specify different types of collections like lists or sets). All values contained in the specified
084     * source object (or the source object itself if it is a single value) are converted to the desired target class and
085     * added to the destination collection. If the conversion of an element is not possible, a {@link ConversionException}
086     * is thrown.
087     *
088     * @param <T> the type of the elements of the destination collection
089     * @param src the object to be converted
090     * @param elemClass the element class of the destination collection
091     * @param ci an object for performing variable substitution
092     * @param dest the destination collection
093     */
094    <T> void toCollection(Object src, Class<T> elemClass, ConfigurationInterpolator ci, Collection<T> dest);
095}