1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.configuration2.convert; 18 19 import java.util.Collection; 20 21 import org.apache.commons.configuration2.ex.ConversionException; 22 import org.apache.commons.configuration2.interpol.ConfigurationInterpolator; 23 24 /** 25 * <p> 26 * An interface defining the possible data type conversions supported by the configuration framework. 27 * </p> 28 * <p> 29 * This interface defines a couple of methods related to different kinds of data type conversion: 30 * </p> 31 * <ul> 32 * <li>Conversion to an object of a specific type</li> 33 * <li>Conversion to an array of a specific type</li> 34 * <li>Conversion to a collection of a specific type</li> 35 * </ul> 36 * <p> 37 * Data type conversion is related to variable substitution (aka interpolation). Before a value can be converted to a 38 * target type substitution has to be performed first, and the conversion is done on the resulting value. In order to 39 * support this, the conversion methods expect a {@link ConfigurationInterpolator} object; {@code Configuration} 40 * implementations here pass in their associated instance. 41 * </p> 42 * <p> 43 * A {@code Configuration} object is associated with a concrete {@code ConversionHandler} implementation. Whenever a 44 * data type conversion is required it delegates to this handler. By providing a custom {@code ConversionHandler} 45 * object, the type conversion performed by the configuration object can be adapted. 46 * </p> 47 * 48 * @since 2.0 49 */ 50 public interface ConversionHandler { 51 /** 52 * Converts a single object to the specified target type. A concrete implementation has to attempt a conversion. If this 53 * is not possible, a {@link ConversionException} is thrown. It is up to a concrete implementation how <strong>null</strong> 54 * values are handled; a default strategy would be to return <strong>null</strong> if the source object is <strong>null</strong>. 55 * 56 * @param <T> the type of the desired result 57 * @param src the object to be converted 58 * @param targetCls the target class of the conversion 59 * @param ci an object for performing variable substitution 60 * @return the converted object 61 * @throws ConversionException if the requested conversion is not possible 62 */ 63 <T> T to(Object src, Class<T> targetCls, ConfigurationInterpolator ci); 64 65 /** 66 * Converts the given object to an array of the specified element type. The object can be a single value (for example a String, 67 * a primitive, etc.) or a complex object containing multiple values (like a collection or another array). In the latter 68 * case all elements contained in the complex object are converted to the target type. If the value(s) cannot be 69 * converted to the desired target class, a {@link ConversionException} is thrown. Note that the result type of this 70 * method is {@code Object}; because this method can also produce arrays of a primitive type the return type 71 * {@code Object[]} cannot be used. 72 * 73 * @param src the object to be converted 74 * @param elemClass the element class of the resulting array 75 * @param ci an object for performing variable substitution 76 * @return the array with the converted values 77 * @throws ConversionException if the conversion of an element is not possible 78 */ 79 Object toArray(Object src, Class<?> elemClass, ConfigurationInterpolator ci); 80 81 /** 82 * Converts the given object to a collection of the specified type. The target collection must be provided (here callers 83 * have the option to specify different types of collections like lists or sets). All values contained in the specified 84 * source object (or the source object itself if it is a single value) are converted to the desired target class and 85 * added to the destination collection. If the conversion of an element is not possible, a {@link ConversionException} 86 * is thrown. 87 * 88 * @param <T> the type of the elements of the destination collection 89 * @param src the object to be converted 90 * @param elemClass the element class of the destination collection 91 * @param ci an object for performing variable substitution 92 * @param dest the destination collection 93 */ 94 <T> void toCollection(Object src, Class<T> elemClass, ConfigurationInterpolator ci, Collection<T> dest); 95 }