/*
 * 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 org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl;

import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.CallEnvironment;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.CallInstruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.NegatingGotoInstruction;

import java.util.Deque;
import java.util.LinkedHashSet;

/**
 * @author ven
 */
public class InstructionImpl implements Instruction {
  private final LinkedHashSet<InstructionImpl> myPredecessors = new LinkedHashSet<InstructionImpl>();
  private final LinkedHashSet<InstructionImpl> mySuccessors = new LinkedHashSet<InstructionImpl>();
  private final LinkedHashSet<NegatingGotoInstruction> myNegations = new LinkedHashSet<NegatingGotoInstruction>();

  PsiElement myPsiElement;
  private int myNumber = -1;

  @Nullable
  public PsiElement getElement() {
    return myPsiElement;
  }

  public InstructionImpl(@Nullable PsiElement element) {
    myPsiElement = element;
  }

  public Iterable<? extends Instruction> successors(CallEnvironment environment) {
    final Deque<CallInstruction> stack = environment.callStack(this);
    for (InstructionImpl instruction : mySuccessors) {
      environment.update(stack, instruction);
    }

    return mySuccessors;
  }

  public Iterable<? extends Instruction> predecessors(CallEnvironment environment) {
    final Deque<CallInstruction> stack = environment.callStack(this);
    for (InstructionImpl instruction : myPredecessors) {
      environment.update(stack, instruction);
    }

    return myPredecessors;
  }

  public Iterable<? extends Instruction> allSuccessors() {
    return mySuccessors;
  }

  public Iterable<? extends Instruction> allPredecessors() {
    return myPredecessors;
  }

  public String toString() {
    final StringBuilder builder = new StringBuilder();
    builder.append(myNumber);
    builder.append("(");
    for (InstructionImpl successor : mySuccessors) {
      builder.append(successor.myNumber);
      builder.append(',');
    }
    if (!mySuccessors.isEmpty()) builder.delete(builder.length() - 1, builder.length());
    builder.append(") ").append(getElementPresentation());
    return builder.toString();
  }

  protected String getElementPresentation() {
    return "element: " + myPsiElement;
  }

  public int num() {
    assert myNumber != -1;
    return myNumber;
  }

  @NotNull
  @Override
  public Iterable<? extends NegatingGotoInstruction> getNegatingGotoInstruction() {
    return myNegations;
  }

  public void addSuccessor(InstructionImpl instruction) {
    mySuccessors.add(instruction);
  }

  public void addPredecessor(InstructionImpl instruction) {
    myPredecessors.add(instruction);
  }

  void addNegationsFrom(Instruction instruction) {
    for (NegatingGotoInstruction negation : instruction.getNegatingGotoInstruction()) {
      myNegations.add(negation);
    }
    if (instruction instanceof NegatingGotoInstruction) {
      myNegations.add((NegatingGotoInstruction)instruction);
    }
  }

  final void setNumber(int num) {
    assert myNumber == -1;
    myNumber = num;
  }
}

@@ -1081,16 +1081,46 @@
 ction;%0A%0A
+import java.util.Collections;%0A
 import j
@@ -1336,32 +1336,33 @@
 nstructionImpl%3E(
+1
 );%0A  private fin
@@ -1445,16 +1445,17 @@
 onImpl%3E(
+1
 );%0A  pri
@@ -1451,38 +1451,32 @@
 %3E(1);%0A  private 
-final 
 LinkedHashSet%3CNe
@@ -1513,55 +1513,8 @@
 ions
- = new LinkedHashSet%3CNegatingGotoInstruction%3E()
 ;%0A%0A 
@@ -3220,32 +3220,107 @@
 Instruction() %7B%0A
+    if (myNegations == null) %7B%0A      return Collections.emptyList();%0A    %7D%0A
     return myNeg
@@ -3579,24 +3579,128 @@
 truction) %7B%0A
+    if (myNegations == null) %7B%0A      myNegations = new LinkedHashSet%3CNegatingGotoInstruction%3E(1);%0A    %7D%0A
     for (Neg

save some memory