/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.util.ui.tree;

import org.jetbrains.annotations.NotNull;

import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import java.util.ArrayDeque;

/**
 * @author Sergey.Malenkov
 */
public final class TreeModelListenerList implements TreeModelListener {
  private static final TreeModelListener[] EMPTY_ARRAY = new TreeModelListener[0];
  private final ArrayDeque<TreeModelListener> myDeque = new ArrayDeque<TreeModelListener>();
  private volatile boolean myDequeEmpty = true;

  /**
   * Adds a listener for changes in a tree model.
   * This method is safe for use by multiple concurrent threads.
   *
   * @param listener a listener to add
   */
  public void add(@NotNull TreeModelListener listener) {
    synchronized (myDeque) {
      myDeque.addFirst(listener);
      myDequeEmpty = myDeque.isEmpty();
    }
  }

  /**
   * Removes a previously added listener.
   * This method is safe for use by multiple concurrent threads.
   *
   * @param listener a listener to remove
   */
  public void remove(@NotNull TreeModelListener listener) {
    if (!myDequeEmpty) {
      synchronized (myDeque) {
        myDeque.remove(listener);
        myDequeEmpty = myDeque.isEmpty();
      }
    }
  }

  /**
   * Removes all added listeners.
   * This method is safe for use by multiple concurrent threads.
   */
  public void clear() {
    if (!myDequeEmpty) {
      synchronized (myDeque) {
        myDeque.clear();
        myDequeEmpty = true;
      }
    }
  }

  /**
   * Returns {@code true} if no listeners have been added.
   * This method is safe for use by multiple concurrent threads.
   *
   * @return {@code true} if no listeners have been added, or {@code false} otherwise
   */
  public boolean isEmpty() {
    return myDequeEmpty;
  }

  /**
   * Returns all added listeners or an empty array if no listeners have been added.
   * This method is safe for use by multiple concurrent threads.
   *
   * @return all added listeners
   */
  @NotNull
  public TreeModelListener[] get() {
    if (myDequeEmpty) return EMPTY_ARRAY;
    synchronized (myDeque) {
      return myDeque.toArray(EMPTY_ARRAY);
    }
  }

  /**
   * Notifies all added listeners that a tree hierarchy was changed.
   *
   * @param event the event object specifying a node with changed hierarchy
   */
  @Override
  public void treeStructureChanged(@NotNull TreeModelEvent event) {
    for (TreeModelListener listener : get()) {
      listener.treeStructureChanged(event);
    }
  }

  /**
   * Notifies all added listeners that some nodes were changed.
   *
   * @param event the event object specifying changed nodes
   */
  @Override
  public void treeNodesChanged(@NotNull TreeModelEvent event) {
    for (TreeModelListener listener : get()) {
      listener.treeNodesChanged(event);
    }
  }

  /**
   * Notifies all added listeners that some nodes were inserted.
   *
   * @param event the event object specifying inserted nodes
   */
  @Override
  public void treeNodesInserted(@NotNull TreeModelEvent event) {
    for (TreeModelListener listener : get()) {
      listener.treeNodesInserted(event);
    }
  }

  /**
   * Notifies all added listeners that some nodes were removed.
   *
   * @param event the event object specifying removed nodes
   */
  @Override
  public void treeNodesRemoved(@NotNull TreeModelEvent event) {
    for (TreeModelListener listener : get()) {
      listener.treeNodesRemoved(event);
    }
  }
}

@@ -630,16 +630,67 @@
 .tree;%0A%0A
+import com.intellij.util.containers.ContainerUtil;%0A
 import o
@@ -723,16 +723,16 @@
 otNull;%0A
-
 %0Aimport 
@@ -826,26 +826,20 @@
 va.util.
-ArrayDeque
+List
 ;%0A%0A/**%0A 
@@ -954,220 +954,97 @@
 ate 
-static final TreeModelListener%5B%5D EMPTY_ARRAY = new TreeModelListener%5B0%5D;%0A  private final ArrayDeque%3CTreeModelListener%3E myDeque = new ArrayDeque%3CTreeModelListener%3E();%0A  private volatile boolean myDequeEmpty = true
+final List%3CTreeModelListener%3E myListeners = ContainerUtil.createLockFreeCopyOnWriteList()
 ;%0A%0A 
@@ -1278,112 +1278,34 @@
     
-synchronized (myDeque) %7B%0A      myDeque.addFirst(listener);%0A      myDequeEmpty = myDeque.isEmpty();%0A    %7D
+myListeners.add(listener);
 %0A  %7D
@@ -1540,149 +1540,37 @@
     
-if (!myDequeEmpty) %7B%0A      synchronized (myDeque) %7B%0A        myDeque.remove(listener);%0A        myDequeEmpty = myDeque.isEmpty();%0A      %7D%0A    %7D
+myListeners.remove(listener);
 %0A  %7D
@@ -1714,127 +1714,28 @@
     
-if (!myDequeEmpty) %7B%0A      synchronized (myDeque) %7B%0A        myDeque.clear();%0A        myDequeEmpty = true;%0A      %7D%0A    %7D
+myListeners.clear();
 %0A  %7D
@@ -2009,391 +2009,28 @@
 n my
-DequeEmpty;%0A  %7D%0A%0A  /**%0A   * Returns all added listeners or an empty array if no listeners have been added.%0A   * This method is safe for use by multiple concurrent threads.%0A   *%0A   * @return all added listeners%0A   */%0A  @NotNull%0A  public TreeModelListener%5B%5D get() %7B%0A    if (myDequeEmpty) return EMPTY_ARRAY;%0A    synchronized (myDeque) %7B%0A      return myDeque.toArray(EMPTY_ARRAY);%0A    %7D
+Listeners.isEmpty();
 %0A  %7D
@@ -2303,37 +2303,43 @@
 ener listener : 
-get()
+myListeners
 ) %7B%0A      listen
@@ -2628,37 +2628,43 @@
 ener listener : 
-get()
+myListeners
 ) %7B%0A      listen
@@ -2952,37 +2952,43 @@
 ener listener : 
-get()
+myListeners
 ) %7B%0A      listen
@@ -3286,13 +3286,19 @@
 r : 
-get()
+myListeners
 ) %7B%0A

make code simpler, get rid of races, avoid copying array on event fire