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

net.sf.gluebooster.java.booster.basic.transformation.CallableClassConverter Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.transformation;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Iterator;

import net.sf.gluebooster.java.booster.basic.container.IteratorOfContainer;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;
import net.sf.gluebooster.java.booster.essentials.utils.Constants;

/**
 * Transforms an object into an instance of a given class.
 * 
 * @defaultParamText name name of the callable
 * @defaultParamText targetClass the resultclass
 * 
 * @param 
 *            the result of the callable
 */
public class CallableClassConverter extends CallableAbstraction {

	/**
	 * The type to convert to a reader
	 */
	public static final CallableAbstraction TO_READER = new CallableClassConverter(Reader.class);

	/**
	 * The type to convert to a buffered reader
	 */
	public static final CallableAbstraction TO_BUFFEREDREADER = new CallableClassConverter(BufferedReader.class);

	/**
	 * The type to convert to a iterator with lines.
	 */
	public static final CallableAbstraction TO_LINES = createLineIterator("to text lines");


	/**
	 * Used by tests.
	 */
	private CallableClassConverter() {

	}

	public CallableClassConverter(Class targetClass) {
		this(null, targetClass);
	}

	public CallableClassConverter(Object name, Class targetClass)
	{
		super(name == null ? "convert to " + targetClass.getSimpleName() : "" + name + " (->" + targetClass.getSimpleName() + ")", targetClass);
	}

	/**
	 * 
	 * @param subtype
	 *            subtype of the instance
	 */
	private CallableClassConverter(Object name, Class targetClass, Object subtype) {
		this(name, targetClass);
		setSubtype(subtype);
	}

	/**
	 * Create a callable that creates a line iterator
	 * 
	 * @return the created callable
	 */
	public static CallableClassConverter createLineIterator(Object name) {
		return new CallableClassConverter(name, Iterator.class, Constants.LINE);
	}


	@Override
	protected Result callImpl(Object... parameters) throws Exception {
		Class type = getType();
		Object input = parameters[0];
		if (input == null) {
			return null;
		} else if (type.isInstance(input)) {
			return (Result) input;
		} else if (Iterator.class.equals(type)) {
			if (input instanceof Collection) {
				return (Result) ((Collection) input).iterator();
			} else if (Constants.LINE.is(getSubtype())) {
				return (Result) IteratorOfContainer.createLineIterator("to lines", input);
			} else {
				throw notSupported(input);
			}
		} else if (Reader.class.equals(type)) {
			if (input instanceof CharSequence) {
				return (Result) new StringReader(input.toString());
			} else if (input instanceof InputStream) {
				return (Result) new InputStreamReader((InputStream) input);
			} else {
				throw notSupported(input);
			}
		} else if (BufferedReader.class.equals(type)) {
			return (Result) new BufferedReader(TO_READER.call(input));

		} else {
			// try construtor with one arg
			for (Constructor constructor : type.getConstructors()) {
				Class[] parameterTypes = constructor.getParameterTypes();
				if (parameterTypes != null && parameterTypes.length == 1) {
					if (parameterTypes[0].isInstance(input)) {
						return (Result) constructor.newInstance(input);
					}
				}
			}

			throw notSupported(null);
		}
	}

	/**
	 * Throw a exception that the input is not supported.
	 * 
	 * @param input
	 *            the unsupported object
	 * @return nothing
	 * @throws RuntimeException
	 *             always throws a runtime exception
	 */
	private Exception notSupported(Object input) {
		Class type = getType();
		if (input == null) {
			throw new IllegalStateException("type not supported " + type.getName());
		} else {
			throw new IllegalStateException("input not supported for type " + type + ": " + input.getClass().getName());
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy