| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * A dynamically-typed value that may appear as the result or intermediate value
of a spreadsheet | |
| 7 * computation. | |
| 8 */ | |
| 9 class Value { | |
| 10 static final int TYPE_UNKNOWN = -1; | |
| 11 static final int TYPE_BOOLEAN = 1; | |
| 12 static final int TYPE_CELLREF = 2; | |
| 13 static final int TYPE_DATE = 3; | |
| 14 static final int TYPE_DATE_TIME = 4; | |
| 15 static final int TYPE_DOUBLE = 5; | |
| 16 static final int TYPE_RANGE = 6; | |
| 17 static final int TYPE_STRING = 7; | |
| 18 static final int TYPE_TIME = 8; | |
| 19 | |
| 20 static String getDatatypeAsString(int datatype) { | |
| 21 switch (datatype) { | |
| 22 case Value.TYPE_DOUBLE: | |
| 23 return "double"; | |
| 24 case Value.TYPE_DATE: | |
| 25 return "date"; | |
| 26 case Value.TYPE_TIME: | |
| 27 return "time"; | |
| 28 case Value.TYPE_DATE_TIME: | |
| 29 return "date_time"; | |
| 30 case Value.TYPE_STRING: | |
| 31 return "string"; | |
| 32 case Value.TYPE_BOOLEAN: | |
| 33 return "boolean"; | |
| 34 default: | |
| 35 return "unknown"; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 int _datatype; | |
| 40 | |
| 41 /** | |
| 42 * Returns the datatype of this [Value], one of [TYPE_BOOLEAN], [TYPE_CELLREF]
, [TYPE_DOUBLE], | |
| 43 * [TYPE_RANGE], [TYPE_STRING], [TYPE_DATE], [TYPE_TIME], or [TYPE_DATE_TIME].
If the datatype | |
| 44 * is unknown, [TYPE_UNKNOWN] is returned. | |
| 45 */ | |
| 46 int get datatype() => _datatype; | |
| 47 | |
| 48 Value(this._datatype) { } | |
| 49 | |
| 50 /** | |
| 51 * Return the value as a boolean. For numeric values, 0 equates to [:false:]
and all other | |
| 52 * values equate to [:true:]. | |
| 53 */ | |
| 54 bool asBoolean(CellLocation location) => asDouble(location) != 0.0; | |
| 55 | |
| 56 /** | |
| 57 * Returns a [CellRefToken] from this [Value]. If this [Value] is not a [RefV
alue], | |
| 58 * an exception is thrown. | |
| 59 */ | |
| 60 CellRefToken asCellRefToken() { | |
| 61 throw new RuntimeException("Expected CellRefToken"); | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Returns a double from this [Value]. If this [Value] is a [RefValue], get t
he computed | |
| 66 * value from the spreadsheet, interpreting the reference relative to the give
n | |
| 67 * (sheet, row, col) [location]. Otherwise, [location] is ignored ([:null:] i
s an | |
| 68 * acceptable value). | |
| 69 */ | |
| 70 abstract double asDouble(CellLocation location); | |
| 71 | |
| 72 RangeToken asRangeToken() { | |
| 73 throw new RuntimeException("Expected RangeToken"); | |
| 74 } | |
| 75 | |
| 76 /** | |
| 77 * Returns a [String] from this [Value]. If this [Value] is a [RefValue], get
the computed | |
| 78 * value from the spreadsheet, interpreting the reference relative to the give
n | |
| 79 * (sheet, row, col) [location]. Otherwise, [location] is ignored ([:null:] i
s an | |
| 80 * acceptable value). | |
| 81 * | |
| 82 * For numeric values, this method will throw an exception. | |
| 83 */ | |
| 84 String asString(CellLocation location) { | |
| 85 throw new RuntimeException("Expected String"); | |
| 86 } | |
| 87 | |
| 88 /** | |
| 89 * Returns [:true:] if this [Value] is a string. If this [Value] is a [RefVal
ue], the reference | |
| 90 * is evaluated relative to the given (sheet, row, col) [location]. | |
| 91 * | |
| 92 * The implementation in this superclass returns [:false:]. | |
| 93 */ | |
| 94 bool isString(CellLocation location) => false; | |
| 95 | |
| 96 /** | |
| 97 * For reference values, the reference is evaluated relative to the given (she
et, row, col) | |
| 98 * [location]. For non-reference [Value]s, [lookup] returns the value itself. | |
| 99 */ | |
| 100 Value lookup(CellLocation location) => this; | |
| 101 | |
| 102 /** | |
| 103 * Returns a String representation of this [Value] for debugging. The return
value is not | |
| 104 * intended to be part of the application's user-facing UI. | |
| 105 */ | |
| 106 abstract String toString(); | |
| 107 } | |
| 108 | |
| 109 /** | |
| 110 * Superclass for all values other than strings and references. | |
| 111 */ | |
| 112 class NumericValue extends Value { | |
| 113 double _value; | |
| 114 | |
| 115 double get value() => _value; | |
| 116 | |
| 117 NumericValue(this._value, int datatype) : super(datatype) { } | |
| 118 | |
| 119 double asDouble(CellLocation location) => _value; | |
| 120 | |
| 121 String toString() => "NumericValue[${value}]"; | |
| 122 } | |
| 123 | |
| 124 class BooleanValue extends NumericValue { | |
| 125 | |
| 126 BooleanValue(bool b) : super(b ? 1.0 : 0.0, Value.TYPE_BOOLEAN) { } | |
| 127 | |
| 128 bool asBoolean(CellLocation location) => value != 0.0; | |
| 129 | |
| 130 String toString() => "BooleanValue[${value == 0.0 ? "FALSE" : "TRUE"}]"; | |
| 131 } | |
| 132 | |
| 133 class DateValue extends NumericValue { | |
| 134 | |
| 135 DateValue(double date) : super(date, Value.TYPE_DATE) { } | |
| 136 | |
| 137 String toString() => "DateValue[${value}]"; | |
| 138 } | |
| 139 | |
| 140 class DateTimeValue extends NumericValue { | |
| 141 | |
| 142 DateTimeValue(double dateTime) : super(dateTime, Value.TYPE_DATE_TIME) { } | |
| 143 | |
| 144 String toString() => "DateTimeValue[${value}]"; | |
| 145 } | |
| 146 | |
| 147 class DoubleValue extends NumericValue { | |
| 148 | |
| 149 DoubleValue(double value) : super(value, Value.TYPE_DOUBLE) { } | |
| 150 | |
| 151 String toString() => "DoubleValue[${value}]"; | |
| 152 } | |
| 153 | |
| 154 class TimeValue extends NumericValue { | |
| 155 | |
| 156 TimeValue(double time) : super(time, Value.TYPE_TIME) { } | |
| 157 | |
| 158 String toString() => "TimeValue[${value}]"; | |
| 159 } | |
| 160 | |
| 161 class StringValue extends Value { | |
| 162 String _s; | |
| 163 | |
| 164 StringValue(this._s) : super(Value.TYPE_STRING) { } | |
| 165 | |
| 166 // Strings are interpreted as 0.0 when used as inputs to a numeric computation | |
| 167 double asDouble(CellLocation location) => 0.0; | |
| 168 | |
| 169 String asString(CellLocation location) => _s; | |
| 170 | |
| 171 bool isString(CellLocation location) => true; | |
| 172 | |
| 173 String toString() => "StringValue[${_s}]"; | |
| 174 } | |
| 175 | |
| 176 class RefValue extends Value { | |
| 177 | |
| 178 RefValue(int datatype) : super(datatype) { } | |
| 179 | |
| 180 double asDouble(CellLocation location) => lookup(location).asDouble(null); | |
| 181 | |
| 182 String asString(CellLocation location) => lookup(location).asString(null); | |
| 183 | |
| 184 bool isString(CellLocation location) => lookup(location).isString(null); | |
| 185 | |
| 186 /** | |
| 187 * Returns the value stored at the location specified by this value's underlyi
ng reference | |
| 188 * (i.e., the reference obtained by calling [asCellRefToken]), relative to a g
iven [CellLocation]. | |
| 189 */ | |
| 190 Value lookup(CellLocation location) { | |
| 191 if (location == null) { | |
| 192 throw new RuntimeException("Attempt to retrieve primitive value from ref")
; | |
| 193 } | |
| 194 CellLocation refLocation = asCellRefToken().getCellLocation(location); | |
| 195 if (!refLocation.isValidCell()) { | |
| 196 throw new RefException(); | |
| 197 } | |
| 198 return refLocation.spreadsheet.getValue(refLocation.rowCol); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 class CellRefTokenValue extends RefValue { | |
| 203 CellRefToken _crt; | |
| 204 | |
| 205 CellRefTokenValue(this._crt) : super(Value.TYPE_CELLREF) { } | |
| 206 | |
| 207 CellRefToken asCellRefToken() => _crt; | |
| 208 | |
| 209 RangeToken asRangeToken() { | |
| 210 // Create a range consisting of a single cell | |
| 211 return new RangeToken(_crt, _crt); | |
| 212 } | |
| 213 | |
| 214 String toString() => "CellRefTokenValue[${_crt.toDebugString()}]"; | |
| 215 } | |
| 216 | |
| 217 class RangeTokenValue extends RefValue { | |
| 218 RangeToken _rt; | |
| 219 | |
| 220 RangeTokenValue(this._rt) : super(Value.TYPE_RANGE) {} | |
| 221 | |
| 222 CellRefToken asCellRefToken() => _rt.startRef; | |
| 223 | |
| 224 RangeToken asRangeToken() => _rt; | |
| 225 | |
| 226 String toString() => "RangeTokenValue[${_rt.toDebugString()}]"; | |
| 227 } | |
| OLD | NEW |