json/src/main/java/org/netbeans/html/json/impl/JSONList.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 29 Feb 2016 05:25:31 +0100
branchxhr4j
changeset 1057 b547f8f663f5
parent 908 ee7a0b3b2d4c
child 1028 453e44c757ff
permissions -rw-r--r--
#257849: xhr4j module uses java.net package to handle @OnReceive requests to workaround CORS limitations
jtulach@2
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@2
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 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@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 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@4
    47
import java.util.Collection;
jaroslav@4
    48
import java.util.Iterator;
jtulach@838
    49
import org.netbeans.html.json.spi.Proto;
jtulach@2
    50
jtulach@2
    51
/**
jtulach@2
    52
 *
jtulach@790
    53
 * @author Jaroslav Tulach
jtulach@2
    54
 */
jtulach@2
    55
public final class JSONList<T> extends ArrayList<T> {
jaroslav@383
    56
    private final Proto proto;
jaroslav@4
    57
    private final String name;
jaroslav@4
    58
    private final String[] deps;
jaroslav@383
    59
    private final int index;
jtulach@2
    60
jaroslav@383
    61
    public JSONList(Proto proto, String name, int changeIndex, String... deps) {
jaroslav@374
    62
        this.proto = proto;
jaroslav@4
    63
        this.name = name;
jaroslav@4
    64
        this.deps = deps;
jaroslav@383
    65
        this.index = changeIndex;
jaroslav@254
    66
    }
jaroslav@312
    67
    
jaroslav@312
    68
    public void init(Object values) {
jaroslav@312
    69
        int len;
jaroslav@312
    70
        if (values == null || (len = Array.getLength(values)) == 0) {
jaroslav@312
    71
            return;
jaroslav@312
    72
        }
jaroslav@312
    73
        for (int i = 0; i < len; i++) {
jaroslav@312
    74
            Object data = Array.get(values, i);
jaroslav@312
    75
            super.add((T)data);
jaroslav@312
    76
        }
jaroslav@312
    77
    }
jaroslav@383
    78
    public static <T> void init(Collection<T> to, Object values) {
jaroslav@383
    79
        int len;
jaroslav@383
    80
        if (values == null || (len = Array.getLength(values)) == 0) {
jaroslav@383
    81
            return;
jaroslav@383
    82
        }
jaroslav@383
    83
        for (int i = 0; i < len; i++) {
jaroslav@383
    84
            Object data = Array.get(values, i);
jaroslav@383
    85
            to.add((T)data);
jaroslav@383
    86
        }
jaroslav@383
    87
    }
jaroslav@312
    88
    
jaroslav@4
    89
    @Override
jaroslav@4
    90
    public boolean add(T e) {
jaroslav@4
    91
        boolean ret = super.add(e);
jaroslav@4
    92
        notifyChange();
jaroslav@4
    93
        return ret;
jtulach@2
    94
    }
jaroslav@4
    95
jtulach@2
    96
    @Override
jaroslav@4
    97
    public boolean addAll(Collection<? extends T> c) {
jaroslav@4
    98
        boolean ret = super.addAll(c);
jaroslav@4
    99
        notifyChange();
jaroslav@4
   100
        return ret;
jaroslav@4
   101
    }
jaroslav@4
   102
jaroslav@4
   103
    @Override
jaroslav@4
   104
    public boolean addAll(int index, Collection<? extends T> c) {
jaroslav@4
   105
        boolean ret = super.addAll(index, c);
jaroslav@4
   106
        notifyChange();
jaroslav@4
   107
        return ret;
jaroslav@4
   108
    }
jaroslav@4
   109
jtulach@970
   110
    public void fastReplace(Collection<? extends T> c) {
jtulach@970
   111
        super.clear();
jtulach@970
   112
        super.addAll(c);
jtulach@970
   113
        notifyChange();
jtulach@970
   114
    }
jtulach@970
   115
jaroslav@4
   116
    @Override
jaroslav@4
   117
    public boolean remove(Object o) {
jaroslav@4
   118
        boolean ret = super.remove(o);
jaroslav@4
   119
        notifyChange();
jaroslav@4
   120
        return ret;
jaroslav@4
   121
    }
jaroslav@4
   122
jaroslav@4
   123
    @Override
jaroslav@4
   124
    public void clear() {
jaroslav@4
   125
        super.clear();
jaroslav@4
   126
        notifyChange();
jaroslav@4
   127
    }
jaroslav@4
   128
jaroslav@4
   129
    @Override
jaroslav@4
   130
    public boolean removeAll(Collection<?> c) {
jaroslav@4
   131
        boolean ret = super.removeAll(c);
jaroslav@4
   132
        notifyChange();
jaroslav@4
   133
        return ret;
jaroslav@4
   134
    }
jaroslav@4
   135
jaroslav@4
   136
    @Override
jaroslav@4
   137
    public boolean retainAll(Collection<?> c) {
jaroslav@4
   138
        boolean ret = super.retainAll(c);
jaroslav@4
   139
        notifyChange();
jaroslav@4
   140
        return ret;
jaroslav@4
   141
    }
jaroslav@4
   142
jaroslav@4
   143
    @Override
jaroslav@4
   144
    public T set(int index, T element) {
jaroslav@4
   145
        T ret = super.set(index, element);
jaroslav@4
   146
        notifyChange();
jaroslav@4
   147
        return ret;
jaroslav@4
   148
    }
jaroslav@4
   149
jaroslav@4
   150
    @Override
jaroslav@4
   151
    public void add(int index, T element) {
jaroslav@4
   152
        super.add(index, element);
jaroslav@4
   153
        notifyChange();
jaroslav@4
   154
    }
jaroslav@4
   155
jaroslav@4
   156
    @Override
jaroslav@4
   157
    public T remove(int index) {
jaroslav@4
   158
        T ret = super.remove(index);
jaroslav@4
   159
        notifyChange();
jaroslav@4
   160
        return ret;
jaroslav@4
   161
    }
jaroslav@4
   162
jaroslav@4
   163
    @Override
jaroslav@4
   164
    public String toString() {
jaroslav@4
   165
        Iterator<T> it = iterator();
jaroslav@4
   166
        if (!it.hasNext()) {
jaroslav@4
   167
            return "[]";
jaroslav@4
   168
        }
jaroslav@4
   169
        String sep = "";
jaroslav@4
   170
        StringBuilder sb = new StringBuilder();
jaroslav@4
   171
        sb.append('[');
jaroslav@4
   172
        while (it.hasNext()) {
jaroslav@4
   173
            T t = it.next();
jaroslav@4
   174
            sb.append(sep);
jaroslav@4
   175
            sb.append(JSON.toJSON(t));
jaroslav@4
   176
            sep = ",";
jaroslav@4
   177
        }
jaroslav@4
   178
        sb.append(']');
jaroslav@4
   179
        return sb.toString();
jaroslav@4
   180
    }
jaroslav@4
   181
jaroslav@4
   182
    private void notifyChange() {
jaroslav@719
   183
        proto.getContext().execute(new Runnable() {
jaroslav@719
   184
            @Override
jaroslav@719
   185
            public void run() {
jaroslav@719
   186
                Bindings m = PropertyBindingAccessor.getBindings(proto, false);
jaroslav@719
   187
                if (m != null) {
jtulach@908
   188
                    m.valueHasMutated(name, null, JSONList.this);
jaroslav@719
   189
                    for (String dependant : deps) {
jaroslav@719
   190
                        m.valueHasMutated(dependant, null, null);
jaroslav@719
   191
                    }
jaroslav@719
   192
                    if (index >= 0) {
jaroslav@719
   193
                        PropertyBindingAccessor.notifyProtoChange(proto, index);
jaroslav@719
   194
                    }
jaroslav@719
   195
                }
jaroslav@4
   196
            }
jaroslav@719
   197
        });
jaroslav@282
   198
    }
jaroslav@4
   199
jaroslav@4
   200
    @Override
jaroslav@4
   201
    public JSONList clone() {
jaroslav@282
   202
        throw new UnsupportedOperationException();
jtulach@2
   203
    }
jaroslav@16
   204
jaroslav@235
   205
    static final Object koData(Collection<?> c, Bindings m) {
jtulach@328
   206
        Object[] arr = c.toArray(new Object[c.size()]);
jaroslav@16
   207
        for (int i = 0; i < arr.length; i++) {
jaroslav@414
   208
            Object r = JSON.find(arr[i], m);
jaroslav@19
   209
            if (r != null) {
jaroslav@19
   210
                arr[i] = r;
jaroslav@19
   211
            }
jaroslav@16
   212
        }
jaroslav@235
   213
        return m.wrapArray(arr);
jaroslav@16
   214
    }
jaroslav@235
   215
jaroslav@235
   216
    final Object koData() {
jaroslav@404
   217
        return koData(this, PropertyBindingAccessor.getBindings(proto, true));
jaroslav@235
   218
    }
jtulach@2
   219
}