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

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

The newest version!
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.kafka;

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.ArrayType;
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.qst.type.BoxedType;
import io.deephaven.qst.type.CustomType;
import io.deephaven.qst.type.GenericType;
import io.deephaven.qst.type.InstantType;
import io.deephaven.qst.type.StringType;
import io.deephaven.util.type.TypeUtils;

import java.util.Objects;
import java.util.Optional;

class UnboxTransform {

    private static final ToByteFunction UNBOX_BYTE = TypeUtils::unbox;
    private static final ToCharFunction UNBOX_CHAR = TypeUtils::unbox;
    private static final ToShortFunction UNBOX_SHORT = TypeUtils::unbox;
    private static final ToIntFunction UNBOX_INT = TypeUtils::unbox;
    private static final ToLongFunction UNBOX_LONG = TypeUtils::unbox;
    private static final ToFloatFunction UNBOX_FLOAT = TypeUtils::unbox;
    private static final ToDoubleFunction UNBOX_DOULE = TypeUtils::unbox;

    /**
     * Returns the Deephaven unboxed equivalent of {@code f}. Relevant for all {@link BoxedType boxed types} except the
     * {@link BoxedBooleanType boxed Boolean type}. All other functions will be return unchanged.
     *
     * @param f the function
     * @return the unboxed equivalent
     * @param  the input type
     * @see #unboxByte(ToObjectFunction)
     * @see #unboxChar(ToObjectFunction)
     * @see #unboxDouble(ToObjectFunction)
     * @see #unboxFloat(ToObjectFunction)
     * @see #unboxInt(ToObjectFunction)
     * @see #unboxLong(ToObjectFunction)
     * @see #unboxShort(ToObjectFunction)
     */
    public static  Optional> of(TypedFunction f) {
        return UnboxFunctionVisitor.of(f);
    }

    /**
     * Returns the Deephaven unboxed equivalent of {@code f}. Relevant for all {@link BoxedType boxed types} except the
     * {@link BoxedBooleanType boxed Boolean type}. All other functions will be return unchanged.
     *
     * @param f the object function
     * @return the unboxed equivalent
     * @param  the input type
     * @see #unboxByte(ToObjectFunction)
     * @see #unboxChar(ToObjectFunction)
     * @see #unboxDouble(ToObjectFunction)
     * @see #unboxFloat(ToObjectFunction)
     * @see #unboxInt(ToObjectFunction)
     * @see #unboxLong(ToObjectFunction)
     * @see #unboxShort(ToObjectFunction)
     */
    public static  Optional> of(ToObjectFunction f) {
        return UnboxObjectFunctionVisitor.of(f);
    }

    /**
     * Equivalent to {@code f.mapByte(TypeUtils::unbox)}.
     *
     * @param f the Byte function
     * @return the byte function
     * @param  the input type
     * @see TypeUtils#unbox(Byte)
     */
    public static  ToByteFunction unboxByte(ToObjectFunction f) {
        return f.mapToByte(UNBOX_BYTE);
    }

    /**
     * Equivalent to {@code f.mapChar(TypeUtils::unbox)}.
     *
     * @param f the Character function
     * @return the char function
     * @param  the input type
     * @see TypeUtils#unbox(Character)
     */
    public static  ToCharFunction unboxChar(ToObjectFunction f) {
        return f.mapToChar(UNBOX_CHAR);
    }

    /**
     * Equivalent to {@code f.mapShort(TypeUtils::unbox)}.
     *
     * @param f the Short function
     * @return the short function
     * @param  the input type
     * @see TypeUtils#unbox(Short)
     */
    public static  ToShortFunction unboxShort(ToObjectFunction f) {
        return f.mapToShort(UNBOX_SHORT);
    }

    /**
     * Equivalent to {@code f.mapInt(TypeUtils::unbox)}.
     *
     * @param f the Integer function
     * @return the int function
     * @param  the input type
     * @see TypeUtils#unbox(Integer)
     */
    public static  ToIntFunction unboxInt(ToObjectFunction f) {
        return f.mapToInt(UNBOX_INT);
    }

