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

io.deephaven.kafka.BoxTransform Maven / Gradle / Ivy

There is a newer version: 0.37.1
Show newest version
/**
 * Copyright (c) 2016-2023 Deephaven Data Labs and Patent Pending
 */
package io.deephaven.kafka;

import io.deephaven.function.ToBooleanFunction;
import io.deephaven.function.ToByteFunction;
import io.deephaven.function.ToCharFunction;
import io.deephaven.function.ToDoubleFunction;
import io.deephaven.function.ToFloatFunction;
import io.deephaven.function.ToIntFunction;
import io.deephaven.function.ToLongFunction;
import io.deephaven.function.ToObjectFunction;
import io.deephaven.function.ToPrimitiveFunction;
import io.deephaven.function.ToShortFunction;
import io.deephaven.function.TypedFunction;
import io.deephaven.qst.type.BoxedBooleanType;
import io.deephaven.qst.type.BoxedByteType;
import io.deephaven.qst.type.BoxedCharType;
import io.deephaven.qst.type.BoxedDoubleType;
import io.deephaven.qst.type.BoxedFloatType;
import io.deephaven.qst.type.BoxedIntType;
import io.deephaven.qst.type.BoxedLongType;
import io.deephaven.qst.type.BoxedShortType;
import io.deephaven.util.type.TypeUtils;

class BoxTransform {

