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

org.databene.commons.converter.AnyConverter Maven / Gradle / Ivy

/*
 * Copyright (C) 2004-2014 Volker Bergmann ([email protected]).
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.databene.commons.converter;

import org.databene.commons.ConversionException;
import org.databene.commons.Converter;
import org.databene.commons.Patterns;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Converts any source type to any target type. It also makes use of the ConverterManager.
 * Created: 16.06.2007 11:34:42
 * @author Volker Bergmann
 */
public class AnyConverter extends FormatHolder implements Converter {
	
    private static final Logger logger = LoggerFactory.getLogger(AnyConverter.class);

    private Class targetType;
    
    public AnyConverter(Class targetType) {
        this(targetType, Patterns.DEFAULT_DATE_PATTERN);
    }

    public AnyConverter(Class targetType, String datePattern) {
    	this.targetType = targetType;
        this.datePattern = datePattern;
    }

    @Override
	public Class getSourceType() {
        return Object.class;
    }
    
    @Override
	public Class getTargetType() {
	    return targetType;
    }

	@Override
	public E convert(Object sourceValue) throws ConversionException {
        return convert(sourceValue, targetType, datePattern, timePattern, timestampPattern);
    }

	@Override
	public boolean isParallelizable() {
	    return true;
    }

	@Override
	public boolean isThreadSafe() {
	    return true;
    }
	
    public static  TT convert(Object source, Class targetType) throws ConversionException {
        return convert(source, targetType, null, null, null);
    }
    
    /**
     * Converts an object of a given type to an object of the target type.
     * @param source the object to convert
     * @param targetType the target type of the conversion
     * @return an object of the target type
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static  TT convert(Object source, Class targetType, String datePattern, 
    		String timePattern, String timestampPattern) throws ConversionException {
        if (logger.isDebugEnabled())
            logger.debug("Converting " + source + (source != null ? " (" + source.getClass().getName() + ")" : "") + " to " + targetType);
    	if (source == null || targetType.equals(source.getClass()))
    		return (TT) source;
        Converter converter = ConverterManager.getInstance().createConverter(source.getClass(), targetType);
		return (TT) converter.convert(source);
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + '(' + targetType.getSimpleName() + ')';
    }
    
}