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

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

Go to download

'databene commons' is an open source Java library by Volker Bergmann. It provides extensions to the Java core library by utility classes, abstract concepts and concrete implementations.

There is a newer version: 1.0.11
Show newest version
/*
 * Copyright (C) 2004-2015 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
 * @param  the type to by checked by this validator
 * @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
     * @param datePattern the date pattern to apply
     * @param timePattern the time pattern to apply
     * @param timestampPattern the timestamp pattern to apply
     * @param  the target type
     * @return an object of the target type
     * @throws ConversionException if conversion fails 
     */
    @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() + ')';
    }
    
}