![]() |
API 0.9.5
|
00001 /* 00002 * CPNumber.j 00003 * Foundation 00004 * 00005 * Created by Francisco Tolmasky. 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00024 var CPNumberUIDs = new CFMutableDictionary(); 00025 00038 @implementation CPNumber : CPObject 00039 { 00040 id __doxygen__; 00041 } 00042 00043 + (id)alloc 00044 { 00045 var result = new Number(); 00046 result.isa = [self class]; 00047 return result; 00048 } 00049 00050 + (id)numberWithBool:(BOOL)aBoolean 00051 { 00052 return aBoolean; 00053 } 00054 00055 + (id)numberWithChar:(char)aChar 00056 { 00057 if (aChar.charCodeAt) 00058 return aChar.charCodeAt(0); 00059 00060 return aChar; 00061 } 00062 00063 + (id)numberWithDouble:(double)aDouble 00064 { 00065 return aDouble; 00066 } 00067 00068 + (id)numberWithFloat:(float)aFloat 00069 { 00070 return aFloat; 00071 } 00072 00073 + (id)numberWithInt:(int)anInt 00074 { 00075 return anInt; 00076 } 00077 00078 + (id)numberWithLong:(long)aLong 00079 { 00080 return aLong; 00081 } 00082 00083 + (id)numberWithLongLong:(long long)aLongLong 00084 { 00085 return aLongLong; 00086 } 00087 00088 + (id)numberWithShort:(short)aShort 00089 { 00090 return aShort; 00091 } 00092 00093 + (id)numberWithUnsignedChar:(unsigned char)aChar 00094 { 00095 if (aChar.charCodeAt) 00096 return aChar.charCodeAt(0); 00097 00098 return aChar; 00099 } 00100 00101 + (id)numberWithUnsignedInt:(unsigned)anUnsignedInt 00102 { 00103 return anUnsignedInt; 00104 } 00105 00106 + (id)numberWithUnsignedLong:(unsigned long)anUnsignedLong 00107 { 00108 return anUnsignedLong; 00109 } 00110 /* 00111 + (id)numberWithUnsignedLongLong:(unsigned long long)anUnsignedLongLong 00112 { 00113 return anUnsignedLongLong; 00114 } 00115 */ 00116 + (id)numberWithUnsignedShort:(unsigned short)anUnsignedShort 00117 { 00118 return anUnsignedShort; 00119 } 00120 00121 - (id)initWithBool:(BOOL)aBoolean 00122 { 00123 return aBoolean; 00124 } 00125 00126 - (id)initWithChar:(char)aChar 00127 { 00128 if (aChar.charCodeAt) 00129 return aChar.charCodeAt(0); 00130 00131 return aChar; 00132 } 00133 00134 - (id)initWithDouble:(double)aDouble 00135 { 00136 return aDouble; 00137 } 00138 00139 - (id)initWithFloat:(float)aFloat 00140 { 00141 return aFloat; 00142 } 00143 00144 - (id)initWithInt:(int)anInt 00145 { 00146 return anInt; 00147 } 00148 00149 - (id)initWithLong:(long)aLong 00150 { 00151 return aLong; 00152 } 00153 00154 - (id)initWithLongLong:(long long)aLongLong 00155 { 00156 return aLongLong; 00157 } 00158 00159 - (id)initWithShort:(short)aShort 00160 { 00161 return aShort; 00162 } 00163 00164 - (id)initWithUnsignedChar:(unsigned char)aChar 00165 { 00166 if (aChar.charCodeAt) 00167 return aChar.charCodeAt(0); 00168 00169 return aChar; 00170 } 00171 00172 - (id)initWithUnsignedInt:(unsigned)anUnsignedInt 00173 { 00174 return anUnsignedInt; 00175 } 00176 00177 - (id)initWithUnsignedLong:(unsigned long)anUnsignedLong 00178 { 00179 return anUnsignedLong; 00180 } 00181 /* 00182 - (id)initWithUnsignedLongLong:(unsigned long long)anUnsignedLongLong 00183 { 00184 return anUnsignedLongLong; 00185 } 00186 */ 00187 - (id)initWithUnsignedShort:(unsigned short)anUnsignedShort 00188 { 00189 return anUnsignedShort; 00190 } 00191 00192 - (CPString)UID 00193 { 00194 var UID = CPNumberUIDs.valueForKey(self); 00195 00196 if (!UID) 00197 { 00198 UID = objj_generateObjectUID(); 00199 CPNumberUIDs.setValueForKey(self, UID); 00200 } 00201 00202 return UID + ""; 00203 } 00204 00205 - (BOOL)boolValue 00206 { 00207 // Ensure we return actual booleans. 00208 return self ? true : false; 00209 } 00210 00211 - (char)charValue 00212 { 00213 return String.fromCharCode(self); 00214 } 00215 00216 /* 00217 FIXME: Do we need this? 00218 */ 00219 - (CPDecimal)decimalValue 00220 { 00221 throw new Error("decimalValue: NOT YET IMPLEMENTED"); 00222 } 00223 00224 - (CPString)descriptionWithLocale:(CPDictionary)aDictionary 00225 { 00226 if (!aDictionary) return toString(); 00227 00228 throw new Error("descriptionWithLocale: NOT YET IMPLEMENTED"); 00229 } 00230 00231 - (CPString)description 00232 { 00233 return [self descriptionWithLocale:nil]; 00234 } 00235 00236 - (double)doubleValue 00237 { 00238 if (typeof self == "boolean") return self ? 1 : 0; 00239 return self; 00240 } 00241 00242 - (float)floatValue 00243 { 00244 if (typeof self == "boolean") return self ? 1 : 0; 00245 return self; 00246 } 00247 00248 - (int)intValue 00249 { 00250 if (typeof self == "boolean") return self ? 1 : 0; 00251 return self; 00252 } 00253 00254 - (long long)longLongValue 00255 { 00256 if (typeof self == "boolean") return self ? 1 : 0; 00257 return self; 00258 } 00259 00260 - (long)longValue 00261 { 00262 if (typeof self == "boolean") return self ? 1 : 0; 00263 return self; 00264 } 00265 00266 - (short)shortValue 00267 { 00268 if (typeof self == "boolean") return self ? 1 : 0; 00269 return self; 00270 } 00271 00272 - (CPString)stringValue 00273 { 00274 return toString(); 00275 } 00276 00277 - (unsigned char)unsignedCharValue 00278 { 00279 return String.fromCharCode(self); 00280 } 00281 00282 - (unsigned int)unsignedIntValue 00283 { 00284 if (typeof self == "boolean") return self ? 1 : 0; 00285 return self; 00286 } 00287 /* 00288 - (unsigned long long)unsignedLongLongValue 00289 { 00290 if (typeof self == "boolean") return self ? 1 : 0; 00291 return self; 00292 } 00293 */ 00294 - (unsigned long)unsignedLongValue 00295 { 00296 if (typeof self == "boolean") return self ? 1 : 0; 00297 return self; 00298 } 00299 00300 - (unsigned short)unsignedShortValue 00301 { 00302 if (typeof self == "boolean") return self ? 1 : 0; 00303 return self; 00304 } 00305 00306 - (CPComparisonResult)compare:(CPNumber)aNumber 00307 { 00308 if (self > aNumber) return CPOrderedDescending; 00309 else if (self < aNumber) return CPOrderedAscending; 00310 00311 return CPOrderedSame; 00312 } 00313 00314 - (BOOL)isEqualToNumber:(CPNumber)aNumber 00315 { 00316 return self == aNumber; 00317 } 00318 00319 @end 00320 00321 @implementation CPNumber (CPCoding) 00322 00323 - (id)initWithCoder:(CPCoder)aCoder 00324 { 00325 return [aCoder decodeNumber]; 00326 } 00327 00328 - (void)encodeWithCoder:(CPCoder)aCoder 00329 { 00330 [aCoder encodeNumber:self forKey:@"self"]; 00331 } 00332 00333 @end 00334 00335 Number.prototype.isa = CPNumber; 00336 Boolean.prototype.isa = CPNumber; 00337 [CPNumber initialize];