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

com.amazon.ion.impl._Private_ScalarConversions Maven / Gradle / Ivy

There is a newer version: 1.11.9
Show newest version
/*
 * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.amazon.ion.impl;

import com.amazon.ion.Decimal;
import com.amazon.ion.IntegerSize;
import com.amazon.ion.IonException;
import com.amazon.ion.IonType;
import com.amazon.ion.Timestamp;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

/**
 * NOT FOR APPLICATION USE!
 */
public class _Private_ScalarConversions
{
    public static final class AS_TYPE {

        public static final int null_value         =  1;
        public static final int boolean_value      =  2;
        public static final int int_value          =  3;
        public static final int long_value         =  4;
        public static final int bigInteger_value   =  5;
        public static final int decimal_value      =  6;
        public static final int double_value       =  7;
        public static final int string_value       =  8;
        public static final int date_value         =  9;
        public static final int timestamp_value    = 10;

        static final int idx_to_bit_mask(int idx) {
            return 1 << (idx - 1);
        }

        public static final int numeric_types      = idx_to_bit_mask(int_value)
                                                   | idx_to_bit_mask(long_value)
                                                   | idx_to_bit_mask(bigInteger_value)
                                                   | idx_to_bit_mask(decimal_value)
                                                   | idx_to_bit_mask(double_value);

        public static final int datetime_types     = idx_to_bit_mask(date_value)
                                                   | idx_to_bit_mask(timestamp_value);

        public static final int convertable_type   = idx_to_bit_mask(numeric_types)
                                                   | idx_to_bit_mask(datetime_types)
                                                   | idx_to_bit_mask(boolean_value)
                                                   | idx_to_bit_mask(string_value);
    }

    public static IntegerSize getIntegerSize(int authoritative_type) {
        switch (authoritative_type)
        {
            case AS_TYPE.int_value:
                return IntegerSize.INT;
            case AS_TYPE.long_value:
                return IntegerSize.LONG;
            case AS_TYPE.bigInteger_value:
                return IntegerSize.BIG_INTEGER;
            default:
                return null;
        }
    }

    public static String getValueTypeName(int value_type) {
        switch (value_type) {
        case AS_TYPE.null_value:       return "null";
        case AS_TYPE.boolean_value:    return "boolean";
        case AS_TYPE.int_value:        return "int";
        case AS_TYPE.long_value:       return "long";
        case AS_TYPE.bigInteger_value: return "bigInteger";
        case AS_TYPE.decimal_value:    return "decimal";
        case AS_TYPE.double_value:     return "double";
        case AS_TYPE.string_value:     return "string";
        case AS_TYPE.date_value:       return "date";
        case AS_TYPE.timestamp_value:  return "timestamp";
        default:                       return "";
        }
    }
    public static String get_value_type_name(int value_type) {
        switch (value_type) {
        case AS_TYPE.null_value:
        case AS_TYPE.boolean_value:
        case AS_TYPE.int_value:
        case AS_TYPE.long_value:
        case AS_TYPE.bigInteger_value:
        case AS_TYPE.decimal_value:
        case AS_TYPE.double_value:
        case AS_TYPE.string_value:
        case AS_TYPE.date_value:
        case AS_TYPE.timestamp_value:
            return getValueTypeName(value_type)+"_value";
        default:
            return "";
        }
    }

    protected static int FNID_no_conversion               = -1;
    protected static int FNID_identity                    =  0;

