001 
002 /*
003  *  Descripter 1.0 - Java Script Engines
004  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.descripter.js.api.global;
021 
022 import org.descripter.js.api.Core;
023 import org.descripter.js.api.Function;
024 import org.descripter.js.api.Functor;
025 import org.descripter.js.api.Script;
026 import org.descripter.js.api.core.CDate;
027 import org.descripter.js.api.core.CObject;
028 
029 /**
030  * <p>Emulates the global Date function in JavaScript.</p>
031  * 
032  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.descripter.org" target="_blank">http://www.descripter.org</a>
033  * @since Descripter 1.0
034  */
035 public class Date_ extends Function<Core>
036 {
037     /**
038      * <p>Creates the singleton of the global {@link Function} object of this type.</p>
039      * @param core The {@link Core} global script context
040      * @since Descripter 1.0
041      */
042     public static final void create(Core core) {
043         if (core.get(core._Date) == null) {
044             core.put(core._Date, new Date_(core));
045         }
046     }
047 
048     private Date_(Core core) {
049         super(core);
050         CObject prototype = prototype();
051         prototype.put(core._getDate, new Function<Core>(core) {
052             @Override
053             protected Functor<Core> functor() {
054                 return new Functor<Core>(this) {
055                     @Override
056                     public Object function() {
057                         return ((CDate)this_()).getDate();
058                     }
059                 };
060             }
061         });
062         prototype.put(core._getDay, new Function<Core>(core) {
063             @Override
064             protected Functor<Core> functor() {
065                 return new Functor<Core>(this) {
066                     @Override
067                     public Object function() {
068                         return ((CDate)this_()).getDay();
069                     }
070                 };
071             }
072         });
073         prototype.put(core._getFullYear, new Function<Core>(core) {
074             @Override
075             protected Functor<Core> functor() {
076                 return new Functor<Core>(this) {
077                     @Override
078                     public Object function() {
079                         return ((CDate)this_()).getFullYear();
080                     }
081                 };
082             }
083         });
084         prototype.put(core._getHours, new Function<Core>(core) {
085             @Override
086             protected Functor<Core> functor() {
087                 return new Functor<Core>(this) {
088                     @Override
089                     public Object function() {
090                         return ((CDate)this_()).getHours();
091                     }
092                 };
093             }
094         });
095         prototype.put(core._getMilliseconds, new Function<Core>(core) {
096             @Override
097             protected Functor<Core> functor() {
098                 return new Functor<Core>(this) {
099                     @Override
100                     public Object function() {
101                         return ((CDate)this_()).getMilliseconds();
102                     }
103                 };
104             }
105         });
106         prototype.put(core._getMinutes, new Function<Core>(core) {
107             @Override
108             protected Functor<Core> functor() {
109                 return new Functor<Core>(this) {
110                     @Override
111                     public Object function() {
112                         return ((CDate)this_()).getMinutes();
113                     }
114                 };
115             }
116         });
117         prototype.put(core._getMonth, new Function<Core>(core) {
118             @Override
119             protected Functor<Core> functor() {
120                 return new Functor<Core>(this) {
121                     @Override
122                     public Object function() {
123                         return ((CDate)this_()).getMonth();
124                     }
125                 };
126             }
127         });
128         prototype.put(core._getSeconds, new Function<Core>(core) {
129             @Override
130             protected Functor<Core> functor() {
131                 return new Functor<Core>(this) {
132                     @Override
133                     public Object function() {
134                         return ((CDate)this_()).getSeconds();
135                     }
136                 };
137             }
138         });
139         prototype.put(core._getTime, new Function<Core>(core) {
140             @Override
141             protected Functor<Core> functor() {
142                 return new Functor<Core>(this) {
143                     @Override
144                     public Object function() {
145                         return ((CDate)this_()).getTime();
146                     }
147                 };
148             }
149         });
150         prototype.put(core._getTimezoneOffset, new Function<Core>(core) {
151             @Override
152             protected Functor<Core> functor() {
153                 return new Functor<Core>(this) {
154                     @Override
155                     public Object function() {
156                         return ((CDate)this_()).getTimezoneOffset();
157                     }
158                 };
159             }
160         });
161         prototype.put(core._getUTCDate, new Function<Core>(core) {
162             @Override
163             protected Functor<Core> functor() {
164                 return new Functor<Core>(this) {
165                     @Override
166                     public Object function() {
167                         return ((CDate)this_()).getUTCDate();
168                     }
169                 };
170             }
171         });
172         prototype.put(core._getUTCDay, new Function<Core>(core) {
173             @Override
174             protected Functor<Core> functor() {
175                 return new Functor<Core>(this) {
176                     @Override
177                     public Object function() {
178                         return ((CDate)this_()).getUTCDay();
179                     }
180                 };
181             }
182         });
183         prototype.put(core._getUTCFullYear, new Function<Core>(core) {
184             @Override
185             protected Functor<Core> functor() {
186                 return new Functor<Core>(this) {
187                     @Override
188                     public Object function() {
189                         return ((CDate)this_()).getUTCFullYear();
190                     }
191                 };
192             }
193         });
194         prototype.put(core._getUTCHours, new Function<Core>(core) {
195             @Override
196             protected Functor<Core> functor() {
197                 return new Functor<Core>(this) {
198                     @Override
199                     public Object function() {
200                         return ((CDate)this_()).getUTCHours();
201                     }
202                 };
203             }
204         });
205         prototype.put(core._getUTCMilliseconds, new Function<Core>(core) {
206             @Override
207             protected Functor<Core> functor() {
208                 return new Functor<Core>(this) {
209                     @Override
210                     public Object function() {
211                         return ((CDate)this_()).getUTCMilliseconds();
212                     }
213                 };
214             }
215         });
216         prototype.put(core._getUTCMinutes, new Function<Core>(core) {
217             @Override
218             protected Functor<Core> functor() {
219                 return new Functor<Core>(this) {
220                     @Override
221                     public Object function() {
222                         return ((CDate)this_()).getUTCMinutes();
223                     }
224                 };
225             }
226         });
227         prototype.put(core._getUTCMonth, new Function<Core>(core) {
228             @Override
229             protected Functor<Core> functor() {
230                 return new Functor<Core>(this) {
231                     @Override
232                     public Object function() {
233                         return ((CDate)this_()).getUTCMonth();
234                     }
235                 };
236             }
237         });
238         prototype.put(core._getUTCSeconds, new Function<Core>(core) {
239             @Override
240             protected Functor<Core> functor() {
241                 return new Functor<Core>(this) {
242                     @Override
243                     public Object function() {
244                         return ((CDate)this_()).getUTCSeconds();
245                     }
246                 };
247             }
248         });
249         prototype.put(core._setDate, new Function<Core>(core) {
250             @Override
251             protected Functor<Core> functor() {
252                 return new Functor<Core>(this) {
253                     @Override
254                     public Object function() {
255                         return ((CDate)this_()).setDate(toNumber(arguments().get(0)));
256                     }
257                 };
258             }
259         });
260         prototype.put(core._setFullYear, new Function<Core>(core) {
261             @Override
262             protected Functor<Core> functor() {
263                 return new Functor<Core>(this) {
264                     @Override
265                     public Object function() {
266                         return ((CDate)this_()).setFullYear(arguments());
267                     }
268                 };
269             }
270         });
271         prototype.put(core._setHours, new Function<Core>(core) {
272             @Override
273             protected Functor<Core> functor() {
274                 return new Functor<Core>(this) {
275                     @Override
276                     public Object function() {
277                         return ((CDate)this_()).setHours(arguments());
278                     }
279                 };
280             }
281         });
282         prototype.put(core._setMilliseconds, new Function<Core>(core) {
283             @Override
284             protected Functor<Core> functor() {
285                 return new Functor<Core>(this) {
286                     @Override
287                     public Object function() {
288                         return ((CDate)this_()).setMilliseconds(toNumber(arguments().get(0)));
289                     }
290                 };
291             }
292         });
293         prototype.put(core._setMinutes, new Function<Core>(core) {
294             @Override
295             protected Functor<Core> functor() {
296                 return new Functor<Core>(this) {
297                     @Override
298                     public Object function() {
299                         return ((CDate)this_()).setMinutes(arguments());
300                     }
301                 };
302             }
303         });
304         prototype.put(core._setMonth, new Function<Core>(core) {
305             @Override
306             protected Functor<Core> functor() {
307                 return new Functor<Core>(this) {
308                     @Override
309                     public Object function() {
310                         return ((CDate)this_()).setMonth(arguments());
311                     }
312                 };
313             }
314         });
315         prototype.put(core._setSeconds, new Function<Core>(core) {
316             @Override
317             protected Functor<Core> functor() {
318                 return new Functor<Core>(this) {
319                     @Override
320                     public Object function() {
321                         return ((CDate)this_()).setSeconds(arguments());
322                     }
323                 };
324             }
325         });
326         prototype.put(core._setTime, new Function<Core>(core) {
327             @Override
328             protected Functor<Core> functor() {
329                 return new Functor<Core>(this) {
330                     @Override
331                     public Object function() {
332                         return ((CDate)this_()).setTime(toNumber(arguments().get(0)));
333                     }
334                 };
335             }
336         });
337         prototype.put(core._setUTCDate, new Function<Core>(core) {
338             @Override
339             protected Functor<Core> functor() {
340                 return new Functor<Core>(this) {
341                     @Override
342                     public Object function() {
343                         return ((CDate)this_()).setUTCDate(toNumber(arguments().get(0)));
344                     }
345                 };
346             }
347         });
348         prototype.put(core._setUTCFullYear, new Function<Core>(core) {
349             @Override
350             protected Functor<Core> functor() {
351                 return new Functor<Core>(this) {
352                     @Override
353                     public Object function() {
354                         return ((CDate)this_()).setUTCFullYear(arguments());
355                     }
356                 };
357             }
358         });
359         prototype.put(core._setUTCHours, new Function<Core>(core) {
360             @Override
361             protected Functor<Core> functor() {
362                 return new Functor<Core>(this) {
363                     @Override
364                     public Object function() {
365                         return ((CDate)this_()).setUTCHours(arguments());
366                     }
367                 };
368             }
369         });
370         prototype.put(core._setUTCMilliseconds, new Function<Core>(core) {
371             @Override
372             protected Functor<Core> functor() {
373                 return new Functor<Core>(this) {
374                     @Override
375                     public Object function() {
376                         return ((CDate)this_()).setUTCMilliseconds(toNumber(arguments().get(0)));
377                     }
378                 };
379             }
380         });
381         prototype.put(core._setUTCMinutes, new Function<Core>(core) {
382             @Override
383             protected Functor<Core> functor() {
384                 return new Functor<Core>(this) {
385                     @Override
386                     public Object function() {
387                         return ((CDate)this_()).setUTCMinutes(arguments());
388                     }
389                 };
390             }
391         });
392         prototype.put(core._setUTCMonth, new Function<Core>(core) {
393             @Override
394             protected Functor<Core> functor() {
395                 return new Functor<Core>(this) {
396                     @Override
397                     public Object function() {
398                         return ((CDate)this_()).setUTCMonth(arguments());
399                     }
400                 };
401             }
402         });
403         prototype.put(core._setUTCSeconds, new Function<Core>(core) {
404             @Override
405             protected Functor<Core> functor() {
406                 return new Functor<Core>(this) {
407                     @Override
408                     public Object function() {
409                         return ((CDate)this_()).setUTCSeconds(arguments());
410                     }
411                 };
412             }
413         });
414         prototype.put(core._toDateString, new Function<Core>(core) {
415             @Override
416             protected Functor<Core> functor() {
417                 return new Functor<Core>(this) {
418                     @Override
419                     public Object function() {
420                         return ((CDate)this_()).toDateString();
421                     }
422                 };
423             }
424         });
425         prototype.put(core._toLocaleDateString, new Function<Core>(core) {
426             @Override
427             protected Functor<Core> functor() {
428                 return new Functor<Core>(this) {
429                     @Override
430                     public Object function() {
431                         return ((CDate)this_()).toLocaleDateString();
432                     }
433                 };
434             }
435         });
436         prototype.put(core._toLocaleTimeString, new Function<Core>(core) {
437             @Override
438             protected Functor<Core> functor() {
439                 return new Functor<Core>(this) {
440                     @Override
441                     public Object function() {
442                         return ((CDate)this_()).toLocaleTimeString();
443                     }
444                 };
445             }
446         });
447         prototype.put(core._toTimeString, new Function<Core>(core) {
448             @Override
449             protected Functor<Core> functor() {
450                 return new Functor<Core>(this) {
451                     @Override
452                     public Object function() {
453                         return ((CDate)this_()).toTimeString();
454                     }
455                 };
456             }
457         });
458         prototype.put(core._toUTCString, new Function<Core>(core) {
459             @Override
460             protected Functor<Core> functor() {
461                 return new Functor<Core>(this) {
462                     @Override
463                     public Object function() {
464                         return ((CDate)this_()).toUTCString();
465                     }
466                 };
467             }
468         });
469     }
470 
471     /**
472      * <p>Allocates a new object with this {@link Function}.</p>
473      * @param script The script context that invoked this service
474      * @param args An array of the arguments passed by the invocation
475      * @return The newly allocated object
476      * @since Descripter 1.0
477      */
478     @Override
479     public <S extends Script<?>> CDate alloc(S script, Object ...args) {
480         if (args != null && args.length > 0 && args[0] != null) {
481             Object arg = valueOf(args[0]);
482             if (arg instanceof Number) {
483                 return new CDate(this, (Number)arg);
484             } else if (arg instanceof String) {
485                 return new CDate(this, (String)arg);
486             }
487         }
488         return new CDate(this);
489     }
490 
491     /**
492      * <p>Gets the {@link Functor} of the {@link Function} object.</p>
493      * <p>This is a concrete implementation of the super abstract method.</p>
494      * @return The {@link Functor} of the {@link Function} object
495      * @since Descripter 1.0
496      */
497     @Override
498     protected Functor<Core> functor() {
499         return new Functor<Core>(this) {
500             @Override
501             public Object function() {
502                 return Date_.this.alloc(this, arguments().get(0));
503             }
504         };
505     }
506 }