Correct PsiAnchor for cls elements
[idea/community.git] / java / java-impl / src / com / intellij / psi / impl / compiled / ClsRepositoryPsiElement.java
1 /*
2  * Copyright 2000-2009 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.psi.impl.compiled;
17
18 import com.intellij.psi.*;
19 import com.intellij.psi.stubs.IStubElementType;
20 import com.intellij.psi.stubs.PsiFileStub;
21 import com.intellij.psi.stubs.StubElement;
22 import com.intellij.util.ArrayUtil;
23 import org.jetbrains.annotations.NotNull;
24
25 import java.util.List;
26
27 public abstract class ClsRepositoryPsiElement<T extends StubElement> extends ClsElementImpl implements StubBasedPsiElement<T> {
28   private final T myStub;
29
30   protected ClsRepositoryPsiElement(final T stub) {
31     myStub = stub;
32   }
33
34   public IStubElementType getElementType() {
35     return myStub.getStubType();
36   }
37
38   public PsiElement getParent() {
39     return myStub.getParentStub().getPsi();
40   }
41
42   public PsiManager getManager() {
43     final PsiFile file = getContainingFile();
44     if (file == null) throw new PsiInvalidElementAccessException(this);
45     return file.getManager();
46   }
47
48   public PsiFile getContainingFile() {
49     StubElement p = myStub;
50     while (!(p instanceof PsiFileStub)) {
51       p = p.getParentStub();
52     }
53     return (PsiFile)p.getPsi();
54   }
55
56   public T getStub() {
57     return myStub;
58   }
59
60   @NotNull
61   public PsiElement[] getChildren() {
62     final List stubs = getStub().getChildrenStubs();
63     PsiElement[] children = new PsiElement[stubs.size()];
64     for (int i = 0; i < stubs.size(); i++) {
65       children[i] = ((StubElement)stubs.get(i)).getPsi();
66     }
67     return children;
68   }
69
70   public PsiElement getFirstChild() {
71     final List<StubElement> children = getStub().getChildrenStubs();
72     if (children.isEmpty()) return null;
73     return children.get(0).getPsi();
74   }
75
76   public PsiElement getLastChild() {
77     final List<StubElement> children = getStub().getChildrenStubs();
78     if (children.isEmpty()) return null;
79     return children.get(children.size() - 1).getPsi();
80   }
81
82   public PsiElement getNextSibling() {
83     final PsiElement[] psiElements = getParent().getChildren();
84     final int i = ArrayUtil.indexOf(psiElements, this);
85     if (i < 0 || i >= psiElements.length - 1) {
86       return null;
87     }
88     return psiElements[i + 1];
89   }
90
91
92   public PsiElement getPrevSibling() {
93     final PsiElement[] psiElements = getParent().getChildren();
94     final int i = ArrayUtil.indexOf(psiElements, this);
95     if (i < 1) {
96       return null;
97     }
98     return psiElements[i - 1];
99   }
100 }