json/src/main/java/org/netbeans/html/json/spi/Proto.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 16 Dec 2014 21:03:16 +0100
branchApplyId
changeset 908 ee7a0b3b2d4c
parent 838 bdc3d696dd4a
child 940 bdec4103bdb2
permissions -rw-r--r--
Providing an ability to apply model to element with certain id
jaroslav@376
     1
/**
jaroslav@373
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@373
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@373
     5
 *
jaroslav@373
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@373
     7
 * Other names may be trademarks of their respective owners.
jaroslav@373
     8
 *
jaroslav@373
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@373
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@373
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@373
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@373
    13
 * License. You can obtain a copy of the License at
jaroslav@373
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@373
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@373
    16
 * specific language governing permissions and limitations under the
jaroslav@373
    17
 * License.  When distributing the software, include this License Header
jaroslav@373
    18
 * Notice in each file and include the License file at
jaroslav@373
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@373
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@373
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@373
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@373
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@373
    24
 * your own identifying information:
jaroslav@373
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@373
    26
 *
jaroslav@373
    27
 * Contributor(s):
jaroslav@373
    28
 *
jaroslav@376
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@373
    31
 *
jaroslav@373
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@373
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@373
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@373
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@373
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@373
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@373
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@373
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@373
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@373
    41
 * made subject to such option by the copyright holder.
jaroslav@373
    42
 */
jtulach@838
    43
package org.netbeans.html.json.spi;
jaroslav@373
    44
jaroslav@383
    45
import java.util.Collection;
jaroslav@383
    46
import java.util.List;
jaroslav@373
    47
import net.java.html.BrwsrCtx;
jaroslav@408
    48
import net.java.html.json.ComputedProperty;
jaroslav@402
    49
import net.java.html.json.Model;
jaroslav@374
    50
import org.netbeans.html.json.impl.Bindings;
jaroslav@374
    51
import org.netbeans.html.json.impl.JSON;
jaroslav@383
    52
import org.netbeans.html.json.impl.JSONList;
jaroslav@386
    53
import org.netbeans.html.json.impl.RcvrJSON;
jaroslav@386
    54
import org.netbeans.html.json.impl.RcvrJSON.MsgEvnt;
jaroslav@373
    55
jaroslav@402
    56
/** Object associated with one instance of a model generated by the
jaroslav@402
    57
 * {@link Model} annotation. Contains methods the generated class can
jaroslav@402
    58
 * use to communicate with behind the scene associated {@link Technology}.
jaroslav@402
    59
 * Each {@link Proto} object is associated with <a href="http://wiki.apidesign.org/wiki/Singletonizer">
jaroslav@402
    60
 * singletonizer</a>-like interface {@link Type} which provides the 
jaroslav@402
    61
 * associated {@link Technology} the necessary information about the 
jaroslav@402
    62
 * generated {@link Model} class.
jaroslav@373
    63
 *
jtulach@655
    64
 * @author Jaroslav Tulach
jaroslav@373
    65
 * @since 0.7
jaroslav@373
    66
 */
jaroslav@373
    67