    //from_string_conversion
    protected final static int FNID_FROM_STRING_TO_NULL         =  1;
    protected final static int FNID_FROM_STRING_TO_BOOLEAN      =  2;
    protected final static int FNID_FROM_STRING_TO_INT          =  3;
    protected final static int FNID_FROM_STRING_TO_LONG         =  4;
    protected final static int FNID_FROM_STRING_TO_BIGINTEGER   =  5;
    protected final static int FNID_FROM_STRING_TO_DECIMAL      =  6;
    protected final static int FNID_FROM_STRING_TO_DOUBLE       =  7;
    protected final static int FNID_FROM_STRING_TO_DATE         =  8;
    protected final static int FNID_FROM_STRING_TO_TIMESTAMP    =  9;
    static int [] from_string_conversion = {
                        FNID_no_conversion,  // fake entry to switch from 0 based to 1 based
                        FNID_FROM_STRING_TO_NULL,
                        FNID_FROM_STRING_TO_BOOLEAN,
                        FNID_FROM_STRING_TO_INT,
                        FNID_FROM_STRING_TO_LONG,
                        FNID_FROM_STRING_TO_BIGINTEGER,
                        FNID_FROM_STRING_TO_DECIMAL,
                        FNID_FROM_STRING_TO_DOUBLE,
                        FNID_identity,
                        FNID_FROM_STRING_TO_DATE,
                        FNID_FROM_STRING_TO_TIMESTAMP,
                        FNID_no_conversion,
                        FNID_no_conversion
                    };

    //to_string_conversions;
    protected final static int FNID_FROM_NULL_TO_STRING         = 10;
    protected final static int FNID_FROM_BOOLEAN_TO_STRING      = 11;
    protected final static int FNID_FROM_INT_TO_STRING          = 12;
    protected final static int FNID_FROM_LONG_TO_STRING         = 13;
    protected final static int FNID_FROM_BIGINTEGER_TO_STRING   = 14;
    protected final static int FNID_FROM_DECIMAL_TO_STRING      = 15;
    protected final static int FNID_FROM_DOUBLE_TO_STRING       = 16;
    protected final static int FNID_FROM_DATE_TO_STRING         = 17;
    protected final static int FNID_FROM_TIMESTAMP_TO_STRING    = 18;
    static int [] to_string_conversions = {
                        FNID_no_conversion,  // fake entry to switch from 0 based to 1 based
                        FNID_FROM_NULL_TO_STRING,
                        FNID_FROM_BOOLEAN_TO_STRING,
                        FNID_FROM_INT_TO_STRING,
                        FNID_FROM_LONG_TO_STRING,
                        FNID_FROM_BIGINTEGER_TO_STRING,
                        FNID_FROM_DECIMAL_TO_STRING,
                        FNID_FROM_DOUBLE_TO_STRING,
                        FNID_identity,
                        FNID_FROM_DATE_TO_STRING,
                        FNID_FROM_TIMESTAMP_TO_STRING,
                        FNID_no_conversion,
                        FNID_no_conversion
                    };


    //to_int_conversion;
    protected final static int FNID_FROM_LONG_TO_INT         = 19;
    protected final static int FNID_FROM_BIGINTEGER_TO_INT   = 20;
    protected final static int FNID_FROM_DECIMAL_TO_INT      = 21;
    protected final static int FNID_FROM_DOUBLE_TO_INT       = 22;
    static int [] to_int_conversion = {
                        FNID_no_conversion,  // fake entry to switch from 0 based to 1 based
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_identity,
                        FNID_FROM_LONG_TO_INT,
                        FNID_FROM_BIGINTEGER_TO_INT,
                        FNID_FROM_DECIMAL_TO_INT,
                        FNID_FROM_DOUBLE_TO_INT,
                        FNID_FROM_STRING_TO_INT,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion
                };

    //to_long_conversion;
    protected final static int FNID_FROM_INT_TO_LONG          = 23;
    protected final static int FNID_FROM_BIGINTEGER_TO_LONG   = 24;
    protected final static int FNID_FROM_DECIMAL_TO_LONG      = 25;
    protected final static int FNID_FROM_DOUBLE_TO_LONG       = 26;
    static int [] to_long_conversion = {
                        FNID_no_conversion,  // fake entry to switch from 0 based to 1 based
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_FROM_INT_TO_LONG,
                        FNID_identity,
                        FNID_FROM_BIGINTEGER_TO_LONG,
                        FNID_FROM_DECIMAL_TO_LONG,
                        FNID_FROM_DOUBLE_TO_LONG,
                        FNID_FROM_STRING_TO_LONG,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion
                    };


