json/src/main/java/org/netbeans/html/json/impl/JSONList.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 414 bc7cb0bbf0b3
child 567 83879118f17e
permissions -rw-r--r--
Updating copyright headers to mention current year
     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.apidesign.html.json.spi.Proto;
    50 
    51 /**
    52  *
    53  * @author Jaroslav Tulach <jtulach@netbeans.org>
    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         Bindings m = PropertyBindingAccessor.getBindings(proto, false);
   178         if (m != null) {
   179             m.valueHasMutated(name);
   180             for (String dependant : deps) {
   181                 m.valueHasMutated(dependant);
   182             }
   183             if (index >= 0) {
   184                 PropertyBindingAccessor.notifyProtoChange(proto, index);
   185             }
   186         }
   187     }
   188 
   189     @Override
   190     public JSONList clone() {
   191         throw new UnsupportedOperationException();
   192     }
   193 
   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);
   198             if (r != null) {
   199                 arr[i] = r;
   200             }
   201         }
   202         return m.wrapArray(arr);
   203     }
   204 
   205     final Object koData() {
   206         return koData(this, PropertyBindingAccessor.getBindings(proto, true));
   207     }
   208 }