    /**
     * Creates the function composition {@code box ∘ f}.
     *
     * 

* For primitive functions {@code f}, see {@link #of(ToPrimitiveFunction)}. * *

* For object functions {@code f}, {@code box} is the identity function (and {@code f} will simply be returned). * * @param f the function * @return the object function * @param the input type * @see #of(ToBooleanFunction) * @see #of(ToCharFunction) * @see #of(ToByteFunction) * @see #of(ToShortFunction) * @see #of(ToIntFunction) * @see #of(ToLongFunction) * @see #of(ToFloatFunction) * @see #of(ToDoubleFunction) */ public static ToObjectFunction of(TypedFunction f) { return BoxedVisitor.of(f); } /** * Creates the function composition {@code box ∘ f}. * *

* The returned object function will have return type {@code f.returnType().boxedType()}. * * @param f the primitive function * @return the object function * @param the input type * @see #of(ToBooleanFunction) * @see #of(ToCharFunction) * @see #of(ToByteFunction) * @see #of(ToShortFunction) * @see #of(ToIntFunction) * @see #of(ToLongFunction) * @see #of(ToFloatFunction) * @see #of(ToDoubleFunction) */ public static ToObjectFunction of(ToPrimitiveFunction f) { return BoxedVisitor.of(f); } /** * Creates the function composition {@code box ∘ f}, where {@code box} is simply a cast from {@code boolean} to * {@code Boolean}. * *

* Equivalent to {@code x -> (Boolean)f.test(x)}. * * @param f the boolean function * @return the object function * @param the input type */ public static ToObjectFunction of(ToBooleanFunction f) { return ToObjectFunction.of(f::test, BoxedBooleanType.of()); } /** * Creates the function composition {@code box ∘ f}. * *

* Equivalent to {@code x -> TypeUtils.box(f.applyAsChar(x))}. * * @param f the char function * @return the object function * @param the input type * @see TypeUtils#box(char) */ public static ToObjectFunction of(ToCharFunction f) { return ToObjectFunction.of(t -> box(f, t), BoxedCharType.of()); } /** * Creates the function composition {@code box ∘ f}. * *

* Equivalent to {@code x -> TypeUtils.box(f.applyAsByte(x))}. * * @param f the byte function * @return the object function * @param the input type * @see TypeUtils#box(byte) */ public static ToObjectFunction of(ToByteFunction f) { return ToObjectFunction.of(t -> box(f, t), BoxedByteType.of()); } /** * Creates the function composition {@code box ∘ f}. * *

* Equivalent to {@code x -> TypeUtils.box(f.applyAsShort(x))}. * * @param f the short function * @return the object function * @param the input type * @see TypeUtils#box(short) */ public static ToObjectFunction of(ToShortFunction f) { return ToObjectFunction.of(t -> box(f, t), BoxedShortType.of()); } /** * Creates the function composition {@code box ∘ f}. * *

* Equivalent to {@code x -> TypeUtils.box(f.applyAsInt(x))}. * * @param f the int function * @return the object function * @param the input type * @see TypeUtils#box(int) */ public static ToObjectFunction of(ToIntFunction f) { return ToObjectFunction.of(t -> box(f, t), BoxedIntType.of()); } /** * Creates the function composition {@code box ∘ f}. * *

* Equivalent to {@code x -> TypeUtils.box(f.applyAsLong(x))}. * * @param f the long function * @return the object function * @param the input type * @see TypeUtils#box(long) */ public static ToObjectFunction of(ToLongFunction f) { return ToObjectFunction.of(t -> box(f, t), BoxedLongType.of()); } /** * Creates the function composition {@code box ∘ f}. * *

* Equivalent to {@code x -> TypeUtils.box(f.applyAsFloat(x))}. * * @param f the float function * @return the object function * @param the input type * @see TypeUtils#box(float) */ public static ToObjectFunction of(ToFloatFunction f) { return ToObjectFunction.of(t -> box(f, t), BoxedFloatType.of()); } /** * Creates the function composition {@code box ∘ f}. * *

* Equivalent to {@code x -> TypeUtils.box(f.applyAsDouble(x))}. * * @param f the double function * @return the object function * @param the input type * @see TypeUtils#box(double) */ public static ToObjectFunction of(ToDoubleFunction f) { return ToObjectFunction.of(t -> box(f, t), BoxedDoubleType.of()); } private enum BoxedVisitor implements TypedFunction.Visitor>, ToPrimitiveFunction.Visitor> { INSTANCE; public static ToObjectFunction of(TypedFunction f) { // noinspection unchecked return f.walk((TypedFunction.Visitor>) (TypedFunction.Visitor) INSTANCE); } public static ToObjectFunction of(ToPrimitiveFunction f) { // noinspection unchecked return f.walk( (ToPrimitiveFunction.Visitor>) (ToPrimitiveFunction.Visitor) INSTANCE); } @Override public ToObjectFunction visit(ToPrimitiveFunction f) { return BoxTransform.of(f); } @Override public ToObjectFunction visit(ToObjectFunction f) { return f; } @Override public ToObjectFunction visit(ToBooleanFunction f) { return BoxTransform.of(f); } @Override public ToObjectFunction visit(ToCharFunction f) { return BoxTransform.of(f); } @Override public ToObjectFunction visit(ToByteFunction f) { return BoxTransform.of(f); } @Override public ToObjectFunction visit(ToShortFunction f) { return BoxTransform.of(f); } @Override public ToObjectFunction visit(ToIntFunction f) { return BoxTransform.of(f); } @Override public ToObjectFunction visit(ToLongFunction f) { return BoxTransform.of(f); } @Override public ToObjectFunction visit(ToFloatFunction f) { return BoxTransform.of(f); } @Override public ToObjectFunction visit(ToDoubleFunction f) { return BoxTransform.of(f); } } private static Character box(ToCharFunction f, T x) { return TypeUtils.box(f.applyAsChar(x)); } private static Byte box(ToByteFunction f, T x) { return TypeUtils.box(f.applyAsByte(x)); } private static Short box(ToShortFunction f, T x) { return TypeUtils.box(f.applyAsShort(x)); } private static Integer box(ToIntFunction f, T x) { return TypeUtils.box(f.applyAsInt(x)); } private static Long box(ToLongFunction f, T x) { return TypeUtils.box(f.applyAsLong(x)); } private static Float box(ToFloatFunction f, T x) { return TypeUtils.box(f.applyAsFloat(x)); } private static Double box(ToDoubleFunction f, T x) { return TypeUtils.box(f.applyAsDouble(x)); } }