/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.functional;

import java.io.Serializable;
import java.util.function.Function;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public interface ProjectionFunction<T,R> extends Function<T,R>, CoFunction<T,R> {

    static <R> ProjectionFunction<R,R> identity() {
        return produce(v->v, v->v);
    }
    
    static <T,R> ProjectionFunction<T,R> produce(Function<T,R> f, CoFunction<T,R> cf) {
        class PF implements ProjectionFunction<T,R>, Serializable {
			
			private static final long serialVersionUID = 1L;
			
            @Override
            public R apply(T value) {
                return f.apply(value);
            }

            @Override
            public T invert(R value) {
                return cf.invert(value);
            }
        };
		return new PF();
    }
    
}