    /**
     * Equivalent to {@code f.mapLong(TypeUtils::unbox)}.
     *
     * @param f the Long function
     * @return the long function
     * @param  the input type
     * @see TypeUtils#unbox(Long)
     */
    public static  ToLongFunction unboxLong(ToObjectFunction f) {
        return f.mapToLong(UNBOX_LONG);
    }

    /**
     * Equivalent to {@code f.mapFloat(TypeUtils::unbox)}.
     *
     * @param f the Float function
     * @return the float function
     * @param  the input type
     * @see TypeUtils#unbox(Float)
     */
    public static  ToFloatFunction unboxFloat(ToObjectFunction f) {
        return f.mapToFloat(UNBOX_FLOAT);
    }

    /**
     * Equivalent to {@code f.mapDouble(TypeUtils::unbox)}.
     *
     * @param f the Double function
     * @return the double function
     * @param  the input type
     * @see TypeUtils#unbox(Double)
     */
    public static  ToDoubleFunction unboxDouble(ToObjectFunction f) {
        return f.mapToDouble(UNBOX_DOULE);
    }

    private enum UnboxFunctionVisitor implements TypedFunction.Visitor> {
        INSTANCE;

        public static  Optional> of(TypedFunction f) {
            // noinspection unchecked
            return Optional.ofNullable(
                    f.walk((TypedFunction.Visitor>) (TypedFunction.Visitor) INSTANCE));
        }

        @Override
        public ToPrimitiveFunction visit(ToPrimitiveFunction f) {
            return f;
        }

        @Override
        public ToPrimitiveFunction visit(ToObjectFunction f) {
            return UnboxTransform.of(f).orElse(null);
        }
    }

    private static class UnboxObjectFunctionVisitor
            implements GenericType.Visitor>, BoxedType.Visitor> {

        public static  Optional> of(ToObjectFunction f) {
            return Optional.ofNullable(f.returnType().walk(new UnboxObjectFunctionVisitor<>(f)));
        }

        private final ToObjectFunction f;

        public UnboxObjectFunctionVisitor(ToObjectFunction f) {
            this.f = Objects.requireNonNull(f);
        }

        @Override
        public ToPrimitiveFunction visit(BoxedType boxedType) {
            return boxedType.walk((BoxedType.Visitor>) this);
        }

        @Override
        public ToPrimitiveFunction visit(StringType stringType) {
            return null;
        }

        @Override
        public ToPrimitiveFunction visit(InstantType instantType) {
            return null;
        }

        @Override
        public ToPrimitiveFunction visit(ArrayType arrayType) {
            return null;
        }

        @Override
        public ToPrimitiveFunction visit(CustomType customType) {
            return null;
        }

        @Override
        public ToPrimitiveFunction visit(BoxedBooleanType booleanType) {
            // We don't have an "unboxed boolean".
            // We _can_ transform it to a byte, but that's a separate operation.
            return null;
        }

        @Override
        public ToPrimitiveFunction visit(BoxedByteType byteType) {
            return unboxByte(f.cast(byteType));
        }

        @Override
        public ToPrimitiveFunction visit(BoxedCharType charType) {
            return unboxChar(f.cast(charType));
        }

        @Override
        public ToPrimitiveFunction visit(BoxedShortType shortType) {
            return unboxShort(f.cast(shortType));
        }

        @Override
        public ToPrimitiveFunction visit(BoxedIntType intType) {
            return unboxInt(f.cast(intType));
        }

        @Override
        public ToPrimitiveFunction visit(BoxedLongType longType) {
            return unboxLong(f.cast(longType));
        }

        @Override
        public ToPrimitiveFunction visit(BoxedFloatType floatType) {
            return unboxFloat(f.cast(floatType));
        }

        @Override
        public ToPrimitiveFunction visit(BoxedDoubleType doubleType) {
            return unboxDouble(f.cast(doubleType));
        }
    }
}