hydra.Adapters Maven / Gradle / Ivy
package hydra;
import hydra.compute.Adapter;
import hydra.compute.Flow;
import hydra.compute.StatelessAdapter;
import hydra.core.Unit;
import java.util.function.Function;
/**
* Convenience functions for working with Hydra coders.
*/
public class Adapters {
/**
* Compose two adapters, in turn composing their coders.
*/
public static Adapter compose(
Adapter adapter1,
Adapter adapter2) {
return new Adapter<>(
adapter1.isLossy || adapter2.isLossy,
adapter1.source,
adapter2.target,
Coders.compose(adapter1.coder, adapter2.coder));
}
/**
* Compose two adapters constructed from their source types, in turn composing their coders.
*/
public static Function>> compose(
Function>> constructor1,
Function>> constructor2) {
return t1 -> Flows.bind(
constructor1.apply(t1),
firstMile -> Flows.map(
constructor2.apply(firstMile.target),
lastMile -> compose(firstMile, lastMile)));
}
/**
* Compose two stateless adapters, in turn composing their coders.
*/
public static StatelessAdapter composeStateless(
StatelessAdapter adapter1,
StatelessAdapter adapter2) {
return new StatelessAdapter<>(
adapter1.isLossy || adapter2.isLossy,
adapter1.source,
adapter2.target,
Coders.composeStateless(adapter1.coder, adapter2.coder));
}
/**
* Compose two stateless adapters constructed from their source types, in turn composing their coders.
*/
public static Function>> composeStateless(
Function>> constructor1,
Function>> constructor2) {
return t1 -> Flows.bind(
constructor1.apply(t1),
firstMile -> Flows.map(
constructor2.apply(firstMile.target),
lastMile -> composeStateless(firstMile, lastMile)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy