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

hydra.Coders Maven / Gradle / Ivy

package hydra;

import hydra.compute.Coder;
import hydra.compute.Flow;
import hydra.compute.StatelessCoder;
import hydra.core.Unit;

/**
 * Convenience functions for working with Hydra coders.
 */
public class Coders {
    /**
     * Compose two coders, chaining their encode and decode functions.
     */
    public static  Coder compose(
            Coder coder1,
            Coder coder2) {
        return new Coder(
                Flows.compose(coder1.encode, coder2.encode),
                Flows.compose(coder2.decode, coder1.decode));
    }

    /**
     * Compose two stateless coders, chaining their encode and decode functions.
     */
    public static  StatelessCoder composeStateless(
            Coder coder1,
            Coder coder2) {
        return new StatelessCoder<>(
                Flows.compose(coder1.encode, coder2.encode),
                Flows.compose(coder2.decode, coder1.decode));
    }

    /**
     * Find the inverse of a coder.
     */
    public static  Coder inverse(Coder coder) {
        return new Coder<>(coder.decode, coder.encode);
    }

    /**
     * Find the inverse of a stateless coder.
     */
    public static  StatelessCoder inverseStateless(Coder coder) {
        return new StatelessCoder(coder.decode, coder.encode);
    }

    /**
     * Pass an initial value through the Coder's encode function, then back through the decode function.
     */
    public static  Flow roundTrip(Coder coder, V1 initialValue) {
        return Flows.compose(coder.encode, coder.decode).apply(initialValue);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy