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.*;
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;
25 import java.util.List;
27 public abstract class ClsRepositoryPsiElement<T extends StubElement> extends ClsElementImpl implements StubBasedPsiElement<T> {
28 private final T myStub;
30 protected ClsRepositoryPsiElement(final T stub) {
34 public IStubElementType getElementType() {
35 return myStub.getStubType();
38 public PsiElement getParent() {
39 return myStub.getParentStub().getPsi();
42 public PsiManager getManager() {
43 final PsiFile file = getContainingFile();
44 if (file == null) throw new PsiInvalidElementAccessException(this);
45 return file.getManager();
48 public PsiFile getContainingFile() {
49 StubElement p = myStub;
50 while (!(p instanceof PsiFileStub)) {
51 p = p.getParentStub();
53 return (PsiFile)p.getPsi();
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();
70 public PsiElement getFirstChild() {
71 final List<StubElement> children = getStub().getChildrenStubs();
72 if (children.isEmpty()) return null;
73 return children.get(0).getPsi();
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();
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) {
88 return psiElements[i + 1];
92 public PsiElement getPrevSibling() {
93 final PsiElement[] psiElements = getParent().getChildren();
94 final int i = ArrayUtil.indexOf(psiElements, this);
98 return psiElements[i - 1];