public final class Proto {
jaroslav@374
    68
    private final Object obj;
jaroslav@373
    69
    private final Type type;
jaroslav@373
    70
    private final net.java.html.BrwsrCtx context;
jaroslav@374
    71
    private org.netbeans.html.json.impl.Bindings ko;
jtulach@782
    72
    private Observers observers;
jaroslav@373
    73
jaroslav@374
    74
    Proto(Object obj, Type type, BrwsrCtx context) {
jaroslav@374
    75
        this.obj = obj;
jaroslav@373
    76
        this.type = type;
jaroslav@373
    77
        this.context = context;
jaroslav@373
    78
    }
jaroslav@373
    79
jaroslav@402
    80
    /** Browser context this proto object and its associated model
jaroslav@402
    81
     * are operating-in.
jaroslav@402
    82
     * 
jaroslav@402
    83
     * @return the associated context 
jaroslav@402
    84
     */
jaroslav@373
    85
    public BrwsrCtx getContext() {
jaroslav@373
    86
        return context;
jaroslav@373
    87
    }
jaroslav@402
    88
jtulach@786
    89
    /** Acquires global lock to compute a {@link ComputedProperty derived property}
jtulach@786
    90
     * on this proto object. This proto object must not be locked yet. No
jtulach@786
    91
     * dependency tracking is performed.
jtulach@786
    92
     * 
jaroslav@402
    93
     * @throws IllegalStateException if already locked
jaroslav@402
    94
     */
jaroslav@373
    95
    public void acquireLock() throws IllegalStateException {
jtulach@778
    96
        acquireLock(null);
jtulach@774
    97
    }
jtulach@786
    98
    
jtulach@786
    99
    /** Acquires global lock to compute a {@link ComputedProperty derived property}
jtulach@786
   100
     * on this proto object. This proto object must not be locked yet. The
jtulach@786
   101
     * name of the property is used to track dependencies on own
jtulach@786
   102
     * properties of other proto objects - when they are changed, this
jtulach@786
   103
     * {@link #valueHasMutated(java.lang.String) property is changed too}.
jtulach@786
   104
     * 
jtulach@786
   105
     * @param propName name of property we are about to compute
jtulach@786
   106
     * @throws IllegalStateException thrown when there is a cyclic
jtulach@786
   107
     *   call is detected
jtulach@786
   108
     * @since 0.9
jtulach@786
   109
     */
jtulach@774
   110
    public void acquireLock(String propName) throws IllegalStateException {
jtulach@783
   111
        Observers.beginComputing(this, propName);
jtulach@774
   112
    }
jtulach@774
   113
    
jtulach@786
   114
    /** A property on this proto object is about to be accessed. Verifies
jtulach@786
   115
     * whether this proto object is accessible - e.g. it has not been
jtulach@786
   116
     * {@link #acquireLock() locked yet}. If everything is OK, the
jtulach@786
   117
     * <code>propName</code> is recorded in the chain of dependencies
jtulach@786
   118
     * tracked by {@link #acquireLock(java.lang.String)} and watched by
jtulach@786
   119
     * {@link #valueHasMutated(java.lang.String)}.
jtulach@786
   120
     * 
jtulach@786
   121
     * @param propName name of the property that is requested
jtulach@786
   122
     * @throws IllegalStateException if the model is locked
jtulach@786
   123
     * @since 0.9
jtulach@786
   124
     */
jtulach@786
   125
    public void accessProperty(String propName) throws IllegalStateException {
jtulach@785
   126
        Observers.accessingValue(this, propName);
jaroslav@373
   127
    }
jaroslav@373
   128
    
jaroslav@402
   129
    /** Verifies the model is not locked otherwise throws an exception.
jaroslav@402
   130
     * @throws IllegalStateException if the model is locked
jaroslav@402
   131
     */
jaroslav@402
   132
    public void verifyUnlocked() throws IllegalStateException {
jtulach@783
   133
        Observers.verifyUnlocked(this);
jaroslav@373
   134
    }
jaroslav@373
   135
    
jaroslav@402
   136
    /** When modifications are over, the model is switched into 
jaroslav@402
   137
     * unlocked state by calling this method.
jaroslav@402
   138
     */
jaroslav@373
   139
    public void releaseLock() {
jtulach@785
   140
        Observers.finishComputing(this);
jaroslav@373
   141
    }
jaroslav@373
   142
    
jaroslav@403
   143
    /** Whenever model changes a property. It should notify the
jaroslav@718
   144
     * associated technology by calling this method. 
jaroslav@718
   145
     * Since 0.8.3: This method may be called by any thread - it reschedules
jaroslav@718
   146
     * its actual execution into appropriate one by using
jaroslav@718
   147
     * {@link BrwsrCtx#execute(java.lang.Runnable)}.
jaroslav@403
   148
     * 
jaroslav@567
   149
     * @param propName name of the changed property
jaroslav@403
   150
     */
jaroslav@717
   151
    public void valueHasMutated(final String propName) {
jaroslav@717
   152
        context.execute(new Runnable() {
jaroslav@717
   153
            @Override
jaroslav@717
   154
            public void run() {
jaroslav@717
   155
                if (ko != null) {
jaroslav@717
   156
                    ko.valueHasMutated(propName, null, null);
jaroslav@717
   157
                }
jtulach@785
   158
                Observers.valueHasMutated(Proto.this, propName);
jaroslav@717
   159
            }
jaroslav@717
   160
        });
jaroslav@567
   161
    }
jaroslav@567
   162
jaroslav@567
   163
    /** Whenever model changes a propertyit should notify the
jaroslav@567
   164
     * associated technology. Either by calling this method
jaroslav@567
   165
     * (if the new value is known and different to the old one) or
jaroslav@567
   166
     * via (slightly ineffective) {@link #valueHasMutated(java.lang.String)}
jaroslav@567
   167
     * method.
jaroslav@718
   168
     * Since 0.8.3: This method may be called by any thread - it reschedules
jaroslav@718
   169
     * its actual execution into appropriate one by using
jaroslav@718
   170
     * {@link BrwsrCtx#execute(java.lang.Runnable)}.
jaroslav@567
   171
     * 
jaroslav@567
   172
     * @param propName name of the changed property
jaroslav@567
   173
     * @param oldValue provides previous value of the property
jaroslav@567
   174
     * @param newValue provides new value of the property
jaroslav@567
   175
     * @since 0.7.6
jaroslav@567
   176
     */
jaroslav@717
   177
    public void valueHasMutated(
jaroslav@717
   178
        final String propName, final Object oldValue, final Object newValue
jaroslav@717
   179
    ) {
jaroslav@717
   180
        context.execute(new Runnable() {
jaroslav@717
   181
            @Override
jaroslav@717
   182
            public void run() {
jaroslav@717
   183
                if (ko != null) {
jaroslav@717
   184
                    ko.valueHasMutated(propName, oldValue, newValue);
jaroslav@717
   185
                }
jtulach@785
   186
                Observers.valueHasMutated(Proto.this, propName);
jaroslav@717
   187
            }
jaroslav@717
   188
        });
jaroslav@373
   189
    }
jaroslav@373
   190
    
jaroslav@403
   191
    /** Initializes the associated model in the current {@link #getContext() context}.
jaroslav@403
   192
     * In case of <em>knockout.js</em> technology, applies given bindings 
jaroslav@403
   193
     * of the current model to the <em>body</em> element of the page.
jaroslav@403
   194
     */
jaroslav@373
   195
    public void applyBindings() {
jtulach@908
   196
        initBindings().applyBindings(null);
jtulach@908
   197
    }
jtulach@908
   198
    
jtulach@908
   199
    /** Initializes the associated model to the specified element's subtree.
jtulach@908
   200
     * The technology is taken from the current {@link #getContext() context} and
jtulach@908
   201
     * in case of <em>knockout.js</em> applies given bindings 
jtulach@908
   202
     * of the current model to the element of the page with 'id' attribute
jtulach@908
   203
     * set to the specified <code>id</code> value.
jtulach@908
   204
     * 
jtulach@908
   205
     * @param id the id of element to apply the binding to
jtulach@908
   206
     * @since 1.1
jtulach@908
   207
     * @see Technology.ApplyId
jtulach@908
   208
     */
jtulach@908
   209
    public void applyBindings(String id) {
jtulach@908
   210
        initBindings().applyBindings(id);
jaroslav@374
   211
    }
jaroslav@374
   212
    
jaroslav@403
   213
    /** Invokes the provided runnable in the {@link #getContext() context}
jaroslav@403
   214
     * of the browser. If the caller is already on the right thread, the
jaroslav@403
   215
     * <code>run.run()</code> is invoked immediately and synchronously. 
jaroslav@403
   216
     * Otherwise the method returns immediately and the <code>run()</code>
jaroslav@403
   217
     * method is performed later
jaroslav@403
   218
     * 
jaroslav@403
   219
     * @param run the action to execute
jaroslav@403
   220
     */
jaroslav@381
   221
    public void runInBrowser(Runnable run) {
jaroslav@574
   222
        context.execute(run);
jaroslav@381
   223
    }
jaroslav@383
   224
jaroslav@598
   225
    /** Invokes the specified function index in the {@link #getContext() context}
jaroslav@598
   226
     * of the browser. If the caller is already on the right thread, the
jaroslav@598
   227
     * index-th function is invoked immediately and synchronously. 
jaroslav@598
   228
     * Otherwise the method returns immediately and the function is invoked
jaroslav@598
   229
     * later.
jaroslav@598
   230
     * 
jaroslav@598
   231
     * @param index the index of the function as will be passed to
jaroslav@598
   232
     *   {@link Type#call(java.lang.Object, int, java.lang.Object, java.lang.Object)}
jaroslav@598
   233
     *   method
jaroslav@598
   234
     * @param args array of arguments that will be passed as
jaroslav@598
   235
     *   <code>data</code> argument of the <code>call</code> method.
jaroslav@598
   236
     * @since 0.7.6
jaroslav@598
   237
     */
jaroslav@598
   238
    public void runInBrowser(final int index, final Object... args) {
jaroslav@598
   239
        context.execute(new Runnable() {
jaroslav@598
   240
            @Override
jaroslav@598
   241
            public void run() {
jaroslav@599
   242
                try {
jaroslav@599
   243
                    type.call(obj, index, args, null);
jaroslav@599
   244
                } catch (Exception ex) {
jaroslav@599
   245
                    ex.printStackTrace();
jaroslav@599
   246
                }
jaroslav@598
   247
            }
jaroslav@598
   248
        });
jaroslav@598
   249
    }
jaroslav@598
   250
    
jaroslav@403
   251
    /** Initializes the provided collection with a content of the <code>array</code>.
jaroslav@403
   252
     * The initialization can only be done soon after the the collection 
jaroslav@403
   253
     * is created, otherwise an exception is throw
jaroslav@403
   254
     * 
jaroslav@403
   255
     * @param to the collection to initialize (assumed to be empty)
jaroslav@403
   256
     * @param array the array to add to the collection
jaroslav@403
   257
     * @throws IllegalStateException if the system has already been initialized
jaroslav@403
   258
     */
jaroslav@383
   259
    public void initTo(Collection<?> to, Object array) {
jaroslav@383
   260
        if (ko != null) {
jaroslav@383
   261
            throw new IllegalStateException();
jaroslav@383
   262
        }
jaroslav@383
   263
        if (to instanceof JSONList) {
jaroslav@383
   264
           ((JSONList)to).init(array);
jaroslav@383
   265
        } else {
jaroslav@383
   266
            JSONList.init(to, array);
jaroslav@383
   267
        }
jaroslav@383
   268
    }
jaroslav@403
   269
jaroslav@403
   270
    /** Takes an object representing JSON result and extract some of its
jaroslav@403
   271
     * properties. It is assumed that the <code>props</code> and
jaroslav@403
   272
     * <code>values</code> arrays have the same length.
jaroslav@403
   273
     * 
jaroslav@403
   274
     * @param json the JSON object (actual type depends on the associated
jaroslav@403
   275
     *   {@link Technology})
jaroslav@403
   276
     * @param props list of properties to extract
jaroslav@403
   277
     * @param values array that will be filled with extracted values
jaroslav@403
   278
     */
jaroslav@385
   279
    public void extract(Object json, String[] props, Object[] values) {
jaroslav@385
   280
        JSON.extract(context, json, props, values);
jaroslav@385
   281
    }
jaroslav@403
   282
jaroslav@403
   283
    /** Converts raw JSON <code>data</code> into a Java {@link Model} class.
jaroslav@403
   284
     * 
jaroslav@403
   285
     * @param <T> type of the model class
jaroslav@403
   286
     * @param modelClass the type of the class to create
jaroslav@403
   287
     * @param data the raw JSON data
jaroslav@403
   288
     * @return newly created instance of the model class
jaroslav@403
   289
     */
jaroslav@385
   290
    public <T> T read(Class<T> modelClass, Object data) {
jaroslav@385
   291
        return JSON.read(context, modelClass, data);
jaroslav@385
   292
    }
jaroslav@380
   293
jtulach@650
   294
    /** Initializes asynchronous JSON connection to specified URL. Delegates
jtulach@650
   295
     * to {@link #loadJSON(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object...) }
jtulach@650
   296
     * with no extra parameters.
jtulach@835
   297
     * 
jtulach@835
   298
     * @param index the callback index to be used when a reply is received
jtulach@835
   299
     *   to call {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object)}.
jtulach@835
   300
     * 
jtulach@835
   301
     * @param urlBefore the part of the URL before JSON-P callback parameter
jtulach@835
   302
     * @param urlAfter the rest of the URL or <code>null</code> if no JSON-P is used
jtulach@835
   303
     * @param method method to use for connection to the server
jtulach@835
   304
     * @param data string, number or a {@link Model} generated class to send to
jtulach@835
   305
     *    the server when doing a query
jtulach@650
   306
     */
jtulach@650
   307
    public void loadJSON(final int index, 
jtulach@650
   308
        String urlBefore, String urlAfter, String method,
jtulach@650
   309
        final Object data
jtulach@650
   310
    ) {
jtulach@650
   311
        loadJSON(index, urlBefore, urlAfter, method, data, new Object[0]);
jtulach@650
   312
    }
jtulach@650
   313
    
jaroslav@407
   314
    /** Initializes asynchronous JSON connection to specified URL. The 
jaroslav@407
   315
     * method returns immediately and later does callback later.
jaroslav@407
   316
     * 
jaroslav@407
   317
     * @param index the callback index to be used when a reply is received
jaroslav@407
   318
     *   to call {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object)}.
jaroslav@407
   319
     * 
jaroslav@407
   320
     * @param urlBefore the part of the URL before JSON-P callback parameter
jaroslav@407
   321
     * @param urlAfter the rest of the URL or <code>null</code> if no JSON-P is used
jaroslav@407
   322
     * @param method method to use for connection to the server
jaroslav@407
   323
     * @param data string, number or a {@link Model} generated class to send to
jaroslav@407
   324
     *    the server when doing a query
jtulach@650
   325
     * @param params extra params to pass back when calling
jtulach@650
   326
     *   {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object, java.lang.Object[])}
jtulach@650
   327
     * @since 0.8.1
jaroslav@407
   328
     */
jaroslav@386
   329
    public void loadJSON(final int index, 
jaroslav@386
   330
        String urlBefore, String urlAfter, String method,
jtulach@650
   331
        final Object data, final Object... params
jaroslav@386
   332
    ) {
jaroslav@386
   333
        class Rcvr extends RcvrJSON {
jaroslav@386
   334
            @Override
jaroslav@386
   335
            protected void onMessage(MsgEvnt msg) {
jtulach@650
   336
                type.onMessage(obj, index, 1, msg.getValues(), params);
jaroslav@386
   337
            }
jaroslav@386
   338
jaroslav@386
   339
            @Override
jaroslav@386
   340
            protected void onError(MsgEvnt msg) {
jtulach@650
   341
                type.onMessage(obj, index, 2, msg.getException(), params);
jaroslav@386
   342
            }
jaroslav@386
   343
        }
jaroslav@386
   344
        JSON.loadJSON(context, new Rcvr(), urlBefore, urlAfter, method, data);
jaroslav@386
   345
    }
jaroslav@386
   346
    
jaroslav@407
   347
    /** Opens new WebSocket connection to the specified URL. 
jaroslav@407
   348
     * 
jaroslav@407
   349
     * @param index the index to use later during callbacks to 
jaroslav@407
   350
     *   {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object)}
jaroslav@407
   351
     * @param url the <code>ws://</code> or <code>wss://</code> URL to connect to
jaroslav@407
   352
     * @param data data to send to server (usually <code>null</code>)
jaroslav@407
   353
     * @return returns a non-null object representing the socket
jaroslav@407
   354
     *   which can be used when calling {@link #wsSend(java.lang.Object, java.lang.String, java.lang.Object) }
jaroslav@407
   355
     */
jaroslav@387
   356
    public Object wsOpen(final int index, String url, Object data) {
jaroslav@387
   357
        class WSrcvr extends RcvrJSON {
jaroslav@387
   358
            @Override
jaroslav@387
   359
            protected void onError(MsgEvnt msg) {
jaroslav@387
   360
                type.onMessage(obj, index, 2, msg.getException());
jaroslav@387
   361
            }
jaroslav@387
   362
            
jaroslav@387
   363
            @Override
jaroslav@387
   364
            protected void onMessage(MsgEvnt msg) {
jaroslav@387
   365
                type.onMessage(obj, index, 1, msg.getValues());
jaroslav@387
   366
            }
jaroslav@387
   367
            
jaroslav@387
   368
            @Override
jaroslav@387
   369
            protected void onClose(MsgEvnt msg) {
jaroslav@387
   370
                type.onMessage(obj, index, 3, null);
jaroslav@387
   371
            }
jaroslav@387
   372
jaroslav@387
   373
            @Override
jaroslav@387
   374
            protected void onOpen(MsgEvnt msg) {
jaroslav@387
   375
                type.onMessage(obj, index, 0, null);
jaroslav@387
   376
            }
jaroslav@387
   377
        }
jaroslav@387
   378
        return JSON.openWS(context, new WSrcvr(), url, data);
jaroslav@387
   379
    }
jaroslav@387
   380
    
jaroslav@407
   381
    /** Sends a message to existing socket.
jaroslav@407
   382
     * 
jaroslav@407
   383
     * @param webSocket the socket to send message to
jaroslav@407
   384
     * @param url the <code>ws://</code> or <code>wss://</code> URL to connect to,
jaroslav@407
   385
     *    preferably the same as the one used when the socket was 
jaroslav@407
   386
     *    {@link #wsOpen(int, java.lang.String, java.lang.Object) opened}
jaroslav@407
   387
     * @param data the data to send or <code>null</code> if the socket is
jaroslav@407
   388
     *    supposed to be closed
jaroslav@407
   389
     */
jaroslav@387
   390
    public void wsSend(Object webSocket, String url, Object data) {
jaroslav@387
   391
        ((JSON.WS)webSocket).send(context, url, data);
jaroslav@387
   392
    }
jaroslav@403
   393
jaroslav@403
   394
    /** Converts raw data (one of its properties) to string representation.
jaroslav@403
   395
     * 
jaroslav@403
   396
     * @param data the object
jaroslav@403
   397
     * @param propName the name of object property or <code>null</code>
jaroslav@403
   398
     *   if the whole object should be converted
jaroslav@403
   399
     * @return the string representation of the object or its property
jaroslav@403
   400
     */
jaroslav@380
   401
    public String toString(Object data, String propName) {
jaroslav@380
   402
        return JSON.toString(context, data, propName);
jaroslav@380
   403
    }
jaroslav@403
   404
    
jaroslav@403
   405
    /** Converts raw data (one of its properties) to a number representation.
jaroslav@403
   406
     * 
jaroslav@403
   407
     * @param data the object
jaroslav@403
   408
     * @param propName the name of object property or <code>null</code>
jaroslav@403
   409
     *   if the whole object should be converted
jaroslav@403
   410
     * @return the number representation of the object or its property
jaroslav@403
   411
     */
jaroslav@380
   412
    public Number toNumber(Object data, String propName) {
jaroslav@380
   413
        return JSON.toNumber(context, data, propName);
jaroslav@380
   414
    }
jaroslav@407
   415
jaroslav@408
   416
    /** Converts raw JSON data into a {@link Model} class representation.
jaroslav@408
   417
     * 
jaroslav@408
   418
     * @param <T> type of the model to create
jaroslav@408
   419
     * @param type class of the model to create
jaroslav@408
   420
     * @param data raw JSON data (depends on associated {@link Technology})
jaroslav@408
   421
     * @return new instances of the model class filled with values from the
jaroslav@408
   422
     *   <code>data</code> object
jaroslav@408
   423
     */
jaroslav@408
   424
    public <T> T toModel(Class<T> type, Object data) {
jaroslav@408
   425
        return JSON.toModel(context, type, data, null);
jaroslav@380
   426
    }
jaroslav@383
   427
jaroslav@408
   428
    /** Creates new JSON like observable list.
jaroslav@408
   429
     * 
jaroslav@408
   430
     * @param <T> the type of the list elements
jaroslav@408
   431
     * @param propName name of a property this list is associated with
jaroslav@408
   432
     * @param onChange index of the property to use when the list is modified
jaroslav@408
   433
     *   during callback to {@link Type#onChange(java.lang.Object, int)}
jaroslav@408
   434
     * @param dependingProps the array of {@link ComputedProperty derived properties}
jaroslav@408
   435
     *   that depend on the value of the list
jaroslav@408
   436
     * @return new, empty list associated with this proto-object and its model
jaroslav@408
   437
     */
jaroslav@383
   438
    public <T> List<T> createList(String propName, int onChange, String... dependingProps) {
jaroslav@383
   439
        return new JSONList<T>(this, propName, onChange, dependingProps);
jaroslav@383
   440
    }
jaroslav@408
   441
jaroslav@408
   442
    /** Copies content of one collection to another, re-assigning all its
jaroslav@408
   443
     * elements from their current context to the new <code>ctx</code>.
jaroslav@408
   444
     * 
jaroslav@408
   445
     * @param <T> type of the collections
jaroslav@408
   446
     * @param to the target collection to be filled with cloned values
jaroslav@408
   447
     * @param ctx context for the new collection
jaroslav@408
   448
     * @param from original collection with its data
jaroslav@408
   449
     */
jaroslav@383
   450
    public <T> void cloneList(Collection<T> to, BrwsrCtx ctx, Collection<T> from) {
jaroslav@383
   451
        Boolean isModel = null;
jaroslav@383
   452
        for (T t : from) {
jaroslav@383
   453
            if (isModel == null) {
jaroslav@383
   454
                isModel = JSON.isModel(t.getClass());
jaroslav@383
   455
            }
jaroslav@383
   456
            if (isModel) {
jaroslav@383
   457
                to.add(JSON.bindTo(t, ctx));
jaroslav@383
   458
            } else {
jaroslav@383
   459
                to.add(t);
jaroslav@383
   460
            }
jaroslav@383
   461
        }
jaroslav@383
   462
    }
jaroslav@406
   463
    
jaroslav@406
   464
    //
jaroslav@406
   465
    // internal state
jaroslav@406
   466
    //
jaroslav@406
   467
    
jtulach@779
   468
    final String toStr() {
jtulach@779
   469
        return "Proto[" + obj + "]@" + Integer.toHexString(System.identityHashCode(this));
jtulach@779
   470
    }
jaroslav@406
   471
    
jaroslav@406
   472
    final Bindings initBindings() {
jaroslav@406
   473
        if (ko == null) {
jaroslav@406
   474
            Bindings b = Bindings.apply(context, obj);
jaroslav@406
   475
            PropertyBinding[] pb = new PropertyBinding[type.propertyNames.length];
jaroslav@406
   476
            for (int i = 0; i < pb.length; i++) {
jaroslav@406
   477
                pb[i] = b.registerProperty(
jaroslav@406
   478
                    type.propertyNames[i], i, obj, type, type.propertyReadOnly[i]
jaroslav@406
   479
                );
jaroslav@406
   480
            }
jaroslav@406
   481
            FunctionBinding[] fb = new FunctionBinding[type.functions.length];
jaroslav@406
   482
            for (int i = 0; i < fb.length; i++) {
jaroslav@406
   483
                fb[i] = FunctionBinding.registerFunction(
jaroslav@406
   484
                    type.functions[i], i, obj, type
jaroslav@406
   485
                );
jaroslav@406
   486
            }
jaroslav@406
   487
            ko = b;
jaroslav@406
   488
            b.finish(obj, pb, fb);
jaroslav@406
   489
        }
jaroslav@406
   490
        return ko;
jaroslav@406
   491
    }
jaroslav@406
   492
jaroslav@406
   493
    final Bindings getBindings() {
jaroslav@406
   494
        return ko;
jaroslav@406
   495
    }
jaroslav@406
   496
jaroslav@406
   497
    final void onChange(int index) {
jaroslav@406
   498
        type.onChange(obj, index);
jaroslav@406
   499
    }
jaroslav@386
   500
jtulach@785
   501
    final Observers observers(boolean create) {
jtulach@785
   502
        if (create && observers == null) {
jtulach@785
   503
            observers = new Observers();
jtulach@785
   504
        }
jtulach@783
   505
        return observers;
jtulach@774
   506
    }
jtulach@774
   507
jaroslav@373
   508
    /** Functionality used by the code generated by annotation
jaroslav@373
   509
     * processor for the {@link net.java.html.json.Model} annotation.
jaroslav@373
   510
     * 
jaroslav@373
   511
     * @param <Model> the generated class
jaroslav@373
   512
     * @since 0.7
jaroslav@373
   513
     */
jaroslav@373
   514
    public static abstract class Type<Model> {
jaroslav@373
   515
        private final Class<Model> clazz;
jaroslav@373
   516
        private final String[] propertyNames;
jaroslav@373
   517
        private final boolean[] propertyReadOnly;
jaroslav@373
   518
        private final String[] functions;
jaroslav@373
   519
jaroslav@409
   520
        /** Constructor for subclasses generated by the annotation processor
jaroslav@409
   521
         * associated with {@link net.java.html.json.Model} annotation.
jaroslav@409
   522
         * 
jaroslav@409
   523
         * @param clazz the generated model class
jaroslav@409
   524
         * @param modelFor the original class annotated by the {@link net.java.html.json.Model} annotation.
jaroslav@409
   525
         * @param properties number of properties the class has
jaroslav@409
   526
         * @param functions  number of functions the class has
jaroslav@409
   527
         */
jaroslav@373
   528
        protected Type(
jaroslav@373
   529
            Class<Model> clazz, Class<?> modelFor, int properties, int functions
jaroslav@373
   530
        ) {
jaroslav@374
   531
            assert getClass().getName().endsWith("$Html4JavaType");
jaroslav@420
   532
            try {
jaroslav@420
   533
                assert getClass().getDeclaringClass() == clazz;
jaroslav@420
   534
            } catch (SecurityException ex) {
jaroslav@420
   535
                // OK, no check
jaroslav@420
   536
            }
jaroslav@373
   537
            this.clazz = clazz;
jaroslav@373
   538
            this.propertyNames = new String[properties];
jaroslav@373
   539
            this.propertyReadOnly = new boolean[properties];
jaroslav@373
   540
            this.functions = new String[functions];
jaroslav@374
   541
            JSON.register(clazz, this);
jaroslav@373
   542
        }
jaroslav@409
   543
jaroslav@409
   544
        /** Registers property for the type. It is expected each index
jaroslav@409
   545
         * is initialized only once.
jaroslav@409
   546
         * 
jaroslav@409
   547
         * @param name name of the property
jaroslav@409
   548
         * @param index index of the property
jaroslav@409
   549
         * @param readOnly is the property read only?
jaroslav@409
   550
         */
jaroslav@373
   551
        protected final void registerProperty(String name, int index, boolean readOnly) {
jaroslav@373
   552
            assert propertyNames[index] == null;
jaroslav@373
   553
            propertyNames[index] = name;
jaroslav@373
   554
            propertyReadOnly[index] = readOnly;
jaroslav@373
   555
        }
jaroslav@409
   556
jaroslav@409
   557
        /** Registers function of given name at given index.
jaroslav@409
   558
         * 
jaroslav@409
   559
         * @param name name of the function
jaroslav@409
   560
         * @param index name of the type
jaroslav@409
   561
         */
jaroslav@373
   562
        protected final void registerFunction(String name, int index) {
jaroslav@373
   563
            assert functions[index] == null;
jaroslav@373
   564
            functions[index] = name;
jaroslav@373
   565
        }
jaroslav@373
   566
        
jaroslav@410
   567
        /** Creates new proto-object for given {@link Model} class bound to
jaroslav@410
   568
         * provided context.
jaroslav@410
   569
         * 
jaroslav@410
   570
         * @param obj instance of appropriate {@link Model} class
jaroslav@410
   571
         * @param context the browser context
jaroslav@410
   572
         * @return new proto-object that the generated class can use for
jaroslav@410
   573
         *   communication with the infrastructure
jaroslav@410
   574
         */
jaroslav@410
   575
        public Proto createProto(Object obj, BrwsrCtx context) {
jaroslav@374
   576
            return new Proto(obj, this, context);
jaroslav@373
   577
        }
jaroslav@373
   578
        
jaroslav@412
   579
        //
jaroslav@412
   580
        // Implemented by subclasses
jaroslav@412
   581
        //
jaroslav@412
   582
        
jaroslav@412
   583
        /** Sets value of a {@link #registerProperty(java.lang.String, int, boolean) registered property}
jaroslav@412
   584
         * to new value.
jaroslav@412
   585
         * 
jaroslav@412
   586
         * @param model the instance of {@link Model model class}
jaroslav@412
   587
         * @param index index of the property used during registration
jaroslav@412
   588
         * @param value the value to set the property to
jaroslav@412
   589
         */
jaroslav@412
   590
        protected abstract void setValue(Model model, int index, Object value);
jaroslav@412
   591
        
jaroslav@412
   592
        /** Obtains and returns value of a 
jaroslav@412
   593
         * {@link #registerProperty(java.lang.String, int, boolean) registered property}.
jaroslav@412
   594
         * 
jaroslav@412
   595
         * @param model the instance of {@link Model model class}
jaroslav@412
   596
         * @param index index of the property used during registration
jaroslav@412
   597
         * @return current value of the property
jaroslav@412
   598
         */
jaroslav@412
   599
        protected abstract Object getValue(Model model, int index);
jaroslav@412
   600
        
jaroslav@419
   601
        /** Invokes a {@link #registerFunction(java.lang.String, int)} registered function
jaroslav@412
   602
         * on given object.
jaroslav@412
   603
         * 
jaroslav@412
   604
         * @param model the instance of {@link Model model class}
jaroslav@412
   605
         * @param index index of the property used during registration
jaroslav@412
   606
         * @param data the currently selected object the function is about to operate on
jaroslav@412
   607
         * @param event the event that triggered the event
jaroslav@599
   608
         * @throws Exception the method can throw exception which is then logged
jaroslav@412
   609
         */
jaroslav@599
   610
        protected abstract void call(Model model, int index, Object data, Object event)
jaroslav@599
   611
        throws Exception;
jaroslav@412
   612
        
jaroslav@412
   613
        /** Re-binds the model object to new browser context.
jaroslav@412
   614
         * 
jaroslav@412
   615
         * @param model the instance of {@link Model model class}
jaroslav@412
   616
         * @param ctx browser context to clone the object to
jaroslav@412
   617
         * @return new instance of the model suitable for new context
jaroslav@412
   618
         */
jaroslav@412
   619
        protected abstract Model cloneTo(Model model, BrwsrCtx ctx);
jaroslav@412
   620
        
jaroslav@412
   621
        /** Reads raw JSON data and converts them to our model class.
jaroslav@412
   622
         * 
jaroslav@412
   623
         * @param c the browser context to work in
jaroslav@412
   624
         * @param json raw JSON data to get values from
jaroslav@412
   625
         * @return new instance of model class filled by the data
jaroslav@412
   626
         */
jaroslav@412
   627
        protected abstract Model read(BrwsrCtx c, Object json);
jaroslav@412
   628
        
jaroslav@412
   629
        /** Called when a {@link #registerProperty(java.lang.String, int, boolean) registered property}
jaroslav@412
   630
         * changes its value.
jaroslav@412
   631
         * 
jaroslav@412
   632
         * @param model the object that has the property
jaroslav@412
   633
         * @param index the index of the property during registration
jaroslav@412
   634
         */
jaroslav@412
   635
        protected abstract void onChange(Model model, int index);
jaroslav@412
   636
        
jaroslav@412
   637
        /** Finds out if there is an associated proto-object for given
jaroslav@412
   638
         * object.
jaroslav@412
   639
         * 
jaroslav@412
   640
         * @param object an object, presumably (but not necessarily) instance of Model class
jaroslav@412
   641
         * @return associated proto-object or <code>null</code>
jaroslav@412
   642
         */
jaroslav@412
   643
        protected abstract Proto protoFor(Object object);
jaroslav@386
   644
jaroslav@413
   645
        /** Called to report results of asynchronous over-the-wire 
jaroslav@413
   646
         * communication. Result of calling {@link Proto#wsOpen(int, java.lang.String, java.lang.Object)}
jtulach@650
   647
         * or {@link Proto#loadJSON(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object...)}.
jaroslav@386
   648
         * 
jaroslav@413
   649
         * @param model the instance of the model class
jaroslav@413
   650
         * @param index index used during initiating the communication (via <code>loadJSON</code> or <code>wsOpen</code> calls)
jaroslav@413
   651
         * @param type type of the message: 0 - onOpen, 1 - onMessage, 2 - onError, 3 - onClose -
jaroslav@413
   652
         *   not all messages are applicable to all communication protocols (JSON has only 1 and 2).
jaroslav@413
   653
         * @param data <code>null</code> or string, number or a {@link Model} class
jaroslav@413
   654
         *   obtained to the server as a response
jaroslav@386
   655
         */
jtulach@650
   656
        protected void onMessage(Model model, int index, int type, Object data) {
jtulach@650
   657
            onMessage(model, index, type, data, new Object[0]);
jtulach@650
   658
        }
jtulach@650
   659
        
jtulach@650
   660
        /** Called to report results of asynchronous over-the-wire 
jtulach@650
   661
         * communication. Result of calling {@link Proto#wsOpen(int, java.lang.String, java.lang.Object)}
jtulach@650
   662
         * or {@link Proto#loadJSON(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object...)}.
jtulach@650
   663
         * 
jtulach@650
   664
         * @param model the instance of the model class
jtulach@650
   665
         * @param index index used during initiating the communication (via <code>loadJSON</code> or <code>wsOpen</code> calls)
jtulach@650
   666
         * @param type type of the message: 0 - onOpen, 1 - onMessage, 2 - onError, 3 - onClose -
jtulach@650
   667
         *   not all messages are applicable to all communication protocols (JSON has only 1 and 2).
jtulach@650
   668
         * @param data <code>null</code> or string, number or a {@link Model} class
jtulach@650
   669
         *   obtained to the server as a response
jtulach@650
   670
         * @param params extra parameters as passed for example to
jtulach@650
   671
         *   {@link Proto#loadJSON(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object...)}
jtulach@650
   672
         *   method
jtulach@650
   673
         * @since 0.8.1
jtulach@650
   674
         */
jtulach@650
   675
        protected void onMessage(Model model, int index, int type, Object data, Object[] params) {
jtulach@650
   676
            onMessage(model, index, type, data);
jtulach@650
   677
        }
jaroslav@386
   678
jaroslav@411
   679
        //
jaroslav@411
   680
        // Various support methods the generated classes use
jaroslav@411
   681
        //
jaroslav@411
   682
jaroslav@411
   683
        /** Converts and array of raw JSON objects into an array of typed
jaroslav@532
   684
         * Java {@link Model} classes.
jaroslav@411
   685
         * 
jaroslav@411
   686
         * @param <T> the type of the destination array
jaroslav@411
   687
         * @param context browser context to use
jaroslav@411
   688
         * @param src array of raw JSON objects
jaroslav@411
   689
         * @param destType type of the individual array elements
jaroslav@411
   690
         * @param dest array to be filled with read type instances
jaroslav@411
   691
         */
jaroslav@386
   692
        public <T> void copyJSON(BrwsrCtx context, Object[] src, Class<T> destType, T[] dest) {
jaroslav@386
   693
            for (int i = 0; i < src.length && i < dest.length; i++) {
jaroslav@386
   694
                dest[i] = org.netbeans.html.json.impl.JSON.read(context, destType, src[i]);
jaroslav@386
   695
            }
jaroslav@386
   696
        }
jaroslav@380
   697
        
jaroslav@380
   698
        /** Compares two objects that can be converted to integers.
jaroslav@449
   699
         * @param a first value
jaroslav@449
   700
         * @param b second value
jaroslav@380
   701
         * @return true if they are the same
jaroslav@380
   702
         */
jaroslav@380
   703
        public final boolean isSame(int a, int b) {
jaroslav@380
   704
            return a == b;
jaroslav@380
   705
        }
jaroslav@380
   706
jaroslav@380
   707
        /** Compares two objects that can be converted to (floating point)
jaroslav@380
   708
         * numbers.
jaroslav@449
   709
         * @param a first value
jaroslav@449
   710
         * @param b second value
jaroslav@380
   711
         * @return  true if they are the same
jaroslav@380
   712
         */
jaroslav@380
   713
        public final boolean isSame(double a, double b) {
jaroslav@380
   714
            return a == b;
jaroslav@380
   715
        }
jaroslav@380
   716
jaroslav@380
   717
        /** Compares two objects for being the same - e.g. either <code>==</code>
jaroslav@380
   718
         * or <code>equals</code>.
jaroslav@449
   719
         * @param a first value
jaroslav@449
   720
         * @param b second value
jaroslav@380
   721
         * @return true if they are equals
jaroslav@380
   722
         */ 
jaroslav@380
   723
        public final boolean isSame(Object a, Object b) {
jaroslav@380
   724
            if (a == b) {
jaroslav@380
   725
                return true;
jaroslav@380
   726
            }
jaroslav@380
   727
            if (a == null || b == null) {
jaroslav@380
   728
                return false;
jaroslav@380
   729
            }
jaroslav@380
   730
            return a.equals(b);
jaroslav@380
   731
        }
jaroslav@380
   732
jaroslav@380
   733
        /** Cumulative hash function. Adds hashcode of the object to the
jaroslav@380
   734
         * previous value.
jaroslav@380
   735
         * @param o the object (or <code>null</code>)
jaroslav@380
   736
         * @param h the previous value of the hash
jaroslav@380
   737
         * @return new hash - the old one xor the object's one
jaroslav@380
   738
         */
jaroslav@380
   739
        public final int hashPlus(Object o, int h) {
jaroslav@380
   740
            return o == null ? h : h ^ o.hashCode();
jaroslav@380
   741
        }
jaroslav@380
   742
        
jaroslav@380
   743
        /** Converts an object to its JSON value.
jaroslav@380
   744
         * 
jaroslav@380
   745
         * @param obj the object to convert
jaroslav@380
   746
         * @return JSON representation of the object
jaroslav@380
   747
         */
jaroslav@380
   748
        public final String toJSON(Object obj) {
jaroslav@380
   749
            return JSON.toJSON(obj);
jaroslav@380
   750
        }
jaroslav@380
   751
        
jaroslav@380
   752
        /** Converts the value to string.
jaroslav@380
   753
         * 
jaroslav@380
   754
         * @param val the value
jaroslav@380
   755
         * @return the converted value
jaroslav@380
   756
         */
jaroslav@380
   757
        public final String stringValue(Object val) {
jaroslav@380
   758
            return JSON.stringValue(val);
jaroslav@380
   759
        }
jaroslav@380
   760
jaroslav@380
   761
        /** Converts the value to number.
jaroslav@380
   762
         * 
jaroslav@380
   763
         * @param val the value
jaroslav@380
   764
         * @return the converted value
jaroslav@380
   765
         */
jaroslav@380
   766
        public final Number numberValue(Object val) {
jaroslav@380
   767
            return JSON.numberValue(val);
jaroslav@380
   768
        }
jaroslav@380
   769
jaroslav@380
   770
        /** Converts the value to character.
jaroslav@380
   771
         * 
jaroslav@380
   772
         * @param val the value
jaroslav@380
   773
         * @return the converted value
jaroslav@380
   774
         */
jaroslav@380
   775
        public final Character charValue(Object val) {
jaroslav@380
   776
            return JSON.charValue(val);
jaroslav@380
   777
        }
jaroslav@380
   778
jaroslav@380
   779
        /** Converts the value to boolean.
jaroslav@380
   780
         * 
jaroslav@380
   781
         * @param val the value
jaroslav@380
   782
         * @return the converted value
jaroslav@380
   783
         */
jaroslav@380
   784
        public final Boolean boolValue(Object val) {
jaroslav@380
   785
            return JSON.boolValue(val);
jaroslav@380
   786
        }
jaroslav@380
   787
        
jaroslav@380
   788
        /** Extracts value of specific type from given object.
jaroslav@380
   789
         * 
jaroslav@380
   790
         * @param <T> the type of object one is interested in
jaroslav@380
   791
         * @param type the type
jaroslav@380
   792
         * @param val the object to convert to type
jaroslav@380
   793
         * @return the converted value
jaroslav@380
   794
         */
jaroslav@380
   795
        public final <T> T extractValue(Class<T> type, Object val) {
jaroslav@380
   796
            if (Number.class.isAssignableFrom(type)) {
jaroslav@380
   797
                val = numberValue(val);
jaroslav@380
   798
            }
jaroslav@380
   799
            if (Boolean.class == type) {
jaroslav@380
   800
                val = boolValue(val);
jaroslav@380
   801
            }
jaroslav@380
   802
            if (String.class == type) {
jaroslav@380
   803
                val = stringValue(val);
jaroslav@380
   804
            }
jaroslav@380
   805
            if (Character.class == type) {
jaroslav@380
   806
                val = charValue(val);
jaroslav@380
   807
            }
jaroslav@380
   808
            if (Integer.class == type) {
jaroslav@380
   809
                val = val instanceof Number ? ((Number) val).intValue() : 0;
jaroslav@380
   810
            }
jaroslav@380
   811
            if (Long.class == type) {
jaroslav@380
   812
                val = val instanceof Number ? ((Number) val).longValue() : 0;
jaroslav@380
   813
            }
jaroslav@380
   814
            if (Short.class == type) {
jaroslav@380
   815
                val = val instanceof Number ? ((Number) val).shortValue() : 0;
jaroslav@380
   816
            }
jaroslav@380
   817
            if (Byte.class == type) {
jaroslav@380
   818
                val = val instanceof Number ? ((Number) val).byteValue() : 0;
jaroslav@380
   819
            }
jaroslav@380
   820
            if (Double.class == type) {
jaroslav@380
   821
                val = val instanceof Number ? ((Number) val).doubleValue() : Double.NaN;
jaroslav@380
   822
            }
jaroslav@380
   823
            if (Float.class == type) {
jaroslav@380
   824
                val = val instanceof Number ? ((Number) val).floatValue() : Float.NaN;
jaroslav@380
   825
            }
jtulach@755
   826
            if (type.isEnum() && val instanceof String) {
jtulach@755
   827
                val = Enum.valueOf(type.asSubclass(Enum.class), (String)val);
jtulach@755
   828
            }
jaroslav@380
   829
            return type.cast(val);
jaroslav@380
   830
        }
jaroslav@384
   831
jtulach@832
   832
        /** Special dealing with array &amp; {@link List} values. This method
jtulach@824
   833
         * takes the provided collection, empties it and fills it again
jtulach@824
   834
         * with values extracted from <code>value</code> (which is supposed
jtulach@824
   835
         * to be an array).
jtulach@824
   836
         * 
jtulach@824
   837
         * @param <T> the type of list elements
jtulach@824
   838
         * @param arr collection to fill with elements in value
jtulach@824
   839
         * @param type the type of elements in the collection
jtulach@824
   840
         * @param value array of elements to put into the collecition. If
jtulach@824
   841
         *   value is not an array it is wrapped into array with only element
jtulach@824
   842
         * @since 1.0
jtulach@824
   843
         */
jtulach@824
   844
        public final <T> void replaceValue(Collection<? super T> arr, Class<T> type, Object value) {
jtulach@824
   845
            Object[] newArr;
jtulach@824
   846
            if (value instanceof Object[]) {
jtulach@824
   847
                newArr = (Object[]) value;
jtulach@824
   848
            } else {
jtulach@824
   849
                newArr = new Object[] { value };
jtulach@824
   850
            }
jtulach@824
   851
            arr.clear();
jtulach@824
   852
            for (Object e : newArr) {
jtulach@824
   853
                arr.add(extractValue(type, e));
jtulach@824
   854
            }
jtulach@824
   855
        }
jaroslav@373
   856
    }
jaroslav@373
   857
}