Skip to content

Commit

Permalink
refactor: make utility classes UnboxTransform and ToChunkTypeTransfor…
Browse files Browse the repository at this point in the history
…m public (deephaven#6338)
  • Loading branch information
jcferretti authored Nov 6, 2024
1 parent 7657ff5 commit 8832682
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
1 change: 1 addition & 0 deletions Util/function/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {

dependencies {
api project(':qst-type')
implementation project(':engine-query-constants')

compileOnly libs.jetbrains.annotations

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
//
// 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;
package io.deephaven.function;

import io.deephaven.qst.type.ArrayType;
import io.deephaven.qst.type.BoxedBooleanType;
import io.deephaven.qst.type.BoxedByteType;
Expand All @@ -27,20 +17,21 @@
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 {
import static io.deephaven.util.QueryConstants.*;

public class UnboxTransform {

private static final ToByteFunction<Byte> UNBOX_BYTE = TypeUtils::unbox;
private static final ToCharFunction<Character> UNBOX_CHAR = TypeUtils::unbox;
private static final ToShortFunction<Short> UNBOX_SHORT = TypeUtils::unbox;
private static final ToIntFunction<Integer> UNBOX_INT = TypeUtils::unbox;
private static final ToLongFunction<Long> UNBOX_LONG = TypeUtils::unbox;
private static final ToFloatFunction<Float> UNBOX_FLOAT = TypeUtils::unbox;
private static final ToDoubleFunction<Double> UNBOX_DOULE = TypeUtils::unbox;
private static final ToDoubleFunction<Double> UNBOX_DOUBLE = TypeUtils::unbox;

/**
* Returns the Deephaven unboxed equivalent of {@code f}. Relevant for all {@link BoxedType boxed types} except the
Expand Down Expand Up @@ -161,7 +152,7 @@ public static <T> ToFloatFunction<T> unboxFloat(ToObjectFunction<T, Float> f) {
* @see TypeUtils#unbox(Double)
*/
public static <T> ToDoubleFunction<T> unboxDouble(ToObjectFunction<T, Double> f) {
return f.mapToDouble(UNBOX_DOULE);
return f.mapToDouble(UNBOX_DOUBLE);
}

private enum UnboxFunctionVisitor implements TypedFunction.Visitor<Object, ToPrimitiveFunction<Object>> {
Expand Down Expand Up @@ -264,4 +255,36 @@ public ToPrimitiveFunction<T> visit(BoxedDoubleType doubleType) {
return unboxDouble(f.cast(doubleType));
}
}

// TODO: Clean up dependencies to be able to use io.deephaven.util.type.TypeUtils.
private static class TypeUtils {
public static byte unbox(Byte value) {
return (value == null ? NULL_BYTE : value);
}

public static char unbox(Character value) {
return (value == null ? NULL_CHAR : value);
}

public static double unbox(Double value) {
return (value == null ? NULL_DOUBLE : value);
}

public static float unbox(Float value) {
return (value == null ? NULL_FLOAT : value);
}

public static int unbox(Integer value) {
return (value == null ? NULL_INT : value);
}

public static long unbox(Long value) {
return (value == null ? NULL_LONG : value);
}

public static short unbox(Short value) {
return (value == null ? NULL_SHORT : value);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import io.deephaven.api.ColumnName;
import io.deephaven.engine.table.ColumnDefinition;
import io.deephaven.engine.table.TableDefinition;
import io.deephaven.function.ToObjectFunction;
import io.deephaven.function.ToPrimitiveFunction;
import io.deephaven.function.TypedFunction;
import io.deephaven.function.*;
import io.deephaven.kafka.KafkaTools.Consume;
import io.deephaven.kafka.KafkaTools.KeyOrValue;
import io.deephaven.kafka.KafkaTools.KeyOrValueIngestData;
Expand All @@ -38,6 +36,7 @@
import io.deephaven.protobuf.ProtobufFunction;
import io.deephaven.protobuf.ProtobufFunctions;
import io.deephaven.protobuf.ProtobufFunctions.Builder;
import io.deephaven.function.ToChunkTypeTransform;
import io.deephaven.qst.type.Type;
import io.deephaven.util.annotations.VisibleForTesting;
import io.deephaven.util.mutable.MutableInt;
Expand Down
1 change: 1 addition & 0 deletions extensions/protobuf/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies {
api project(':util-function')
api libs.protobuf.java
implementation project(':engine-query-constants')
implementation project(':engine-time')

compileOnly project(':util-immutables')
annotationProcessor libs.immutables.value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.kafka;
package io.deephaven.function;

import io.deephaven.function.ToByteFunction;
import io.deephaven.function.ToLongFunction;
import io.deephaven.function.ToObjectFunction;
import io.deephaven.function.ToPrimitiveFunction;
import io.deephaven.function.TypedFunction;
import io.deephaven.qst.type.ArrayType;
import io.deephaven.qst.type.BoxedBooleanType;
import io.deephaven.qst.type.BoxedByteType;
Expand All @@ -28,15 +23,15 @@
import java.time.Instant;
import java.util.Objects;

import static io.deephaven.kafka.UnboxTransform.unboxByte;
import static io.deephaven.kafka.UnboxTransform.unboxChar;
import static io.deephaven.kafka.UnboxTransform.unboxDouble;
import static io.deephaven.kafka.UnboxTransform.unboxFloat;
import static io.deephaven.kafka.UnboxTransform.unboxInt;
import static io.deephaven.kafka.UnboxTransform.unboxLong;
import static io.deephaven.kafka.UnboxTransform.unboxShort;
import static io.deephaven.function.UnboxTransform.unboxByte;
import static io.deephaven.function.UnboxTransform.unboxChar;
import static io.deephaven.function.UnboxTransform.unboxDouble;
import static io.deephaven.function.UnboxTransform.unboxFloat;
import static io.deephaven.function.UnboxTransform.unboxInt;
import static io.deephaven.function.UnboxTransform.unboxLong;
import static io.deephaven.function.UnboxTransform.unboxShort;

class ToChunkTypeTransform {
public class ToChunkTypeTransform {

private static final ToByteFunction<Boolean> BOOLEAN_AS_BYTE = BooleanUtils::booleanAsByte;
private static final ToLongFunction<Instant> EPOCH_NANOS = DateTimeUtils::epochNanos;
Expand Down

0 comments on commit 8832682

Please sign in to comment.