| Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 1 | /* | 
 | 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 |  | 
 | 21 | package thrift | 
 | 22 |  | 
 | 23 | import ( | 
 | 24 |   "math" | 
 | 25 |   "strconv" | 
 | 26 | ) | 
 | 27 |  | 
 | 28 | type 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 |  | 
 | 40 | type numeric struct { | 
 | 41 |   iValue int64 | 
 | 42 |   dValue float64 | 
 | 43 |   sValue string | 
 | 44 |   isNil  bool | 
 | 45 | } | 
 | 46 |  | 
 | 47 | var ( | 
 | 48 |   INFINITY          Numeric | 
 | 49 |   NEGATIVE_INFINITY Numeric | 
 | 50 |   NAN               Numeric | 
 | 51 |   ZERO              Numeric | 
 | 52 |   NUMERIC_NULL      Numeric | 
 | 53 | ) | 
 | 54 |  | 
 | 55 | func 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 |  | 
 | 71 | func 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 |  | 
 | 78 | func 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 |  | 
 | 85 | func 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 |  | 
 | 101 | func 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 |  | 
 | 119 | func NewNullNumeric() Numeric { | 
 | 120 |   return &numeric{iValue: 0, dValue: 0.0, sValue: "", isNil: true} | 
 | 121 | } | 
 | 122 |  | 
 | 123 | func (p *numeric) Int64() int64 { | 
 | 124 |   return p.iValue | 
 | 125 | } | 
 | 126 |  | 
 | 127 | func (p *numeric) Int32() int32 { | 
 | 128 |   return int32(p.iValue) | 
 | 129 | } | 
 | 130 |  | 
 | 131 | func (p *numeric) Int16() int16 { | 
 | 132 |   return int16(p.iValue) | 
 | 133 | } | 
 | 134 |  | 
 | 135 | func (p *numeric) Byte() byte { | 
 | 136 |   return byte(p.iValue) | 
 | 137 | } | 
 | 138 |  | 
 | 139 | func (p *numeric) Int() int { | 
 | 140 |   return int(p.iValue) | 
 | 141 | } | 
 | 142 |  | 
 | 143 | func (p *numeric) Float64() float64 { | 
 | 144 |   return p.dValue | 
 | 145 | } | 
 | 146 |  | 
 | 147 | func (p *numeric) Float32() float32 { | 
 | 148 |   return float32(p.dValue) | 
 | 149 | } | 
 | 150 |  | 
 | 151 | func (p *numeric) String() string { | 
 | 152 |   return p.sValue | 
 | 153 | } | 
 | 154 |  | 
 | 155 | func (p *numeric) isNull() bool { | 
 | 156 |   return p.isNil | 
 | 157 | } | 
 | 158 |  | 
 | 159 | func 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 | } |