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

com.jnape.palatable.lambda.functor.builtin.Exchange Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
package com.jnape.palatable.lambda.functor.builtin;

import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functor.Profunctor;
import com.jnape.palatable.lambda.optics.Iso;

/**
 * A profunctor used to extract the isomorphic functions an {@link Iso} is composed of.
 *
 * @param  the smaller viewed value of an {@link Iso}
 * @param  the smaller viewing value of an {@link Iso}
 * @param  the larger viewing value of an {@link Iso}
 * @param  the larger viewed value of an {@link Iso}
 */
public final class Exchange implements Profunctor> {
    private final Fn1 sa;
    private final Fn1 bt;

    public Exchange(Fn1 sa, Fn1 bt) {
        this.sa = sa;
        this.bt = bt;
    }

    /**
     * Extract the mapping S -> A.
     *
     * @return an {@link Fn1}<S, A>
     */
    public Fn1 sa() {
        return sa;
    }

    /**
     * Extract the mapping B -> T.
     *
     * @return an {@link Fn1}<B, T>
     */
    public Fn1 bt() {
        return bt;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public  Exchange diMap(Fn1 lFn,
                                             Fn1 rFn) {
        return new Exchange<>(lFn.fmap(sa), bt.fmap(rFn));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public  Exchange diMapL(Fn1 fn) {
        return (Exchange) Profunctor.super.diMapL(fn);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public  Exchange diMapR(Fn1 fn) {
        return (Exchange) Profunctor.super.diMapR(fn);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public  Exchange contraMap(Fn1 fn) {
        return (Exchange) Profunctor.super.contraMap(fn);
    }
}