001    /*
002     * Copyright 2010-2015 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    
017    package org.jetbrains.kotlin.psi.stubs.impl;
018    
019    import com.google.common.collect.Lists;
020    import com.intellij.psi.PsiClass;
021    import com.intellij.psi.impl.java.stubs.PsiClassStub;
022    import com.intellij.psi.stubs.PsiClassHolderFileStub;
023    import com.intellij.psi.stubs.PsiFileStubImpl;
024    import com.intellij.psi.stubs.StubElement;
025    import com.intellij.psi.tree.IStubFileElementType;
026    import com.intellij.util.io.StringRef;
027    import org.jetbrains.annotations.NotNull;
028    import org.jetbrains.kotlin.psi.JetFile;
029    import org.jetbrains.kotlin.psi.stubs.KotlinFileStub;
030    import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
031    import org.jetbrains.kotlin.name.FqName;
032    
033    import java.util.List;
034    
035    public class KotlinFileStubImpl extends PsiFileStubImpl<JetFile> implements KotlinFileStub, PsiClassHolderFileStub<JetFile> {
036    
037        private final StringRef packageName;
038        private final boolean isScript;
039    
040        public KotlinFileStubImpl(JetFile jetFile, @NotNull StringRef packageName, boolean isScript) {
041            super(jetFile);
042            this.packageName = packageName;
043            // SCRIPT: PsiJetFileStubImpl knows about scripting
044            this.isScript = isScript;
045        }
046    
047        public KotlinFileStubImpl(JetFile jetFile, @NotNull String packageName, boolean isScript) {
048            this(jetFile, StringRef.fromString(packageName), isScript);
049        }
050    
051        @Override
052        @NotNull
053        public FqName getPackageFqName() {
054            return new FqName(StringRef.toString(packageName));
055        }
056    
057        @Override
058        public boolean isScript() {
059            return isScript;
060        }
061    
062        @Override
063        public IStubFileElementType getType() {
064            return JetStubElementTypes.FILE;
065        }
066    
067        @Override
068        public String toString() {
069            return "PsiJetFileStubImpl[" + "package=" + getPackageFqName().asString() + "]";
070        }
071    
072        @Override
073        public PsiClass[] getClasses() {
074            List<PsiClass> result = Lists.newArrayList();
075            for (StubElement child : getChildrenStubs()) {
076                if (child instanceof PsiClassStub) {
077                    result.add((PsiClass) child.getPsi());
078                }
079            }
080            return result.toArray(new PsiClass[result.size()]);
081        }
082    }