01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.core;
21 
22 import js.*;
23 import js.core.*;
24 
25 public final class ObjectLikes extends Disposable
26 {
27     private ObjectLikes() {}
28 
29     public static final boolean delete(ObjectLike o, String name) {
30         return o.delete(name);
31     }
32 
33     public static final boolean delete(ObjectLike o, Id<?> id) {
34         return o.delete(id);
35     }
36 
37     public static final <T> void delete(ObjectLike o, ArrayLike<Id<T>> ids) {
38         for (int i = 0, len = ArrayLikes.length(ids); i < len; i++) {
39             delete(o, ids.get(i));
40         }
41     }
42 
43     public static final int get(ObjectLike o, Id<Integer> id) {
44         Integer r = o.var(id);
45         if (Js.not(r)) {
46             r = 0;
47         }
48         return r;
49     }
50 
51     public static final void inc(ObjectLike o, Id<Integer> id) {
52         o.var(id, get(o, id) + 1);
53     }
54 
55     public static final void dec(ObjectLike o, Id<Integer> id) {
56         o.var(id, get(o, id) - 1);
57     }
58 
59     public static final void each(ObjectLike o, JsFunction<Boolean> f) {
60         ArrayLike<String> keys = Js.keys(o);
61         for (int i = 0, len = ArrayLikes.length(keys); i < len; i++) {
62             String k = keys.get(i);
63             if (!f.call(o, k)) {
64                 return;
65             }
66         }
67     }
68 
69     public static final boolean retrieve(ObjectLike o, ObjectLike a) {
70         boolean ret = false;
71         ArrayLike<String> keys = Js.keys(o);
72         for (int i = 0, len = ArrayLikes.length(keys); i < len; i++) {
73             String k = keys.get(i);
74             Object v = a.var(k);
75             if (Variables.defined(v)) {
76                 o.var(k, v);
77                 ret = true;
78             }
79         }
80         return ret;
81     }
82 }