2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.json.impl;
45 import java.lang.reflect.Array;
46 import java.util.ArrayList;
47 import java.util.Collection;
48 import java.util.Iterator;
49 import org.apidesign.html.json.spi.Proto;
53 * @author Jaroslav Tulach <jtulach@netbeans.org>
55 public final class JSONList<T> extends ArrayList<T> {
56 private final Proto proto;
57 private final String name;
58 private final String[] deps;
59 private final int index;
61 public JSONList(Proto proto, String name, int changeIndex, String... deps) {
65 this.index = changeIndex;
68 public void init(Object values) {
70 if (values == null || (len = Array.getLength(values)) == 0) {
73 for (int i = 0; i < len; i++) {
74 Object data = Array.get(values, i);
78 public static <T> void init(Collection<T> to, Object values) {
80 if (values == null || (len = Array.getLength(values)) == 0) {
83 for (int i = 0; i < len; i++) {
84 Object data = Array.get(values, i);
90 public boolean add(T e) {
91 boolean ret = super.add(e);
97 public boolean addAll(Collection<? extends T> c) {
98 boolean ret = super.addAll(c);
104 public boolean addAll(int index, Collection<? extends T> c) {
105 boolean ret = super.addAll(index, c);
111 public boolean remove(Object o) {
112 boolean ret = super.remove(o);
118 public void clear() {
124 public boolean removeAll(Collection<?> c) {
125 boolean ret = super.removeAll(c);
131 public boolean retainAll(Collection<?> c) {
132 boolean ret = super.retainAll(c);
138 public T set(int index, T element) {
139 T ret = super.set(index, element);
145 public void add(int index, T element) {
146 super.add(index, element);
151 public T remove(int index) {
152 T ret = super.remove(index);
158 public String toString() {
159 Iterator<T> it = iterator();
164 StringBuilder sb = new StringBuilder();
166 while (it.hasNext()) {
169 sb.append(JSON.toJSON(t));
173 return sb.toString();
176 private void notifyChange() {
177 Bindings m = PropertyBindingAccessor.getBindings(proto, false);
179 m.valueHasMutated(name);
180 for (String dependant : deps) {
181 m.valueHasMutated(dependant);
184 PropertyBindingAccessor.notifyProtoChange(proto, index);
190 public JSONList clone() {
191 throw new UnsupportedOperationException();
194 static final Object koData(Collection<?> c, Bindings m) {
195 Object[] arr = c.toArray(new Object[c.size()]);
196 for (int i = 0; i < arr.length; i++) {
197 Object r = JSON.find(arr[i], m);
202 return m.wrapArray(arr);
205 final Object koData() {
206 return koData(this, PropertyBindingAccessor.getBindings(proto, true));