json/src/main/java/org/netbeans/html/json/impl/JSONList.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 json/src/main/java/org/apidesign/html/json/impl/JSONList.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jtulach@2
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@2
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
jtulach@2
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jtulach@2
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jtulach@2
    42
 */
jaroslav@362
    43
package org.netbeans.html.json.impl;
jtulach@2
    44
jaroslav@312
    45
import java.lang.reflect.Array;
jtulach@2
    46
import java.util.ArrayList;
jaroslav@254
    47
import java.util.Arrays;
jaroslav@4
    48
import java.util.Collection;
jaroslav@4
    49
import java.util.Iterator;
jaroslav@312
    50
import java.util.List;
jaroslav@282
    51
import net.java.html.BrwsrCtx;
jtulach@2
    52
jtulach@2
    53
/**
jtulach@2
    54
 *
jtulach@2
    55
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@2
    56
 */
jtulach@2
    57
public final class JSONList<T> extends ArrayList<T> {
jaroslav@4
    58
    private final String name;
jaroslav@4
    59
    private final String[] deps;
jaroslav@278
    60
    private Bindings[] model;
jaroslav@4
    61
    private Runnable onchange;
jtulach@2
    62
jaroslav@278
    63
    public JSONList(Bindings[] model, String name, String... deps) {
jaroslav@278
    64
        assert model.length == 1;
jaroslav@278
    65
        this.model = model;
jaroslav@4
    66
        this.name = name;
jaroslav@4
    67
        this.deps = deps;
jtulach@2
    68
    }
jaroslav@254
    69
    
jaroslav@254
    70
    public void init(T... values) {
jaroslav@254
    71
        if (values == null || values.length == 0) {
jaroslav@254
    72
            return;
jaroslav@254
    73
        }
jaroslav@278
    74
        if (this.model[0] != null || !isEmpty()) {
jaroslav@254
    75
            throw new IllegalStateException();
jaroslav@254
    76
        }
jaroslav@254
    77
        super.addAll(Arrays.asList(values));
jaroslav@254
    78
    }
jaroslav@312
    79
    
jaroslav@312
    80
    public void init(Object values) {
jaroslav@312
    81
        int len;
jaroslav@312
    82
        if (values == null || (len = Array.getLength(values)) == 0) {
jaroslav@312
    83
            return;
jaroslav@312
    84
        }
jaroslav@312
    85
        if (this.model[0] != null || !isEmpty()) {
jaroslav@312
    86
            throw new IllegalStateException();
jaroslav@312
    87
        }
jaroslav@312
    88
        for (int i = 0; i < len; i++) {
jaroslav@312
    89
            Object data = Array.get(values, i);
jaroslav@312
    90
            super.add((T)data);
jaroslav@312
    91
        }
jaroslav@312
    92
    }
jaroslav@312
    93
    
jtulach@2
    94
    public JSONList<T> onChange(Runnable r) {
jaroslav@4
    95
        if (this.onchange != null) {
jaroslav@4
    96
            throw new IllegalStateException();
jaroslav@4
    97
        }
jaroslav@4
    98
        this.onchange = r;
jtulach@2
    99
        return this;
jtulach@2
   100
    }
jtulach@2
   101
jaroslav@4
   102
    @Override
jaroslav@4
   103
    public boolean add(T e) {
jaroslav@4
   104
        boolean ret = super.add(e);
jaroslav@4
   105
        notifyChange();
jaroslav@4
   106
        return ret;
jtulach@2
   107
    }
jaroslav@4
   108
jtulach@2
   109
    @Override
jaroslav@4
   110
    public boolean addAll(Collection<? extends T> c) {
jaroslav@4
   111
        boolean ret = super.addAll(c);
jaroslav@4
   112
        notifyChange();
jaroslav@4
   113
        return ret;
jaroslav@4
   114
    }
jaroslav@4
   115
jaroslav@4
   116
    @Override
jaroslav@4
   117
    public boolean addAll(int index, Collection<? extends T> c) {
jaroslav@4
   118
        boolean ret = super.addAll(index, c);
jaroslav@4
   119
        notifyChange();
jaroslav@4
   120
        return ret;
jaroslav@4
   121
    }
jaroslav@4
   122
jaroslav@4
   123
    @Override
jaroslav@4
   124
    public boolean remove(Object o) {
jaroslav@4
   125
        boolean ret = super.remove(o);
jaroslav@4
   126
        notifyChange();
jaroslav@4
   127
        return ret;
jaroslav@4
   128
    }
jaroslav@4
   129
jaroslav@4
   130
    @Override
jaroslav@4
   131
    public void clear() {
jaroslav@4
   132
        super.clear();
jaroslav@4
   133
        notifyChange();
jaroslav@4
   134
    }
jaroslav@4
   135
jaroslav@4
   136
    @Override
jaroslav@4
   137
    public boolean removeAll(Collection<?> c) {
jaroslav@4
   138
        boolean ret = super.removeAll(c);
jaroslav@4
   139
        notifyChange();
jaroslav@4
   140
        return ret;
jaroslav@4
   141
    }
jaroslav@4
   142
jaroslav@4
   143
    @Override
jaroslav@4
   144
    public boolean retainAll(Collection<?> c) {
jaroslav@4
   145
        boolean ret = super.retainAll(c);
jaroslav@4
   146
        notifyChange();
jaroslav@4
   147
        return ret;
jaroslav@4
   148
    }
jaroslav@4
   149
jaroslav@4
   150
    @Override
jaroslav@4
   151
    public T set(int index, T element) {
jaroslav@4
   152
        T ret = super.set(index, element);
jaroslav@4
   153
        notifyChange();
jaroslav@4
   154
        return ret;
jaroslav@4
   155
    }
jaroslav@4
   156
jaroslav@4
   157
    @Override
jaroslav@4
   158
    public void add(int index, T element) {
jaroslav@4
   159
        super.add(index, element);
jaroslav@4
   160
        notifyChange();
jaroslav@4
   161
    }
jaroslav@4
   162
jaroslav@4
   163
    @Override
jaroslav@4
   164
    public T remove(int index) {
jaroslav@4
   165
        T ret = super.remove(index);
jaroslav@4
   166
        notifyChange();
jaroslav@4
   167
        return ret;
jaroslav@4
   168
    }
jaroslav@4
   169
jaroslav@4
   170
    @Override
jaroslav@4
   171
    public String toString() {
jaroslav@4
   172
        Iterator<T> it = iterator();
jaroslav@4
   173
        if (!it.hasNext()) {
jaroslav@4
   174
            return "[]";
jaroslav@4
   175
        }
jaroslav@4
   176
        String sep = "";
jaroslav@4
   177
        StringBuilder sb = new StringBuilder();
jaroslav@4
   178
        sb.append('[');
jaroslav@4
   179
        while (it.hasNext()) {
jaroslav@4
   180
            T t = it.next();
jaroslav@4
   181
            sb.append(sep);
jaroslav@4
   182
            sb.append(JSON.toJSON(t));
jaroslav@4
   183
            sep = ",";
jaroslav@4
   184
        }
jaroslav@4
   185
        sb.append(']');
jaroslav@4
   186
        return sb.toString();
jaroslav@4
   187
    }
jaroslav@4
   188
jaroslav@4
   189
    private void notifyChange() {
jaroslav@278
   190
        Bindings m = model[0];
jaroslav@4
   191
        if (m != null) {
jaroslav@4
   192
            m.valueHasMutated(name);
jaroslav@4
   193
            for (String dependant : deps) {
jaroslav@4
   194
                m.valueHasMutated(dependant);
jaroslav@4
   195
            }
jaroslav@112
   196
            Runnable r = onchange;
jaroslav@112
   197
            if (r != null) {
jaroslav@112
   198
                r.run();
jaroslav@112
   199
            }
jaroslav@4
   200
        }
jaroslav@4
   201
    }
jaroslav@282
   202
    
jaroslav@282
   203
    public void cloneAll(BrwsrCtx c, Collection<T> other) {
jaroslav@282
   204
        Boolean isModel = null;
jaroslav@282
   205
        for (T t : other) {
jaroslav@282
   206
            if (isModel == null) {
jaroslav@282
   207
                isModel = JSON.isModel(t.getClass());
jaroslav@282
   208
            }
jaroslav@282
   209
            if (isModel) {
jaroslav@282
   210
                add(JSON.bindTo(t, c));
jaroslav@282
   211
            } else {
jaroslav@282
   212
                add(t);
jaroslav@282
   213
            }
jaroslav@282
   214
        }
jaroslav@282
   215
    }
jaroslav@4
   216
jaroslav@4
   217
    @Override
jaroslav@4
   218
    public JSONList clone() {
jaroslav@282
   219
        throw new UnsupportedOperationException();
jtulach@2
   220
    }
jaroslav@16
   221
jaroslav@235
   222
    static final Object koData(Collection<?> c, Bindings m) {
jtulach@328
   223
        Object[] arr = c.toArray(new Object[c.size()]);
jaroslav@16
   224
        for (int i = 0; i < arr.length; i++) {
jaroslav@235
   225
            Object r = WrapperObject.find(arr[i], m);
jaroslav@19
   226
            if (r != null) {
jaroslav@19
   227
                arr[i] = r;
jaroslav@19
   228
            }
jaroslav@16
   229
        }
jaroslav@235
   230
        return m.wrapArray(arr);
jaroslav@16
   231
    }
jaroslav@235
   232
jaroslav@235
   233
    final Object koData() {
jaroslav@278
   234
        return koData(this, model[0]);
jaroslav@235
   235
    }
jtulach@2
   236
}