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;
018    
019    import com.intellij.extapi.psi.StubBasedPsiElementBase;
020    import com.intellij.lang.ASTNode;
021    import com.intellij.lang.Language;
022    import com.intellij.psi.PsiElement;
023    import com.intellij.psi.PsiElementVisitor;
024    import com.intellij.psi.StubBasedPsiElement;
025    import com.intellij.psi.stubs.IStubElementType;
026    import com.intellij.psi.stubs.StubElement;
027    import org.jetbrains.annotations.NotNull;
028    import org.jetbrains.jet.plugin.JetLanguage;
029    
030    public class JetElementImplStub<T extends StubElement> extends StubBasedPsiElementBase<T>
031            implements JetElement, StubBasedPsiElement<T> {
032        public JetElementImplStub(@NotNull T stub, @NotNull IStubElementType nodeType) {
033            super(stub, nodeType);
034        }
035    
036        public JetElementImplStub(@NotNull ASTNode node) {
037            super(node);
038        }
039    
040        @NotNull
041        @Override
042        public Language getLanguage() {
043            return JetLanguage.INSTANCE;
044        }
045    
046        @Override
047        public String toString() {
048            return getNode().getElementType().toString();
049        }
050    
051        @Override
052        public final void accept(@NotNull PsiElementVisitor visitor) {
053            if (visitor instanceof JetVisitorVoid) {
054                accept((JetVisitorVoid) visitor);
055            }
056            else {
057                visitor.visitElement(this);
058            }
059        }
060    
061        @Override
062        public <D> void acceptChildren(@NotNull JetTreeVisitor<D> visitor, D data) {
063            PsiElement child = getFirstChild();
064            while (child != null) {
065                if (child instanceof JetElement) {
066                    ((JetElement) child).accept(visitor, data);
067                }
068                child = child.getNextSibling();
069            }
070        }
071    
072        @Override
073        public void accept(@NotNull JetVisitorVoid visitor) {
074            visitor.visitJetElement(this);
075        }
076    
077        @Override
078        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
079            return visitor.visitJetElement(this, data);
080        }
081    }