json/src/main/java/org/netbeans/html/json/impl/JSONList.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 30f20d9c0986
child 908 ee7a0b3b2d4c
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    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.
    42  */
    43 package org.netbeans.html.json.impl;
    44 
    45 import java.lang.reflect.Array;
    46 import java.util.ArrayList;
    47 import java.util.Collection;
    48 import java.util.Iterator;
    49 import org.netbeans.html.json.spi.Proto;
    50 
    51 /**
    52  *
    53  * @author Jaroslav Tulach
    54  */
    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;
    60 
    61     public JSONList(Proto proto, String name, int changeIndex, String... deps) {
    62         this.proto = proto;
    63         this.name = name;
    64         this.deps = deps;
    65         this.index = changeIndex;
    66     }
    67     
    68     public void init(Object values) {
    69         int len;
    70         if (values == null || (len = Array.getLength(values)) == 0) {
    71             return;
    72         }
    73         for (int i = 0; i < len; i++) {
    74             Object data = Array.get(values, i);
    75             super.add((T)data);
    76         }
    77     }
    78     public static <T> void init(Collection<T> to, Object values) {
    79         int len;
    80         if (values == null || (len = Array.getLength(values)) == 0) {
    81             return;
    82         }
    83         for (int i = 0; i < len; i++) {
    84             Object data = Array.get(values, i);
    85             to.add((T)data);
    86         }
    87     }
    88     
    89     @Override
    90     public boolean add(T e) {
    91         boolean ret = super.add(e);
    92         notifyChange();
    93         return ret;
    94     }
    95 
    96     @Override
    97     public boolean addAll(Collection<? extends T> c) {
    98         boolean ret = super.addAll(c);
    99         notifyChange();
   100         return ret;
   101     }
   102 
   103     @Override
   104     public boolean addAll(int index, Collection<? extends T> c) {
   105         boolean ret = super.addAll(index, c);
   106         notifyChange();
   107         return ret;
   108     }
   109 
   110     @Override
   111     public boolean remove(Object o) {
   112         boolean ret = super.remove(o);
   113         notifyChange();
   114         return ret;
   115     }
   116 
   117     @Override
   118     public void clear() {
   119         super.clear();
   120         notifyChange();
   121     }
   122 
   123     @Override
   124     public boolean removeAll(Collection<?> c) {
   125         boolean ret = super.removeAll(c);
   126         notifyChange();
   127         return ret;
   128     }
   129 
   130     @Override
   131     public boolean retainAll(Collection<?> c) {
   132         boolean ret = super.retainAll(c);
   133         notifyChange();
   134         return ret;
   135     }
   136 
   137     @Override
   138     public T set(int index, T element) {
   139         T ret = super.set(index, element);
   140         notifyChange();
   141         return ret;
   142     }
   143 
   144     @Override
   145     public void add(int index, T element) {
   146         super.add(index, element);
   147         notifyChange();
   148     }
   149 
   150     @Override
   151     public T remove(int index) {
   152         T ret = super.remove(index);
   153         notifyChange();
   154         return ret;
   155     }
   156 
   157     @Override
   158     public String toString() {
   159         Iterator<T> it = iterator();
   160         if (!it.hasNext()) {
   161             return "[]";
   162         }
   163         String sep = "";
   164         StringBuilder sb = new StringBuilder();
   165         sb.append('[');
   166         while (it.hasNext()) {
   167             T t = it.next();
   168             sb.append(sep);
   169             sb.append(JSON.toJSON(t));
   170             sep = ",";
   171         }
   172         sb.append(']');
   173         return sb.toString();
   174     }
   175 
   176     private void notifyChange() {
   177         proto.getContext().execute(new Runnable() {
   178             @Override
   179             public void run() {
   180                 Bindings m = PropertyBindingAccessor.getBindings(proto, false);
   181                 if (m != null) {
   182                     m.valueHasMutated(name, null, null);
   183                     for (String dependant : deps) {
   184                         m.valueHasMutated(dependant, null, null);
   185                     }
   186                     if (index >= 0) {
   187                         PropertyBindingAccessor.notifyProtoChange(proto, index);
   188                     }
   189                 }
   190             }
   191         });
   192     }
   193 
   194     @Override
   195     public JSONList clone() {
   196         throw new UnsupportedOperationException();
   197     }
   198 
   199     static final Object koData(Collection<?> c, Bindings m) {
   200         Object[] arr = c.toArray(new Object[c.size()]);
   201         for (int i = 0; i < arr.length; i++) {
   202             Object r = JSON.find(arr[i], m);
   203             if (r != null) {
   204                 arr[i] = r;
   205             }
   206         }
   207         return m.wrapArray(arr);
   208     }
   209 
   210     final Object koData() {
   211         return koData(this, PropertyBindingAccessor.getBindings(proto, true));
   212     }
   213 }