Revision: 4550
          http://sourceforge.net/p/vexi/code/4550
Author:   mkpg2
Date:     2013-09-10 22:56:05 +0000 (Tue, 10 Sep 2013)
Log Message:
-----------
Create 'static' versions of the addPeriod and as (renamed cast) date functions 
- on the constructor.
Useful in emanate expressions. These generally return null if any of the 
arguments are null. 
Possibly this should be the convention... Arithmetic style methods on the 
constructor should return null if arguments are null, instead of throwing 
errors. Methods on the instance should throw errors if arguments are null.

Modified Paths:
--------------
    branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/JSU.java
    branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp

Modified: branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/JSU.java
===================================================================
--- branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/JSU.java       
2013-09-07 00:11:07 UTC (rev 4549)
+++ branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/JSU.java       
2013-09-10 22:56:05 UTC (rev 4550)
@@ -319,6 +319,12 @@
         if(js==null) return default_;
         return expectArg_int(args, index);
     }
+    static public Integer expectArg_Integer(JS[] args, int index) throws JSExn 
{
+        JS js = expectArg(args, index);
+        if(js==null) return null;
+        if(isInt(js)) return toInt32(js);
+        throw new JSExn("Arg "+index+" is not an integer: "+toString(js));     
   
+    }
     static public String getArg_string(JS[] args, int index) throws JSExn {
         JS js = getArg(args, index);
         if(js==null) return null;

Modified: branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp
===================================================================
--- branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp      
2013-09-07 00:11:07 UTC (rev 4549)
+++ branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp      
2013-09-10 22:56:05 UTC (rev 4550)
@@ -35,8 +35,16 @@
 public class JSDate extends JS.Immutable {
 
     static private JSDate expectDate(JS[] args, int i) throws JSExn {
-        JS arg = JSU.expectArg(args, i);
-        if (arg==null || !(arg instanceof JSDate)) 
+        JSDate arg = expectDateOrNull(args, i);
+        if (arg==null ) 
+            throw new JSExn("Argument "+i+" expected date, got null");
+        return arg;
+    }
+    
+    static private JSDate expectDateOrNull(JS[] args, int i) throws JSExn {
+       JS arg = JSU.expectArg(args, i);
+        if (arg==null) return null;
+        if(!(arg instanceof JSDate)) 
             throw new JSExn("Argument "+i+" expected date, got: 
"+JSU.toString(arg));
         return (JSDate)arg;
     }
@@ -57,7 +65,11 @@
         
         public JS get(JS key) throws JSExn {
             //#switch(JSU.toString(key))
-            case "today":
+               case "addPeriod":
+                       return METHOD;
+               case "cast":
+                       return METHOD;                          
+               case "today":
                 return new JSDate(new 
Instant().getDate(JSInstant.getTimeZone()));
             case "compare": 
                 return METHOD;
@@ -69,26 +81,36 @@
         }
         
         public JS callMethod(JS this_, JS method, JS[] args) throws JSExn {
-            switch(args.length) {
-                case 1: {
-                    //#switch(JSU.toString(method))
-                    case "tryParseString":
-                        String s = JSU.toString(args[0]);
-                        Date d = Date.tryParseString(s);
-                        if(d==null) return null;
-                        return new JSDate(d);
-                    //#end
-                }
-                case 2: {
-                    //#switch(JSU.toString(method))
-                    case "compare":
-                        JSDate a = expectDate(args,0); 
-                        JSDate b = expectDate(args,1); 
-                        return JSU.N(a.date.compareTo(b.date));
-                    //#end
-                }
-            }
-            return super.callMethod(this_, method, args);
+               try{
+                   //#switch(JSU.toString(method))
+                       case "addPeriod":
+                               JSDate a = expectDateOrNull(args,0); 
+                               if(a==null) return null;
+                               String part = JSU.getArg_string(args, 1);
+                       Integer amount = JSU.expectArg_Integer(args,2);
+                               if(amount==null) return null;
+                               int partId = Date.partId(part);
+                       return new JSDate(a.date.addPeriod(partId, amount));
+                       case "cast":
+                               JSDate a = expectDateOrNull(args,0); 
+                               if(a==null) return null;
+                               String scheme = JSU.expectArg_string(args, 1);
+                       return new JSDate(a.date.as(scheme));
+                   case "tryParseString":
+                       String s = JSU.getArg_string(args,0);
+                       Date d = Date.tryParseString(s);
+                       if(d==null) return null;
+                       return new JSDate(d);
+                   case "compare":
+                       JSDate a = expectDateOrNull(args,0); 
+                       JSDate b = expectDateOrNull(args,1); 
+                       if(a==null || b==null) return null;
+                       return JSU.N(a.date.compareTo(b.date));
+                   //#end
+                   return super.callMethod(this_, method, args);
+               }catch(ValueException e){
+                       throw new JSExn(e.getMessage());
+               }
         }
 
     };

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. Consolidate legacy IT systems to a single system of record for IT
2. Standardize and globalize service processes across IT
3. Implement zero-touch automation to replace manual, redundant tasks
http://pubads.g.doubleclick.net/gampad/clk?id=51271111&iu=/4140/ostg.clktrk
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to