static T castTo()

in api/src/main/java/org/apache/iceberg/expressions/VariantExpressionUtil.java [51:117]


  static <T> T castTo(VariantValue value, Type type) {
    if (value == null) {
      return null;
    } else if (NO_CONVERSION_NEEDED.get(type) == value.type()) {
      return (T) value.asPrimitive().get();
    }

    switch (type.typeId()) {
      case INTEGER:
        switch (value.type()) {
          case INT8:
          case INT16:
            return (T) (Integer) ((Number) value.asPrimitive().get()).intValue();
        }

        break;
      case LONG:
        switch (value.type()) {
          case INT8:
          case INT16:
          case INT32:
            return (T) (Long) ((Number) value.asPrimitive().get()).longValue();
        }

        break;
      case DOUBLE:
        if (value.type() == PhysicalType.FLOAT) {
          return (T) (Double) ((Number) value.asPrimitive().get()).doubleValue();
        }

        break;
      case FIXED:
        Types.FixedType fixedType = (Types.FixedType) type;
        if (value.type() == PhysicalType.BINARY) {
          ByteBuffer buffer = (ByteBuffer) value.asPrimitive().get();
          if (buffer.remaining() == fixedType.length()) {
            return (T) buffer;
          }
        }

        break;
      case DECIMAL:
        Types.DecimalType decimalType = (Types.DecimalType) type;
        switch (value.type()) {
          case DECIMAL4:
          case DECIMAL8:
          case DECIMAL16:
            BigDecimal decimalValue = (BigDecimal) value.asPrimitive().get();
            if (decimalValue.scale() == decimalType.scale()) {
              return (T) decimalValue;
            }
        }

        break;
      case BOOLEAN:
        switch (value.type()) {
          case BOOLEAN_FALSE:
            return (T) Boolean.FALSE;
          case BOOLEAN_TRUE:
            return (T) Boolean.TRUE;
        }

        break;
    }

    return null;
  }