/*
 * Copyright 2000-2009 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.openapi.vcs.changes;

import com.intellij.openapi.vcs.changes.local.*;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedList;
import java.util.List;

/** synchronization aspect is external for this class; only logic here
 * have internal command queue; applies commands to another copy of change lists (ChangeListWorker) and sends notifications
 * (after update is done)
 */
public class Modifier implements ChangeListsWriteOperations {
  private ChangeListWorker myWorker;
  private boolean myInsideUpdate;
  private final List<ChangeListCommand> myCommandQueue;
  private final DelayedNotificator myNotificator;

  public Modifier(ChangeListWorker worker, DelayedNotificator notificator) {
    myWorker = worker;
    myNotificator = notificator;
    myCommandQueue = new LinkedList<>();
  }

  @Override
  public LocalChangeList addChangeList(@NotNull String name, @Nullable String comment, @Nullable Object data) {
    AddList command = new AddList(name, comment, data);
    impl(command);
    return command.getNewListCopy();
  }

  @Override
  public String setDefault(String name) {
    SetDefault command = new SetDefault(name);
    impl(command);
    return command.getPrevious();
  }

  @Override
  public boolean removeChangeList(@NotNull String name) {
    RemoveList command = new RemoveList(name);
    impl(command);
    return command.isRemoved();
  }

  @Override
  public MultiMap<LocalChangeList, Change> moveChangesTo(String name, Change[] changes) {
    MoveChanges command = new MoveChanges(name, changes);
    impl(command);
    return command.getMovedFrom();
  }

  private void impl(ChangeListCommand command) {
    command.apply(myWorker);
    if (myInsideUpdate) {
      myCommandQueue.add(command);
      // notify after change lsist are synchronized
    } else {
      // notify immediately
      myNotificator.callNotify(command);
    }
  }

  @Override
  public boolean setReadOnly(String name, boolean value) {
    SetReadOnly command = new SetReadOnly(name, value);
    impl(command);
    return command.isResult();
  }

  @Override
  public boolean editName(@NotNull String fromName, @NotNull String toName) {
    EditName command = new EditName(fromName, toName);
    impl(command);
    return command.isResult();
  }

  @Override
  public String editComment(@NotNull String fromName, String newComment) {
    EditComment command = new EditComment(fromName, newComment);
    impl(command);
    return command.getOldComment();
  }

  public boolean isInsideUpdate() {
    return myInsideUpdate;
  }

  public void enterUpdate() {
    myInsideUpdate = true;
  }

  public void finishUpdate(ChangeListWorker worker) {
    exitUpdate();
    // should be applied for notifications to be delivered (they were delayed)
    apply(worker);
    clearQueue();
  }

  private void exitUpdate() {
    myInsideUpdate = false;
  }

  private void clearQueue() {
    myCommandQueue.clear();
  }

  private void apply(ChangeListWorker worker) {
    for (ChangeListCommand command : myCommandQueue) {
      command.apply(worker);
      myNotificator.callNotify(command);
    }
  }

  public void setWorker(ChangeListWorker worker) {
    myWorker = worker;
  }
}

@@ -3377,316 +3377,32 @@
     
-exitUpdate();%0A    // should be applied for notifications to be delivered (they were delayed)%0A    apply(worker);%0A    clearQueue();%0A  %7D%0A%0A  private void exitUpdate() %7B%0A    myInsideUpdate = false;%0A  %7D%0A%0A  private void clearQueue() %7B%0A    myCommandQueue.clear();%0A  %7D%0A%0A  private void apply(ChangeListWorker worker) %7B
+myInsideUpdate = false;%0A
 %0A   
@@ -3520,32 +3520,61 @@
 (command);%0A    %7D
+%0A%0A    myCommandQueue.clear();
 %0A  %7D%0A%0A  public v

simplify - inline method