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