001/*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.jetbrains.jet.lang.psi.stubs.impl;
018
019import com.google.common.collect.Lists;
020import com.intellij.psi.PsiClass;
021import com.intellij.psi.impl.java.stubs.PsiClassStub;
022import com.intellij.psi.stubs.PsiClassHolderFileStub;
023import com.intellij.psi.stubs.PsiFileStubImpl;
024import com.intellij.psi.stubs.StubElement;
025import com.intellij.psi.tree.IStubFileElementType;
026import com.intellij.util.io.StringRef;
027import org.jetbrains.jet.lang.psi.JetFile;
028import org.jetbrains.jet.lang.psi.stubs.PsiJetFileStub;
029import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
030
031import java.util.List;
032
033public class PsiJetFileStubImpl extends PsiFileStubImpl<JetFile> implements PsiJetFileStub, PsiClassHolderFileStub<JetFile> {
034
035    private final StringRef packageName;
036    private final boolean isScript;
037
038    public PsiJetFileStubImpl(JetFile jetFile, StringRef packageName, boolean isScript) {
039        super(jetFile);
040        this.packageName = packageName;
041        this.isScript = isScript;
042    }
043
044    public PsiJetFileStubImpl(JetFile jetFile, String packageName, boolean isScript) {
045        this(jetFile, StringRef.fromString(packageName), isScript);
046    }
047
048    @Override
049    public String getPackageName() {
050        return StringRef.toString(packageName);
051    }
052
053    @Override
054    public boolean isScript() {
055        return isScript;
056    }
057
058    @Override
059    public IStubFileElementType getType() {
060        return JetStubElementTypes.FILE;
061    }
062
063    @Override
064    public String toString() {
065        StringBuilder builder = new StringBuilder();
066
067        builder.append("PsiJetFileStubImpl[");
068
069
070        builder.append("package=").append(getPackageName());
071        builder.append("]");
072
073        return builder.toString();
074    }
075
076    @Override
077    public PsiClass[] getClasses() {
078        List<PsiClass> result = Lists.newArrayList();
079        for (StubElement child : getChildrenStubs()) {
080            if (child instanceof PsiClassStub) {
081                result.add((PsiClass) child.getPsi());
082            }
083        }
084        return result.toArray(new PsiClass[result.size()]);
085    }
086}