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

org.decision_deck.jmcda.utils.FunctionUtils Maven / Gradle / Ivy

Go to download

The base classes of the J-MCDA project. Contains the main structure classes that define MCDA concepts such as alternatives and performance matrixes.

The newest version!
package org.decision_deck.jmcda.utils;

import org.decisiondeck.jmcda.exc.FunctionWithInputCheck;
import org.decisiondeck.jmcda.exc.InvalidInputException;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;

public class FunctionUtils {

    static public  FunctionWithInputCheck compose(final Function g,
	    final FunctionWithInputCheck f) {
	final FunctionWithInputCheck functionWithInputCheck = functionWithInputCheck(g);
	return compose(functionWithInputCheck, f);
    }

    static public  FunctionWithInputCheck compose(final FunctionWithInputCheck g,
	    final Function f) {
	final FunctionWithInputCheck functionWithInputCheck = functionWithInputCheck(f);
	final FunctionWithInputCheck composed = compose(g, functionWithInputCheck);
	return composed;
    }

    static public  FunctionWithInputCheck compose(final FunctionWithInputCheck g,
	    final FunctionWithInputCheck f) {
	return new FunctionWithInputCheck() {
	    @Override
	    public C apply(A input) throws InvalidInputException {
		final B intermediary = f.apply(input);
		try {
		    return g.apply(intermediary);
		} catch (InvalidInputException exc) {
		    throw new InvalidInputException("Exception while attempting to transform input " + input + ".", exc);
		}
	    }
	};
    }

    /**
     * Transforms a function into a function with input check. This is mainly intended as a technical method to provide
     * type compatibility where a function with input check is expected instead of a function. The returned function
     * does not throw {@link InvalidInputException}s.
     * 
     * @param 
     *            the domain of the given function: the type of value it expects.
     * @param 
     *            the codomain of the given function: the type of value it returns.
     * @param 
     *            the domain of the returned function: the type of value it expects.
     * @param 
     *            the codomain of the returned function: the type of value it returns.
     * @param f
     *            not null.
     * @return not null.
     */
    static public  FunctionWithInputCheck functionWithInputCheck(
	    final Function f) {
	Preconditions.checkNotNull(f);
	return new FunctionWithInputCheck() {
	    @Override
	    public V2 apply(F2 input) {
		return f.apply(input);
	    }
	};
    }

    static public  FunctionWithInputCheck constant(V value) {
	return functionWithInputCheck(Functions.constant(value));
    }

    /**
     * @param 
     *            the input and output type of the function.
     * @return the identity function.
     */
    static public  FunctionWithInputCheck identity() {
	return functionWithInputCheck(Functions. identity());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy