API  0.9.8
 All Classes Files Functions Variables Typedefs 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 
37 @implementation CPNumber : CPObject
38 {
39  id __doxygen__;
40 }
41 
42 + (id)alloc
43 {
44  var result = new Number();
45  result.isa = [self class];
46  return result;
47 }
48 
49 + (id)numberWithBool:(BOOL)aBoolean
50 {
51  return aBoolean ? 1 : 0;
52 }
53 
54 + (id)numberWithChar:(char)aChar
55 {
56  if (aChar.charCodeAt)
57  return aChar.charCodeAt(0);
58 
59  return aChar;
60 }
61 
62 + (id)numberWithDouble:(double)aDouble
63 {
64  return aDouble;
65 }
66 
67 + (id)numberWithFloat:(float)aFloat
68 {
69  return aFloat;
70 }
71 
72 + (id)numberWithInt:(int)anInt
73 {
74  return anInt;
75 }
76 
77 + (id)numberWithLong:(long)aLong
78 {
79  return aLong;
80 }
81 
82 + (id)numberWithLongLong:(long long)aLongLong
83 {
84  return aLongLong;
85 }
86 
87 + (id)numberWithShort:(short)aShort
88 {
89  return aShort;
90 }
91 
92 + (id)numberWithUnsignedChar:(unsigned char)aChar
93 {
94  if (aChar.charCodeAt)
95  return aChar.charCodeAt(0);
96 
97  return aChar;
98 }
99 
100 + (id)numberWithUnsignedInt:(unsigned)anUnsignedInt
101 {
102  return anUnsignedInt;
103 }
104 
105 + (id)numberWithUnsignedLong:(unsigned long)anUnsignedLong
106 {
107  return anUnsignedLong;
108 }
109 /*
110 + (id)numberWithUnsignedLongLong:(unsigned long long)anUnsignedLongLong
111 {
112  return anUnsignedLongLong;
113 }
114 */
115 + (id)numberWithUnsignedShort:(unsigned short)anUnsignedShort
116 {
117  return anUnsignedShort;
118 }
119 
120 - (id)initWithBool:(BOOL)aBoolean
121 {
122  return aBoolean;
123 }
124 
125 - (id)initWithChar:(char)aChar
126 {
127  if (aChar.charCodeAt)
128  return aChar.charCodeAt(0);
129 
130  return aChar;
131 }
132 
133 - (id)initWithDouble:(double)aDouble
134 {
135  return aDouble;
136 }
137 
138 - (id)initWithFloat:(float)aFloat
139 {
140  return aFloat;
141 }
142 
143 - (id)initWithInt:(int)anInt
144 {
145  return anInt;
146 }
147 
148 - (id)initWithLong:(long)aLong
149 {
150  return aLong;
151 }
152 
153 - (id)initWithLongLong:(long long)aLongLong
154 {
155  return aLongLong;
156 }
157 
158 - (id)initWithShort:(short)aShort
159 {
160  return aShort;
161 }
162 
163 - (id)initWithUnsignedChar:(unsigned char)aChar
164 {
165  if (aChar.charCodeAt)
166  return aChar.charCodeAt(0);
167 
168  return aChar;
169 }
170 
171 - (id)initWithUnsignedInt:(unsigned)anUnsignedInt
172 {
173  return anUnsignedInt;
174 }
175 
176 - (id)initWithUnsignedLong:(unsigned long)anUnsignedLong
177 {
178  return anUnsignedLong;
179 }
180 /*
181 - (id)initWithUnsignedLongLong:(unsigned long long)anUnsignedLongLong
182 {
183  return anUnsignedLongLong;
184 }
185 */
186 - (id)initWithUnsignedShort:(unsigned short)anUnsignedShort
187 {
188  return anUnsignedShort;
189 }
190 
191 - (CPString)UID
192 {
193  var UID = CPNumberUIDs.valueForKey(self);
194 
195  if (!UID)
196  {
197  UID = objj_generateObjectUID();
198  CPNumberUIDs.setValueForKey(self, UID);
199  }
200 
201  return UID + "";
202 }
203 
204 - (BOOL)boolValue
205 {
206  // Ensure we return actual booleans.
207  return self ? true : false;
208 }
209 
210 - (char)charValue
211 {
212  return String.fromCharCode(self);
213 }
214 
215 /*
216 FIXME: Do we need this?
217 */
218 - (CPDecimal)decimalValue
219 {
220  throw new Error("decimalValue: NOT YET IMPLEMENTED");
221 }
222 
223 - (CPString)descriptionWithLocale:(CPDictionary)aDictionary
224 {
225  if (!aDictionary)
226  return self.toString();
227 
228  throw new Error("descriptionWithLocale: NOT YET IMPLEMENTED");
229 }
230 
231 - (CPString)description
232 {
233  return [self descriptionWithLocale:nil];
234 }
235 
236 - (double)doubleValue
237 {
238  if (typeof self == "boolean")
239  return self ? 1 : 0;
240  return self;
241 }
242 
243 - (float)floatValue
244 {
245  if (typeof self == "boolean")
246  return self ? 1 : 0;
247  return self;
248 }
249 
250 - (int)intValue
251 {
252  if (typeof self == "boolean")
253  return self ? 1 : 0;
254  return self;
255 }
256 
257 - (long long)longLongValue
258 {
259  if (typeof self == "boolean")
260  return self ? 1 : 0;
261  return self;
262 }
263 
264 - (long)longValue
265 {
266  if (typeof self == "boolean")
267  return self ? 1 : 0;
268  return self;
269 }
270 
271 - (short)shortValue
272 {
273  if (typeof self == "boolean")
274  return self ? 1 : 0;
275  return self;
276 }
277 
278 - (CPString)stringValue
279 {
280  return self.toString();
281 }
282 
283 - (unsigned char)unsignedCharValue
284 {
285  return String.fromCharCode(self);
286 }
287 
288 - (unsigned int)unsignedIntValue
289 {
290  if (typeof self == "boolean")
291  return self ? 1 : 0;
292  return self;
293 }
294 /*
295 - (unsigned long long)unsignedLongLongValue
296 {
297  if (typeof self == "boolean") return self ? 1 : 0;
298  return self;
299 }
300 */
301 - (unsigned long)unsignedLongValue
302 {
303  if (typeof self == "boolean")
304  return self ? 1 : 0;
305  return self;
306 }
307 
308 - (unsigned short)unsignedShortValue
309 {
310  if (typeof self == "boolean")
311  return self ? 1 : 0;
312  return self;
313 }
314 
315 - (CPComparisonResult)compare:(CPNumber)aNumber
316 {
317  if (aNumber === nil || aNumber['isa'] === CPNull)
318  [CPException raise:CPInvalidArgumentException reason:"nil argument"];
319 
320  if (self > aNumber)
321  return CPOrderedDescending;
322  else if (self < aNumber)
323  return CPOrderedAscending;
324 
325  return CPOrderedSame;
326 }
327 
328 - (BOOL)isEqualToNumber:(CPNumber)aNumber
329 {
330  return self == aNumber;
331 }
332 
333 @end
334 
335 @implementation CPNumber (CPCoding)
336 
337 - (id)initWithCoder:(CPCoder)aCoder
338 {
339  return [aCoder decodeNumber];
340 }
341 
342 - (void)encodeWithCoder:(CPCoder)aCoder
343 {
344  [aCoder encodeNumber:self forKey:@"self"];
345 }
346 
347 @end
348 
349 Number.prototype.isa = CPNumber;
350 Boolean.prototype.isa = CPNumber;