API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
CPNumber.j
Go to the documentation of this file.
1 /*
2  * CPNumber.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 var CPNumberUIDs = new CFMutableDictionary();
25 
38 @implementation CPNumber : CPObject
39 {
40  id __doxygen__;
41 }
42 
43 + (id)alloc
44 {
45  var result = new Number();
46  result.isa = [self class];
47  return result;
48 }
49 
50 + (id)numberWithBool:(BOOL)aBoolean
51 {
52  return aBoolean ? 1 : 0;
53 }
54 
55 + (id)numberWithChar:(char)aChar
56 {
57  if (aChar.charCodeAt)
58  return aChar.charCodeAt(0);
59 
60  return aChar;
61 }
62 
63 + (id)numberWithDouble:(double)aDouble
64 {
65  return aDouble;
66 }
67 
68 + (id)numberWithFloat:(float)aFloat
69 {
70  return aFloat;
71 }
72 
73 + (id)numberWithInt:(int)anInt
74 {
75  return anInt;
76 }
77 
78 + (id)numberWithLong:(long)aLong
79 {
80  return aLong;
81 }
82 
83 + (id)numberWithLongLong:(long long)aLongLong
84 {
85  return aLongLong;
86 }
87 
88 + (id)numberWithShort:(short)aShort
89 {
90  return aShort;
91 }
92 
93 + (id)numberWithUnsignedChar:(unsigned char)aChar
94 {
95  if (aChar.charCodeAt)
96  return aChar.charCodeAt(0);
97 
98  return aChar;
99 }
100 
101 + (id)numberWithUnsignedInt:(unsigned)anUnsignedInt
102 {
103  return anUnsignedInt;
104 }
105 
106 + (id)numberWithUnsignedLong:(unsigned long)anUnsignedLong
107 {
108  return anUnsignedLong;
109 }
110 /*
111 + (id)numberWithUnsignedLongLong:(unsigned long long)anUnsignedLongLong
112 {
113  return anUnsignedLongLong;
114 }
115 */
116 + (id)numberWithUnsignedShort:(unsigned short)anUnsignedShort
117 {
118  return anUnsignedShort;
119 }
120 
121 - (id)initWithBool:(BOOL)aBoolean
122 {
123  return aBoolean;
124 }
125 
126 - (id)initWithChar:(char)aChar
127 {
128  if (aChar.charCodeAt)
129  return aChar.charCodeAt(0);
130 
131  return aChar;
132 }
133 
134 - (id)initWithDouble:(double)aDouble
135 {
136  return aDouble;
137 }
138 
139 - (id)initWithFloat:(float)aFloat
140 {
141  return aFloat;
142 }
143 
144 - (id)initWithInt:(int)anInt
145 {
146  return anInt;
147 }
148 
149 - (id)initWithLong:(long)aLong
150 {
151  return aLong;
152 }
153 
154 - (id)initWithLongLong:(long long)aLongLong
155 {
156  return aLongLong;
157 }
158 
159 - (id)initWithShort:(short)aShort
160 {
161  return aShort;
162 }
163 
164 - (id)initWithUnsignedChar:(unsigned char)aChar
165 {
166  if (aChar.charCodeAt)
167  return aChar.charCodeAt(0);
168 
169  return aChar;
170 }
171 
172 - (id)initWithUnsignedInt:(unsigned)anUnsignedInt
173 {
174  return anUnsignedInt;
175 }
176 
177 - (id)initWithUnsignedLong:(unsigned long)anUnsignedLong
178 {
179  return anUnsignedLong;
180 }
181 /*
182 - (id)initWithUnsignedLongLong:(unsigned long long)anUnsignedLongLong
183 {
184  return anUnsignedLongLong;
185 }
186 */
187 - (id)initWithUnsignedShort:(unsigned short)anUnsignedShort
188 {
189  return anUnsignedShort;
190 }
191 
192 - (CPString)UID
193 {
194  var UID = CPNumberUIDs.valueForKey(self);
195 
196  if (!UID)
197  {
198  UID = objj_generateObjectUID();
199  CPNumberUIDs.setValueForKey(self, UID);
200  }
201 
202  return UID + "";
203 }
204 
205 - (BOOL)boolValue
206 {
207  // Ensure we return actual booleans.
208  return self ? true : false;
209 }
210 
211 - (char)charValue
212 {
213  return String.fromCharCode(self);
214 }
215 
216 /*
217 FIXME: Do we need this?
218 */
219 - (CPDecimal)decimalValue
220 {
221  throw new Error("decimalValue: NOT YET IMPLEMENTED");
222 }
223 
224 - (CPString)descriptionWithLocale:(CPDictionary)aDictionary
225 {
226  if (!aDictionary)
227  return self.toString();
228 
229  throw new Error("descriptionWithLocale: NOT YET IMPLEMENTED");
230 }
231 
232 - (CPString)description
233 {
234  return [self descriptionWithLocale:nil];
235 }
236 
237 - (double)doubleValue
238 {
239  if (typeof self == "boolean")
240  return self ? 1 : 0;
241  return self;
242 }
243 
244 - (float)floatValue
245 {
246  if (typeof self == "boolean")
247  return self ? 1 : 0;
248  return self;
249 }
250 
251 - (int)intValue
252 {
253  if (typeof self == "boolean")
254  return self ? 1 : 0;
255  return self;
256 }
257 
258 - (long long)longLongValue
259 {
260  if (typeof self == "boolean")
261  return self ? 1 : 0;
262  return self;
263 }
264 
265 - (long)longValue
266 {
267  if (typeof self == "boolean")
268  return self ? 1 : 0;
269  return self;
270 }
271 
272 - (short)shortValue
273 {
274  if (typeof self == "boolean")
275  return self ? 1 : 0;
276  return self;
277 }
278 
279 - (CPString)stringValue
280 {
281  return self.toString();
282 }
283 
284 - (unsigned char)unsignedCharValue
285 {
286  return String.fromCharCode(self);
287 }
288 
289 - (unsigned int)unsignedIntValue
290 {
291  if (typeof self == "boolean")
292  return self ? 1 : 0;
293  return self;
294 }
295 /*
296 - (unsigned long long)unsignedLongLongValue
297 {
298  if (typeof self == "boolean") return self ? 1 : 0;
299  return self;
300 }
301 */
302 - (unsigned long)unsignedLongValue
303 {
304  if (typeof self == "boolean")
305  return self ? 1 : 0;
306  return self;
307 }
308 
309 - (unsigned short)unsignedShortValue
310 {
311  if (typeof self == "boolean")
312  return self ? 1 : 0;
313  return self;
314 }
315 
316 - (CPComparisonResult)compare:(CPNumber)aNumber
317 {
318  if (aNumber === nil || aNumber['isa'] === CPNull)
319  [CPException raise:CPInvalidArgumentException reason:"nil argument"];
320 
321  if (self > aNumber)
322  return CPOrderedDescending;
323  else if (self < aNumber)
324  return CPOrderedAscending;
325 
326  return CPOrderedSame;
327 }
328 
329 - (BOOL)isEqualToNumber:(CPNumber)aNumber
330 {
331  return self == aNumber;
332 }
333 
334 @end
335 
336 @implementation CPNumber (CPCoding)
337 
338 - (id)initWithCoder:(CPCoder)aCoder
339 {
340  return [aCoder decodeNumber];
341 }
342 
343 - (void)encodeWithCoder:(CPCoder)aCoder
344 {
345  [aCoder encodeNumber:self forKey:@"self"];
346 }
347 
348 @end
349 
350 Number.prototype.isa = CPNumber;
351 Boolean.prototype.isa = CPNumber;
352 [CPNumber initialize];