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.elements;
018
019import com.intellij.lang.ASTNode;
020import com.intellij.psi.stubs.IndexSink;
021import com.intellij.psi.stubs.StubElement;
022import com.intellij.psi.stubs.StubInputStream;
023import com.intellij.psi.stubs.StubOutputStream;
024import com.intellij.util.io.StringRef;
025import org.jetbrains.annotations.NonNls;
026import org.jetbrains.annotations.NotNull;
027import org.jetbrains.jet.lang.psi.JetTypeParameter;
028import org.jetbrains.jet.lang.psi.JetTypeReference;
029import org.jetbrains.jet.lang.psi.stubs.PsiJetTypeParameterStub;
030import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetTypeParameterStubImpl;
031import org.jetbrains.jet.lang.types.Variance;
032
033import java.io.IOException;
034
035public class JetTypeParameterElementType extends JetStubElementType<PsiJetTypeParameterStub, JetTypeParameter> {
036    public JetTypeParameterElementType(@NotNull @NonNls String debugName) {
037        super(debugName);
038    }
039
040    @Override
041    public JetTypeParameter createPsiFromAst(@NotNull ASTNode node) {
042        return new JetTypeParameter(node);
043    }
044
045    @Override
046    public JetTypeParameter createPsi(@NotNull PsiJetTypeParameterStub stub) {
047        return new JetTypeParameter(stub, JetStubElementTypes.TYPE_PARAMETER);
048    }
049
050    @Override
051    public PsiJetTypeParameterStub createStub(@NotNull JetTypeParameter psi, StubElement parentStub) {
052        JetTypeReference extendsBound = psi.getExtendsBound();
053        return new PsiJetTypeParameterStubImpl(JetStubElementTypes.TYPE_PARAMETER, parentStub,
054                psi.getName(),
055                extendsBound != null ? extendsBound.getText() : null,
056                psi.getVariance() == Variance.IN_VARIANCE,
057                psi.getVariance() == Variance.OUT_VARIANCE);
058    }
059
060    @Override
061    public void serialize(PsiJetTypeParameterStub stub, StubOutputStream dataStream) throws IOException {
062        dataStream.writeName(stub.getName());
063        dataStream.writeName(stub.getExtendBoundTypeText());
064        dataStream.writeBoolean(stub.isInVariance());
065        dataStream.writeBoolean(stub.isOutVariance());
066    }
067
068    @Override
069    public PsiJetTypeParameterStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
070        StringRef name = dataStream.readName();
071        StringRef extendBoundTypeText = dataStream.readName();
072        boolean isInVariance = dataStream.readBoolean();
073        boolean isOutVariance = dataStream.readBoolean();
074
075        return new PsiJetTypeParameterStubImpl(JetStubElementTypes.TYPE_PARAMETER, parentStub,
076                name, extendBoundTypeText, isInVariance, isOutVariance);
077    }
078
079    @Override
080    public void indexStub(PsiJetTypeParameterStub stub, IndexSink sink) {
081        // No index
082    }
083}