1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/json/src/main/java/org/netbeans/html/json/spi/Observers.java Tue Aug 26 18:13:30 2014 +0200
1.3 @@ -0,0 +1,221 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package org.netbeans.html.json.spi;
1.47 +
1.48 +import java.lang.ref.WeakReference;
1.49 +import java.util.ArrayList;
1.50 +import java.util.Iterator;
1.51 +import java.util.LinkedList;
1.52 +import java.util.List;
1.53 +
1.54 +/**
1.55 + *
1.56 + * @author Jaroslav Tulach
1.57 + */
1.58 +final class Observers {
1.59 + private static final LinkedList<Watcher> GLOBAL = new LinkedList<Watcher>();
1.60 + private final List<Watcher> watchers = new ArrayList<Watcher>();
1.61 + private final List<Ref> observers = new ArrayList<Ref>();
1.62 +
1.63 + Observers() {
1.64 + assert Thread.holdsLock(GLOBAL);
1.65 + }
1.66 +
1.67 + static void beginComputing(Proto p, String name) {
1.68 + synchronized (GLOBAL) {
1.69 + verifyUnlocked(p);
1.70 + final Watcher nw = new Watcher(p, name);
1.71 + GLOBAL.push(nw);
1.72 + }
1.73 + }
1.74 +
1.75 + static void verifyUnlocked(Proto p) {
1.76 + synchronized (GLOBAL) {
1.77 + for (Watcher w : GLOBAL) {
1.78 + if (w.proto == p) {
1.79 + throw new IllegalStateException("Re-entrant attempt to access " + p);
1.80 + }
1.81 + }
1.82 + }
1.83 + }
1.84 +
1.85 + static void accessingValue(Proto p, String propName) {
1.86 + synchronized (GLOBAL) {
1.87 + verifyUnlocked(p);
1.88 + for (Watcher w : GLOBAL) {
1.89 + Observers mine = p.observers(true);
1.90 + mine.add(w, new Ref(w, propName));
1.91 + }
1.92 + }
1.93 + }
1.94 +
1.95 + static void finishComputing(Proto p) {
1.96 + synchronized (GLOBAL) {
1.97 + Watcher w = GLOBAL.pop();
1.98 + if (w.proto != p) {
1.99 + throw new IllegalStateException("Inconsistency: " + w.proto + " != " + p);
1.100 + }
1.101 + if (w.prop != null) {
1.102 + Observers mine = p.observers(true);
1.103 + mine.add(w);
1.104 + }
1.105 + }
1.106 + }
1.107 +
1.108 + private static final class Ref extends WeakReference<Watcher> {
1.109 + private final String prop;
1.110 +
1.111 + public Ref(Watcher ref, String prop) {
1.112 + super(ref);
1.113 + this.prop = prop;
1.114 + }
1.115 +
1.116 + final Watcher watcher() {
1.117 + Watcher w = get();
1.118 + if (w == null) {
1.119 + return null;
1.120 + }
1.121 + final Observers o = w.proto.observers(false);
1.122 + if (o == null) {
1.123 + return null;
1.124 + }
1.125 + if (o.find(w.prop) == w) {
1.126 + return w;
1.127 + }
1.128 + return null;
1.129 + }
1.130 + }
1.131 +
1.132 + private Watcher find(String prop) {
1.133 + if (prop == null) {
1.134 + return null;
1.135 + }
1.136 + for (Watcher w : watchers) {
1.137 + if (prop.equals(w.prop)) {
1.138 + return w;
1.139 + }
1.140 + }
1.141 + return null;
1.142 + }
1.143 +
1.144 + final void add(Watcher w) {
1.145 + for (int i = 0; i < watchers.size(); i++) {
1.146 + Watcher ith = watchers.get(i);
1.147 + if (w.prop == null) {
1.148 + if (ith.prop == null) {
1.149 + watchers.set(i, w);
1.150 + return;
1.151 + }
1.152 + } else if (w.prop.equals(ith.prop)) {
1.153 + watchers.set(i, w);
1.154 + return;
1.155 + }
1.156 + }
1.157 + watchers.add(w);
1.158 + }
1.159 +
1.160 + static final void valueHasMutated(Proto p, String propName) {
1.161 + List<Watcher> mutated = new LinkedList<Watcher>();
1.162 + synchronized (GLOBAL) {
1.163 + Observers mine = p.observers(false);
1.164 + if (mine == null) {
1.165 + return;
1.166 + }
1.167 + Iterator<Ref> it = mine.observers.iterator();
1.168 + while (it.hasNext()) {
1.169 + Ref ref = it.next();
1.170 + if (ref.get() == null) {
1.171 + it.remove();
1.172 + continue;
1.173 + }
1.174 + if (ref.prop.equals(propName)) {
1.175 + Watcher w = ref.watcher();
1.176 + if (w != null) {
1.177 + mutated.add(w);
1.178 + }
1.179 + }
1.180 + }
1.181 + }
1.182 + for (Watcher w : mutated) {
1.183 + w.proto.valueHasMutated(w.prop);
1.184 + }
1.185 + }
1.186 +
1.187 + void add(Watcher w, Ref r) {
1.188 + Thread.holdsLock(GLOBAL);
1.189 + if (w == null) {
1.190 + return;
1.191 + }
1.192 + Iterator<Ref> it = observers.iterator();
1.193 + while (it.hasNext()) {
1.194 + Ref ref = it.next();
1.195 + if (r == ref) {
1.196 + return;
1.197 + }
1.198 + final Watcher rw = ref.get();
1.199 + if (rw == null) {
1.200 + it.remove();
1.201 + continue;
1.202 + }
1.203 + if (rw == w && r.prop.equals(r.prop)) {
1.204 + return;
1.205 + }
1.206 + }
1.207 + observers.add(r);
1.208 + }
1.209 +
1.210 + private static final class Watcher {
1.211 + final Proto proto;
1.212 + final String prop;
1.213 +
1.214 + Watcher(Proto proto, String prop) {
1.215 + this.proto = proto;
1.216 + this.prop = prop;
1.217 + }
1.218 +
1.219 + @Override
1.220 + public String toString() {
1.221 + return "Watcher: " + proto + ", " + prop;
1.222 + }
1.223 + }
1.224 +}