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

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

Go to download

Basic classes to support the development of applications. There should be as few dependencies on other frameworks as possible.

The newest version!
package net.sf.gluebooster.java.booster.basic.transformation;

import java.util.HashMap;
import java.util.Map;

import net.sf.gluebooster.java.booster.essentials.eventsCommands.Callable;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;

/**
 * Selects a callable and invokes its call.
 * 
 * @author cbauer
 *
 * @param 
 *            Parameter of the call
 * @param 
 *            Result of the call
 */
public class CallableBySelectingACallable extends CallableAbstraction {

	/**
	 * The default callable if none could be selected
	 */
	private Callable defaultCallable;

	/**
	 * The callables to select from.
	 */
	private Map> callables = new HashMap();

	/**
	 * A callable to generate the key for the callables map.
	 */
	private Callable keyGenerator;

	/**
	 * 
	 * @param keyCallables
	 *            key and the corresponding callable: key1, callable1, key2, callable2, ...
	 */
	public CallableBySelectingACallable(Callable keyGenerator, Callable defaultCallable, Object... keyCallables) {
		this.keyGenerator = keyGenerator;
		this.defaultCallable = defaultCallable;
		if (keyCallables != null) {
			for (int i = 0; i < keyCallables.length; i = i + 2) {
				callables.put(keyCallables[i], (Callable) keyCallables[i + 1]);
			}
		}
	}

	@Override
	protected Result callImpl(Parameter... parameters) throws Exception {
		Object key = keyGenerator.call(parameters);
		Callable callable;
		if (callables.containsKey(key)) {
			callable = callables.get(key);
		} else {
			callable = defaultCallable;
		}

		return callable.call(parameters);
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy