All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xwiki.properties.converter.AbstractConverter Maven / Gradle / Ivy

There is a newer version: 16.8.0-rc-1
Show newest version
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.properties.converter;

import java.lang.reflect.Type;

import org.xwiki.component.util.ReflectionUtils;

/**
 * Helper base class for a {@link Converter} component.
 * 

* Commonly a new component is supposed to implements {@link AbstractConverter#convertToString(Object)} and * {@link AbstractConverter#convertToType(Class, Object)}. * * @param the type in which the provided value has to be converted * @version $Id: 1ab948f1f76935985bc9d433afff884dfda7650b $ * @since 2.0M2 */ public abstract class AbstractConverter implements Converter { @Override public G convert(Type targetType, Object sourceValue) { Class sourceType = sourceValue == null ? null : sourceValue.getClass(); G result; if (targetType.equals(String.class)) { // Convert --> String result = (G) ((Class) targetType).cast(convertToString((T) sourceValue)); } else if (targetType.equals(sourceType)) { // No conversion necessary result = (G) sourceValue; } else { // Convert --> Type result = (G) convertToType(targetType, sourceValue); } return result; } /** * Convert the input object into a String. *

* N.B.This implementation simply uses the value's toString() method and should be overridden if * a more sophisticated mechanism for conversion to a String is required. * * @param value The input value to be converted. * @return the converted String value. */ protected String convertToString(T value) { return value.toString(); } /** * Convert the input object into an output object of the specified type. *

* Typical implementations will provide a minimum of {@code String --> type} conversion. * * @param the type in which the provided value has o be converted * @param targetType Data type to which this value should be converted. * @param value The input value to be converted. * @return The converted value. * @since 3.0M1 */ protected G convertToType(Type targetType, Object value) { Class clazz = ReflectionUtils.getTypeClass(targetType); // Call #convertToType(Class type, Object value) for retro-compatibility return convertToType(clazz, value); } /** * Convert the input object into an output object of the specified type. *

* Typical implementations will provide a minimum of {@code String --> type} conversion. * * @param the type in which the provided value has o be converted * @param type Data type to which this value should be converted. * @param value The input value to be converted. * @return The converted value. * @deprecated since 3.0M1 overwrite {@link #convertToType(Type, Object)} instead */ @Deprecated protected G convertToType(Class type, Object value) { return convertToType((Type) type, value); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy