blob: b1f6508402a8dc581c4f8885fd2b8653fbfe83e9 [file] [log] [blame]
Christian Lavoieafc6d8f2011-02-20 02:39:19 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20
21package thrift
22
23import (
24 "math"
25 "strconv"
26)
27
28type Numeric interface {
29 Int64() int64
30 Int32() int32
31 Int16() int16
32 Byte() byte
33 Int() int
34 Float64() float64
35 Float32() float32
36 String() string
37 isNull() bool
38}
39
40type numeric struct {
41 iValue int64
42 dValue float64
43 sValue string
44 isNil bool
45}
46
47var (
48 INFINITY Numeric
49 NEGATIVE_INFINITY Numeric
50 NAN Numeric
51 ZERO Numeric
52 NUMERIC_NULL Numeric
53)
54
55func NewNumericFromDouble(dValue float64) Numeric {
56 if math.IsInf(dValue, 1) {
57 return INFINITY
58 }
59 if math.IsInf(dValue, -1) {
60 return NEGATIVE_INFINITY
61 }
62 if math.IsNaN(dValue) {
63 return NAN
64 }
65 iValue := int64(dValue)
66 sValue := strconv.Ftoa64(dValue, 'g', 10)
67 isNil := false
68 return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNil}
69}
70
71func NewNumericFromI64(iValue int64) Numeric {
72 dValue := float64(iValue)
73 sValue := string(iValue)
74 isNil := false
75 return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNil}
76}
77
78func NewNumericFromI32(iValue int32) Numeric {
79 dValue := float64(iValue)
80 sValue := string(iValue)
81 isNil := false
82 return &numeric{iValue: int64(iValue), dValue: dValue, sValue: sValue, isNil: isNil}
83}
84
85func NewNumericFromString(sValue string) Numeric {
86 if sValue == INFINITY.String() {
87 return INFINITY
88 }
89 if sValue == NEGATIVE_INFINITY.String() {
90 return NEGATIVE_INFINITY
91 }
92 if sValue == NAN.String() {
93 return NAN
94 }
95 iValue, _ := strconv.Atoi64(sValue)
96 dValue, _ := strconv.Atof64(sValue)
97 isNil := len(sValue) == 0
98 return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNil}
99}
100
101func NewNumericFromJSONString(sValue string, isNull bool) Numeric {
102 if isNull {
103 return NewNullNumeric()
104 }
105 if sValue == JSON_INFINITY {
106 return INFINITY
107 }
108 if sValue == JSON_NEGATIVE_INFINITY {
109 return NEGATIVE_INFINITY
110 }
111 if sValue == JSON_NAN {
112 return NAN
113 }
114 iValue, _ := strconv.Atoi64(sValue)
115 dValue, _ := strconv.Atof64(sValue)
116 return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNull}
117}
118
119func NewNullNumeric() Numeric {
120 return &numeric{iValue: 0, dValue: 0.0, sValue: "", isNil: true}
121}
122
123func (p *numeric) Int64() int64 {
124 return p.iValue
125}
126
127func (p *numeric) Int32() int32 {
128 return int32(p.iValue)
129}
130
131func (p *numeric) Int16() int16 {
132 return int16(p.iValue)
133}
134
135func (p *numeric) Byte() byte {
136 return byte(p.iValue)
137}
138
139func (p *numeric) Int() int {
140 return int(p.iValue)
141}
142
143func (p *numeric) Float64() float64 {
144 return p.dValue
145}
146
147func (p *numeric) Float32() float32 {
148 return float32(p.dValue)
149}
150
151func (p *numeric) String() string {
152 return p.sValue
153}
154
155func (p *numeric) isNull() bool {
156 return p.isNil
157}
158
159func init() {
160 INFINITY = &numeric{iValue: 0, dValue: math.Inf(1), sValue: "Infinity", isNil: false}
161 NEGATIVE_INFINITY = &numeric{iValue: 0, dValue: math.Inf(-1), sValue: "-Infinity", isNil: false}
162 NAN = &numeric{iValue: 0, dValue: math.NaN(), sValue: "NaN", isNil: false}
163 ZERO = &numeric{iValue: 0, dValue: 0, sValue: "0", isNil: false}
164 NUMERIC_NULL = &numeric{iValue: 0, dValue: 0, sValue: "0", isNil: true}
165}