    //to_bigInteger_conversion;
    protected final static int FNID_FROM_INT_TO_BIGINTEGER          = 27;
    protected final static int FNID_FROM_LONG_TO_BIGINTEGER         = 28;
    protected final static int FNID_FROM_DECIMAL_TO_BIGINTEGER      = 29;
    protected final static int FNID_FROM_DOUBLE_TO_BIGINTEGER       = 30;
    static int [] to_bigInteger_conversion = {
                        FNID_no_conversion,  // fake entry to switch from 0 based to 1 based
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_FROM_INT_TO_BIGINTEGER,
                        FNID_FROM_LONG_TO_BIGINTEGER,
                        FNID_identity,
                        FNID_FROM_DECIMAL_TO_BIGINTEGER,
                        FNID_FROM_DOUBLE_TO_BIGINTEGER,
                        FNID_FROM_STRING_TO_BIGINTEGER,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion
                    };

    //to_decimal_conversion;
    protected final static int FNID_FROM_INT_TO_DECIMAL          = 31;
    protected final static int FNID_FROM_LONG_TO_DECIMAL         = 32;
    protected final static int FNID_FROM_BIGINTEGER_TO_DECIMAL   = 33;
    protected final static int FNID_FROM_DOUBLE_TO_DECIMAL       = 34;
    static int [] to_decimal_conversion = {
                        FNID_no_conversion,  // fake entry to switch from 0 based to 1 based
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_FROM_INT_TO_DECIMAL,
                        FNID_FROM_LONG_TO_DECIMAL,
                        FNID_FROM_BIGINTEGER_TO_DECIMAL,
                        FNID_identity,
                        FNID_FROM_DOUBLE_TO_DECIMAL,
                        FNID_FROM_STRING_TO_DECIMAL,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion
                    };

    //to_double_conversion;
    protected final static int FNID_FROM_INT_TO_DOUBLE          = 35;
    protected final static int FNID_FROM_LONG_TO_DOUBLE         = 36;
    protected final static int FNID_FROM_BIGINTEGER_TO_DOUBLE   = 37;
    protected final static int FNID_FROM_DECIMAL_TO_DOUBLE      = 38;
    static int [] to_double_conversion = {
                        FNID_no_conversion,  // fake entry to switch from 0 based to 1 based
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_FROM_INT_TO_DOUBLE,
                        FNID_FROM_LONG_TO_DOUBLE,
                        FNID_FROM_BIGINTEGER_TO_DOUBLE,
                        FNID_FROM_DECIMAL_TO_DOUBLE,
                        FNID_identity,
                        FNID_FROM_STRING_TO_DOUBLE,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion,
                        FNID_no_conversion
                    };
    protected final static int FNID_FROM_TIMESTAMP_TO_DATE      = 39;
    protected final static int FNID_FROM_DATE_TO_TIMESTAMP      = 40;

