/*
 * Copyright 2012-2015 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 jetbrains.jetpad.model.event;

import jetbrains.jetpad.base.BaseRegistration;
import jetbrains.jetpad.base.Registration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CompositeRegistration extends BaseRegistration {
  private List<Registration> myRegistrations;
  private boolean myRemoved;
  private boolean myRemoving;

  public CompositeRegistration(Registration... regs) {
    myRegistrations = new ArrayList<>(regs.length);
    myRegistrations.addAll(Arrays.asList(regs));
  }

  public CompositeRegistration add(Registration r) {
    myRegistrations.add(r);
    return this;
  }

  public CompositeRegistration add(Registration... rs) {
    for (Registration r : rs) {
      add(r);
    }
    return this;
  }

  public boolean isEmpty() {
    return myRegistrations.isEmpty();
  }

  @Override
  protected void doRemove() {
    if (myRemoved || myRemoving) {
      throw new IllegalStateException();
    }

    myRemoving = true;
    try {
      for (Registration r : myRegistrations) {
        try {
          r.remove();
        } catch (Throwable t) {
          ThrowableHandlers.handle(t);
        }
      }
    } finally {
      myRemoving = false;
    }
    myRemoved = true;
  }
}
@@ -914,37 +914,8 @@
 ns;%0A
-  private boolean myRemoved;%0A
   pr
@@ -1462,21 +1462,8 @@
 if (
-myRemoved %7C%7C 
 myRe
@@ -1519,17 +1519,16 @@
 ;%0A    %7D%0A
-%0A
     myRe
@@ -1776,30 +1776,8 @@
   %7D%0A
-    myRemoved = true;%0A
   %7D%0A

remove duplicating myRemoved flag from CompositeRegistration