update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / compiled / ClsRepositoryPsiElement.java
blob6d48380dedd0b3fc66f4360cb92ba14d6e29b6d7
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.psi.impl.compiled;
18 import com.intellij.psi.PsiElement;
19 import com.intellij.psi.PsiFile;
20 import com.intellij.psi.PsiInvalidElementAccessException;
21 import com.intellij.psi.PsiManager;
22 import com.intellij.psi.stubs.PsiFileStub;
23 import com.intellij.psi.stubs.StubElement;
24 import com.intellij.util.ArrayUtil;
25 import org.jetbrains.annotations.NotNull;
27 import java.util.List;
29 public abstract class ClsRepositoryPsiElement<T extends StubElement> extends ClsElementImpl {
30 private final T myStub;
32 protected ClsRepositoryPsiElement(final T stub) {
33 myStub = stub;
36 public PsiElement getParent() {
37 return myStub.getParentStub().getPsi();
40 public PsiManager getManager() {
41 final PsiFile file = getContainingFile();
42 if (file == null) throw new PsiInvalidElementAccessException(this);
43 return file.getManager();
46 public PsiFile getContainingFile() {
47 StubElement p = myStub;
48 while (!(p instanceof PsiFileStub)) {
49 p = p.getParentStub();
51 return (PsiFile)p.getPsi();
54 public T getStub() {
55 return myStub;
58 @NotNull
59 public PsiElement[] getChildren() {
60 final List stubs = getStub().getChildrenStubs();
61 PsiElement[] children = new PsiElement[stubs.size()];
62 for (int i = 0; i < stubs.size(); i++) {
63 children[i] = ((StubElement)stubs.get(i)).getPsi();
65 return children;
68 public PsiElement getFirstChild() {
69 final List<StubElement> children = getStub().getChildrenStubs();
70 if (children.isEmpty()) return null;
71 return children.get(0).getPsi();
74 public PsiElement getLastChild() {
75 final List<StubElement> children = getStub().getChildrenStubs();
76 if (children.isEmpty()) return null;
77 return children.get(children.size() - 1).getPsi();
80 public PsiElement getNextSibling() {
81 final PsiElement[] psiElements = getParent().getChildren();
82 final int i = ArrayUtil.indexOf(psiElements, this);
83 if (i < 0 || i >= psiElements.length - 1) {
84 return null;
86 return psiElements[i + 1];
90 public PsiElement getPrevSibling() {
91 final PsiElement[] psiElements = getParent().getChildren();
92 final int i = ArrayUtil.indexOf(psiElements, this);
93 if (i < 1) {
94 return null;
96 return psiElements[i - 1];