update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / controlFlow / ControlFlowImpl.java
blob6b4a22d3608bfe9e7231677983f17e39100a87a6
2 /*
3 * Copyright 2000-2009 JetBrains s.r.o.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package com.intellij.psi.controlFlow;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.psi.PsiElement;
21 import com.intellij.psi.PsiStatement;
22 import gnu.trove.TObjectIntHashMap;
23 import org.jetbrains.annotations.NotNull;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Stack;
29 class ControlFlowImpl implements ControlFlow {
30 private static final Logger LOG = Logger.getInstance("#com.intellij.psi.controlFlow.ControlFlowImpl");
32 private final List<Instruction> myInstructions = new ArrayList<Instruction>();
33 private final TObjectIntHashMap<PsiElement> myElementToStartOffsetMap = new TObjectIntHashMap<PsiElement>();
34 private final TObjectIntHashMap<PsiElement> myElementToEndOffsetMap = new TObjectIntHashMap<PsiElement>();
35 private final List<PsiElement> myElementsForInstructions = new ArrayList<PsiElement>();
36 private boolean myConstantConditionOccurred;
38 private final Stack<PsiElement> myElementStack = new Stack<PsiElement>();
40 public void addInstruction(Instruction instruction) {
41 myInstructions.add(instruction);
42 myElementsForInstructions.add(myElementStack.peek());
45 public void startElement(PsiElement element) {
46 myElementStack.push(element);
47 myElementToStartOffsetMap.put(element, myInstructions.size());
49 if (LOG.isDebugEnabled()){
50 if (element instanceof PsiStatement){
51 String text = element.getText();
52 int index = Math.min(text.indexOf('\n'), text.indexOf('\r'));
53 if (index >= 0){
54 text = text.substring(0, index);
56 addInstruction(new CommentInstruction(text));
61 public void finishElement(PsiElement element) {
62 LOG.assertTrue(myElementStack.pop().equals(element));
63 myElementToEndOffsetMap.put(element, myInstructions.size());
66 @NotNull
67 public List<Instruction> getInstructions() {
68 return myInstructions;
70 public int getSize() {
71 return myInstructions.size();
74 public int getStartOffset(@NotNull PsiElement element) {
75 int value = myElementToStartOffsetMap.get(element);
76 if (value == 0){
77 if (!myElementToStartOffsetMap.containsKey(element)) return -1;
79 return value;
82 public int getEndOffset(@NotNull PsiElement element) {
83 int value = myElementToEndOffsetMap.get(element);
84 if (value == 0){
85 if (!myElementToEndOffsetMap.containsKey(element)) return -1;
87 return value;
90 public PsiElement getElement(int offset) {
91 return myElementsForInstructions.get(offset);
94 public boolean isConstantConditionOccurred() {
95 return myConstantConditionOccurred;
97 public void setConstantConditionOccurred(boolean constantConditionOccurred) {
98 myConstantConditionOccurred = constantConditionOccurred;
101 public String toString() {
102 StringBuilder buffer = new StringBuilder();
103 for(int i = 0; i < myInstructions.size(); i++){
104 Instruction instruction = myInstructions.get(i);
105 buffer.append(Integer.toString(i));
106 buffer.append(": ");
107 buffer.append(instruction.toString());
108 buffer.append("\n");
110 return buffer.toString();