001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
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 js;
021 
022 /**
023  * <p>An <b>opaque</b> class resembling JavaScript object initializers (also known 
024  * as object literals).</p>
025  * <p>JavaScript defines an object literal syntax that allows you to create an object 
026  * and specify its properties. An object literal (also called an object initializer) 
027  * consists of a comma-separated list of colon-separated property/value pairs, all 
028  * enclosed within curly braces.</p>
029  * <p>In JavaScript, the property values used in object literals need not be constants; 
030  * they can be arbitrary expressions. Also, the property names in object literals may 
031  * be string literal rather than identifiers.</p>
032  *
033  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
034  * @see Js#object()
035  * @see Js#object(Object)
036  * @see js.core.JsObject#JsObject()
037  * @see js.core.JsGlobal.Object#create()
038  * 
039  * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be 
040  * generated into the target codes. Re-compilers must exit with error on the operations of 
041  * accessing that kind of class objects.
042  * Re-compilers should convert Java expressions like:
043  * <pre>new Initializer().set("label1", v1).set("label2", v2).set("label3", v3).var()</pre>
044  * or
045  * <pre>new Initializer().set("label1", v1).set("label2", v2).set("label3", v3)</pre>
046  * into JavaScript expressions as:
047  * <pre>{"label1":v1,"label2":v2,"label3":v3}</pre>
048  */
049 
050 public final class Initializer extends Var<ObjectLike>
051 {
052     private final ObjectLike o;
053 
054     /**
055      * <p>Creates an <tt>Initializer</tt> cache.</p>
056      * @since 1.0
057      * @javascript Re-compilers must create a re-compile time <tt>Initializer</tt> cache 
058      * without generating any JavaScript statements or expressions for this constructor.
059      */
060     public Initializer() {
061         o = Js.object();
062     }
063 
064     /**
065      * <p>Converts the current <tt>Initializer</tt> cache instance to an object.</p>
066      * @return The object represented by the current <tt>Initializer</tt> cache itself.
067      * @since 1.0
068      * @javascript Re-compilers must replace the virtual invocation of this method with the 
069      * JavaScript object literal represented by the current <tt>Initializer</tt> cache 
070      * instance they have kept.
071      */
072     @Override
073     public final ObjectLike var() {
074         return o;
075     }
076 
077     /**
078      * <p>Appends a property-value pair to the current <tt>Initializer</tt> cache 
079      * instance.</p>
080      * @param label The member ID of the property to set <tt>value</tt> to the current 
081      * <tt>Initializer</tt> cache instance.
082      * @param value The value to be set to the <tt>label</tt> of the current 
083      * <tt>Initializer</tt> cache instance.
084      * @return The current <tt>Initializer</tt> cache instance itself.
085      * @since 1.0
086      * @javascript Re-compilers must append the property-value pair, that is <tt>label:value</tt>, 
087      * to the current re-compile time <tt>Initializer</tt> cache they have kept, without 
088      * generating any JavaScript statements or expressions for the virtual invocation of 
089      * this method, where <tt>label</tt> must be resolved to the property identifier.
090      */
091     private final <T> Initializer set(Mid label, T value) {
092         o.var(label, value);
093         return this;
094     }
095     /**
096      * <p>Appends a property-value pair to the current <tt>Initializer</tt> cache 
097      * instance.</p>
098      * @param label The member ID of the property to set <tt>value</tt> on the current 
099      * <tt>Initializer</tt> cache instance. This argument must be a string literal. 
100      * Otherwise, JS compilers shall report an error.
101      * @param value The value to be set to the <tt>label</tt> of the current 
102      * <tt>Initializer</tt> cache instance.
103      * @return The current <tt>Initializer</tt> cache instance itself.
104      * @since 1.0
105      * @javascript Re-compilers must append the property-value pair, that is <tt>label:value</tt>, 
106      * to the current re-compile time <tt>Initializer</tt> cache they have kept without 
107      * generating any JavaScript statements or expressions for the virtual invocation of 
108      * this method and report error if <tt>label</tt> can not be resolved to a string 
109      * literal.
110      */
111     public final Initializer set(String label, Object value) {
112         return set(Members.id(label), value);
113     }
114     /**
115      * <p>Appends a property-value pair to the current <tt>Initializer</tt> cache 
116      * instance.</p>
117      * @param label The member ID of the property to set the <tt>value</tt> on the current 
118      * <tt>Initializer</tt> cache instance. This argument must be an integer literal. 
119      * Otherwise, JS compilers shall report an error.
120      * @param value The value to be set to the <tt>label</tt> of the current 
121      * <tt>Initializer</tt> cache instance.
122      * @return The current <tt>Initializer</tt> cache instance itself.
123      * @since 1.0
124      * @javascript Re-compilers must append the property-value pair, that is <tt>label:value</tt>, 
125      * to the current re-compile time <tt>Initializer</tt> cache they have kept without 
126      * generating any JavaScript statements or expressions for the virtual invocation of 
127      * this method and report error if <tt>label</tt> can not be resolved to a number 
128      * literal as the property identifier.
129      */
130     public final Initializer set(int label, Object value) {
131         return set(Integer.toString(label), value);
132     }
133     /**
134      * <p>Appends a property-value pair to the current <tt>Initializer</tt> cache 
135      * instance.</p>
136      * @param label The ID of the property to set the <tt>value</tt> on the current 
137      * <tt>Initializer</tt> cache instance.
138      * @param value The value to be set to the <tt>label</tt> of the current 
139      * <tt>Initializer</tt> cache instance.
140      * @return The current <tt>Initializer</tt> cache instance itself.
141      * @since 1.0
142      * @javascript Re-compilers must append the property-value pair, that is <tt>label:value</tt>, 
143      * to the current re-compile time <tt>Initializer</tt> cache they have kept without 
144      * generating any JavaScript statements or expressions for the virtual invocation of 
145      * this method, where <tt>label</tt> must be resolved to a globally unique numerical 
146      * property identifier.
147      */
148     public final <T> Initializer set(Id<T> label, T value) {
149         return set(label.mid(), value);
150     }
151     /**
152      * <p>Appends a property-value pair to the current <tt>Initializer</tt> cache 
153      * instance.</p>
154      * @param label The member ID of the property to set the <tt>value</tt> to the current 
155      * <tt>Initializer</tt> cache instance.
156      * @param value The value to be set to the <tt>label</tt> of the current 
157      * <tt>Initializer</tt> cache instance.
158      * @return The current <tt>Initializer</tt> cache instance itself.
159      * @since 1.0
160      * @javascript Re-compilers must append the property-value pair, that is <tt>label:value</tt>, 
161      * to the current re-compile time <tt>Initializer</tt> cache they have kept without 
162      * generating any JavaScript statements or expressions for the virtual invocation of 
163      * the method, where <tt>label</tt> must be resolved to the property identifier.
164      */
165     public final <T> Initializer set(Member<T> label, T value) {
166         return set(label.mid(), value);
167     }
168 }