    public final static String getAllValueTypeNames(int value_type) {
        StringBuilder sb = new StringBuilder();
        sb.append('(');
        int bit = 1;
        for (int ii=0; ii Integer.MAX_VALUE) {
                throw new CantConvertException("long is too large to fit in an int");
            }
            _int_value = (int)_long_value;
            add_value_type(AS_TYPE.int_value);
        }
        private final void fn_from_biginteger_to_int() {
            if (min_int_value.compareTo(_bigInteger_value) > 0
             || max_int_value.compareTo(_bigInteger_value) < 0
            ) {
                throw new CantConvertException("bigInteger value is too large to fit in an int");
            }
            _int_value = _bigInteger_value.intValue();
            add_value_type(AS_TYPE.int_value);
        }
        private final void fn_from_decimal_to_int() {
            if (min_int_decimal_value.compareTo(_decimal_value) > 0
             || max_int_decimal_value.compareTo(_decimal_value) < 0
             ) {
                throw new CantConvertException("BigDecimal value is too large to fit in an int");
            }
            _int_value = _decimal_value.intValue();
            add_value_type(AS_TYPE.int_value);
        }
        private final void fn_from_double_to_int() {
            if (_double_value < Integer.MIN_VALUE
             || _double_value > Integer.MAX_VALUE
            ) {
                throw new CantConvertException("double is too large to fit in an int");
            }
            _int_value = (int)_double_value;
            add_value_type(AS_TYPE.int_value);
        }
        private final void fn_from_int_to_long() {
            _long_value = _int_value;
            add_value_type(AS_TYPE.long_value);
        }
        private final void fn_from_biginteger_to_long() {
            if (min_long_value.compareTo(_bigInteger_value) > 0
             || max_long_value.compareTo(_bigInteger_value) < 0
            ) {
                throw new CantConvertException("BigInteger is too large to fit in a long");
            }
            _long_value = _bigInteger_value.longValue();
            add_value_type(AS_TYPE.long_value);
        }
        private final void fn_from_decimal_to_long() {
            if (min_long_decimal_value.compareTo(_decimal_value) > 0
             || max_long_decimal_value.compareTo(_decimal_value) < 0
            ) {
                throw new CantConvertException("BigDecimal value is too large to fit in a long");
            }
            _long_value = _decimal_value.longValue();
            add_value_type(AS_TYPE.long_value);
        }
        private final void fn_from_double_to_long() {
            if (_double_value < Long.MIN_VALUE
             || _double_value > Long.MAX_VALUE
            ) {
                throw new CantConvertException("double is too large to fit in a long");
            }
            _long_value = (long)_double_value;
            add_value_type(AS_TYPE.long_value);
        }
        private final void fn_from_int_to_biginteger() {
            _bigInteger_value = BigInteger.valueOf(_int_value);
            add_value_type(AS_TYPE.bigInteger_value);
        }
        private final void fn_from_long_to_biginteger() {
            _bigInteger_value = BigInteger.valueOf(_long_value);
            add_value_type(AS_TYPE.bigInteger_value);
        }
        private final void fn_from_decimal_to_biginteger() {
            _bigInteger_value = _decimal_value.toBigInteger();
            add_value_type(AS_TYPE.bigInteger_value);
        }
        private final void fn_from_double_to_biginteger() {
            // To avoid decapitating values that are > Long.MAX_VALUE, we must
            // convert to BigDecimal first.
            _bigInteger_value =
                Decimal.valueOf(_double_value).toBigInteger();
            add_value_type(AS_TYPE.bigInteger_value);
        }
        private final void fn_from_int_to_decimal() {
            _decimal_value = Decimal.valueOf(_int_value);
            add_value_type(AS_TYPE.decimal_value);
        }
        private final void fn_from_long_to_decimal() {
            _decimal_value = Decimal.valueOf(_long_value);
            add_value_type(AS_TYPE.decimal_value);
        }
        private final void fn_from_biginteger_to_decimal() {
            _decimal_value = Decimal.valueOf(_bigInteger_value);
            add_value_type(AS_TYPE.decimal_value);
        }
        private final void fn_from_double_to_decimal() {
            _decimal_value = Decimal.valueOf(_double_value);
            add_value_type(AS_TYPE.decimal_value);
        }
        private final void fn_from_int_to_double() {
            _double_value = _int_value;
            add_value_type(AS_TYPE.double_value);
        }
        private final void fn_from_long_to_double() {
            _double_value = _long_value;
            add_value_type(AS_TYPE.double_value);
        }
        private final void fn_from_biginteger_to_double() {
            _double_value = _bigInteger_value.doubleValue();
            add_value_type(AS_TYPE.double_value);
        }
        private final void fn_from_decimal_to_double() {
            _double_value = _decimal_value.doubleValue();
            add_value_type(AS_TYPE.double_value);
        }
        private final void fn_from_timestamp_to_date() {
            _date_value = _timestamp_value.dateValue();
            add_value_type(AS_TYPE.date_value);
        }
        private final void fn_from_date_to_timestamp() {
            _timestamp_value = new Timestamp(_date_value.getTime(), null);
            add_value_type(AS_TYPE.timestamp_value);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy