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    
017    package org.jetbrains.jet.lang.psi.stubs.impl;
018    
019    import com.intellij.psi.stubs.StubBase;
020    import com.intellij.psi.stubs.StubElement;
021    import com.intellij.util.ArrayUtil;
022    import com.intellij.util.io.StringRef;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.jet.lang.psi.JetNamedFunction;
026    import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
027    import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
028    import org.jetbrains.jet.lang.resolve.name.FqName;
029    
030    public class PsiJetFunctionStubImpl extends StubBase<JetNamedFunction> implements PsiJetFunctionStub {
031    
032        private final StringRef nameRef;
033        private final boolean isTopLevel;
034        private final boolean isExtension;
035        private final FqName fqName;
036    
037        public PsiJetFunctionStubImpl(
038                @NotNull StubElement parent,
039                @Nullable String name,
040                boolean isTopLevel,
041                @Nullable FqName fqName,
042                boolean isExtension
043        ) {
044            this(parent, StringRef.fromString(name), isTopLevel, fqName, isExtension);
045        }
046    
047        public PsiJetFunctionStubImpl(
048                @NotNull StubElement parent,
049                @Nullable StringRef nameRef,
050                boolean isTopLevel,
051                @Nullable FqName fqName,
052                boolean isExtension
053        ) {
054            super(parent, JetStubElementTypes.FUNCTION);
055    
056            if (isTopLevel && fqName == null) {
057                throw new IllegalArgumentException("fqName shouldn't be null for top level functions");
058            }
059    
060            this.nameRef = nameRef;
061            this.fqName = fqName;
062            this.isTopLevel = isTopLevel;
063            this.isExtension = isExtension;
064        }
065    
066        @Override
067        public String getName() {
068            return StringRef.toString(nameRef);
069        }
070    
071        @Override
072        public boolean isTopLevel() {
073            return isTopLevel;
074        }
075    
076        @Override
077        public boolean isExtension() {
078            return isExtension;
079        }
080    
081        @NotNull
082        @Override
083        public String[] getAnnotations() {
084            // TODO (stubs)
085            return ArrayUtil.EMPTY_STRING_ARRAY;
086        }
087    
088        @Override
089        public String toString() {
090            StringBuilder builder = new StringBuilder();
091            builder.append("PsiJetFunctionStubImpl[");
092    
093            if (isTopLevel()) {
094                assert fqName != null;
095                builder.append("top ").append("fqName=").append(fqName.toString()).append(" ");
096            }
097    
098            if (isExtension()) {
099                builder.append("ext ");
100            }
101    
102            builder.append("name=").append(getName());
103    
104            builder.append("]");
105    
106            return builder.toString();
107        }
108    
109        @Nullable
110        @Override
111        public FqName getFqName() {
112            return fqName;
113        }
114    }