io.virtdata.core.DataMapperFunctionMapper Maven / Gradle / Ivy
Show all versions of virtdata-lib-realer Show documentation
package io.virtdata.core;
import io.virtdata.api.FunctionType;
import io.virtdata.api.DataMapper;
import java.util.function.*;
/**
* This class implements an obtuse way of avoiding autoboxing and M:N type
* mapping complexity by way of doublish dispatch. It was preferred over a more
* generalized reflection and annotation-based approach. If it gets too verbose,
* (for some definition of "too"), then it may be refactored.
* The primary goal of this approach is to allow for primitive-level
* lambdas when function are composed together. This will allow for significant
* performance gains when there are only a few steps in a composed function
* which are non-primitive, which is the general case.
* Composition should be supported between all primitive functions
* for types listed in TypeMap, as well as generic functions, with generic
* functions as the last resort.
*/
@SuppressWarnings("unchecked")
public class DataMapperFunctionMapper {
public static DataMapper map(Object function) {
FunctionType functionType = FunctionType.valueOf(function);
switch (functionType) {
case long_double:
return (DataMapper) map((LongToDoubleFunction) function);
case long_int:
return (DataMapper) map((LongToIntFunction) function);
case long_long:
return (DataMapper) map((LongUnaryOperator) function);
case long_T:
return (DataMapper) map((LongFunction) function);
case R_T:
return (DataMapper) map((Function) function);
case int_int:
return (DataMapper) map((IntUnaryOperator) function);
case int_long:
return (DataMapper) map((IntToLongFunction) function);
case int_double:
return (DataMapper) map((IntToDoubleFunction) function);
case int_T:
return (DataMapper) map((IntFunction) function);
case double_double:
return (DataMapper) map((DoubleUnaryOperator) function);
case double_long:
return (DataMapper) map((DoubleToLongFunction) function);
case double_int:
return (DataMapper) map((DoubleToIntFunction) function);
case double_T:
return (DataMapper) map((DoubleFunction) function);
default:
throw new RuntimeException(
"Function object was not a recognized type for mapping to a data mapping lambda: "
+ function.toString());
}
}
public static DataMapper map(DoubleFunction f) {
return (long l) -> f.apply((double) l);
}
public static DataMapper map(DoubleToIntFunction f) {
return f::applyAsInt;
}
public static DataMapper map(DoubleToLongFunction f) {
return f::applyAsLong;
}
public static DataMapper map(DoubleUnaryOperator f) {
return f::applyAsDouble;
}
public static DataMapper map(IntFunction f) {
return (long l) -> f.apply((int) l);
}
public static DataMapper map(IntToDoubleFunction f) {
return (long l) -> (long) f.applyAsDouble((int) l);
}
public static DataMapper map(IntToLongFunction f) {
return (long l) -> f.applyAsLong((int) l);
}
public static DataMapper map(IntUnaryOperator f) {
return (long l) -> f.applyAsInt((int) l);
}
public static DataMapper map(LongToDoubleFunction f) {
return f::applyAsDouble;
}
public static DataMapper map(LongToIntFunction f) {
return f::applyAsInt;
}
public static DataMapper map(LongUnaryOperator f) {
return f::applyAsLong;
}
public static DataMapper map(LongFunction f) {
return f::apply;
}
public static DataMapper map(Function f) {
return f::apply;
}
}