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

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

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

import java.util.ArrayList;
import java.util.List;

import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;
import net.sf.gluebooster.java.booster.essentials.utils.ReflectionBoostUtils;

/**
 * Stores parameters in other objects
 * 
 * @defaultParamText indexObjectProperty index and the corresponding object and property: index1, object1, property1, index2, object2, property2, ...
 * @defaultParamText name name of the callable
 * 
 * @author cbauer
 *
 */
public class CallableBySetting extends CallableAbstraction {

	/**
	 * Configuration of the setters. Each entry states that the {property} of {object} is to be set to the value of parameter[{index}].
	 */
	private List configuration = new ArrayList();

	/**
	 * Should the first object of the configuration be returned
	 */
	private boolean returnFirstObject = false;

	public CallableBySetting(String name, Object... indexObjectProperty) {
		super(name);
		for (int i = 0; i < indexObjectProperty.length; i = i + 3) {
			configuration.add(new Object[] { indexObjectProperty[i], indexObjectProperty[i + 1], indexObjectProperty[i + 2] });
		}
	}

	/**
	 * Create a callables that sets values and returns the object of the first indexObjectProperty
	 * 
	 * @return the created callable
	 */
	public static CallableAbstraction createWithReturnFirstObject(String name, Object... indexObjectProperty) {
		CallableBySetting result = new CallableBySetting(name, indexObjectProperty);
		result.setReturnFirstObject(true);
		return result;
	}

	@Override
	public Object callImpl(Object... parameters) throws Exception {

		for (Object[] indexObjectProperty : configuration) {
			Integer index = (Integer) indexObjectProperty[0];
			Object object = indexObjectProperty[1];
			String property = (String) indexObjectProperty[2];

			ReflectionBoostUtils.setProperty(object, property, parameters[index]);
		}

		if (returnFirstObject) {
			return configuration.get(0)[1];
		} else {
			return parameters;
		}
	}

	public boolean isReturnFirstObject() {
		return returnFirstObject;
	}

	public void setReturnFirstObject(boolean returnFirstObject) {
		this.returnFirstObject = returnFirstObject;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy