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

net.entropysoft.transmorph.converters.ObjectToFormattedString Maven / Gradle / Ivy

Go to download

Java library to convert a Java object of one type into an object of another type (with another signature, possibly parameterized).

There is a newer version: 3.1.3
Show newest version
package net.entropysoft.transmorph.converters;

import java.text.Format;

import net.entropysoft.transmorph.ConversionContext;
import net.entropysoft.transmorph.ConverterException;
import net.entropysoft.transmorph.type.TypeReference;

/**
 * Converter used to convert an object to a formatted string
 * 
 * @author Cedric Chabanois (cchabanois at gmail.com)
 * 
 */
public class ObjectToFormattedString extends AbstractContainerConverter {

	private Format format;
	private Class sourceClass;
	private Class formatExpectedClass;

	public ObjectToFormattedString() {
		this.useObjectPool = true;
	}

	/**
	 * 
	 * @param sourceClass
	 *            class of the objects we want to format. Must be supported by
	 *            given Format
	 * @param format
	 *            format to use
	 */
	public ObjectToFormattedString(Class sourceClass, Format format) {
		this.useObjectPool = true;
		this.sourceClass = sourceClass;
		this.formatExpectedClass = sourceClass;
		this.format = format;
	}

	/**
	 * 
	 * @param sourceClass
	 *            the class of the object we want to format
	 * @param formatExpectedClass
	 *            class supported by given Format
	 * @param format
	 *            format to use
	 */
	public ObjectToFormattedString(Class sourceClass,
			Class formatExpectedClass, Format format) {
		this.useObjectPool = true;
		this.sourceClass = sourceClass;
		this.formatExpectedClass = formatExpectedClass;
		this.format = format;
	}

	public Format getFormat() {
		return format;
	}

	public Object doConvert(ConversionContext context, Object sourceObject,
			TypeReference destinationType) throws ConverterException {
		if (sourceObject == null) {
			return null;
		}

		Object formatSource = sourceObject;
		if (sourceClass != formatExpectedClass) {
			formatSource = elementConverter.convert(context, sourceObject,
					TypeReference.get(formatExpectedClass));
		}
		return format.format(formatSource);
	}

	protected boolean canHandleDestinationType(TypeReference destinationType) {
		return destinationType.isType(String.class);
	}

	protected boolean canHandleSourceObject(Object sourceObject) {
		if (sourceObject == null) {
			return true;
		}
		return sourceClass.isInstance(sourceObject);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy