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.elements;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.psi.stubs.StubElement;
022    import com.intellij.psi.stubs.StubInputStream;
023    import com.intellij.psi.stubs.StubOutputStream;
024    import com.intellij.util.io.StringRef;
025    import org.jetbrains.annotations.NonNls;
026    import org.jetbrains.annotations.NotNull;
027    import org.jetbrains.jet.lang.psi.JetParameter;
028    import org.jetbrains.jet.lang.psi.stubs.PsiJetParameterStub;
029    import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetParameterStubImpl;
030    import org.jetbrains.jet.lang.resolve.name.FqName;
031    
032    import java.io.IOException;
033    
034    public class JetParameterElementType extends JetStubElementType<PsiJetParameterStub, JetParameter> {
035        public JetParameterElementType(@NotNull @NonNls String debugName) {
036            super(debugName, JetParameter.class, PsiJetParameterStub.class);
037        }
038    
039        @Override
040        public PsiJetParameterStub createStub(@NotNull JetParameter psi, StubElement parentStub) {
041            FqName fqName = psi.getFqName();
042            StringRef fqNameRef = StringRef.fromString(fqName != null ? fqName.asString() : null);
043            return new PsiJetParameterStubImpl(parentStub, fqNameRef, StringRef.fromString(psi.getName()),
044                                               psi.isMutable(), psi.hasValOrVarNode(), psi.hasDefaultValue());
045        }
046    
047        @Override
048        public void serialize(@NotNull PsiJetParameterStub stub, @NotNull StubOutputStream dataStream) throws IOException {
049            dataStream.writeName(stub.getName());
050            dataStream.writeBoolean(stub.isMutable());
051            dataStream.writeBoolean(stub.hasValOrValNode());
052            dataStream.writeBoolean(stub.hasDefaultValue());
053            FqName name = stub.getFqName();
054            dataStream.writeName(name != null ? name.asString() : null);
055        }
056    
057        @NotNull
058        @Override
059        public PsiJetParameterStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
060            StringRef name = dataStream.readName();
061            boolean isMutable = dataStream.readBoolean();
062            boolean hasValOrValNode = dataStream.readBoolean();
063            boolean hasDefaultValue = dataStream.readBoolean();
064            StringRef fqName = dataStream.readName();
065    
066             return new PsiJetParameterStubImpl(parentStub, fqName, name, isMutable, hasValOrValNode, hasDefaultValue);
067        }
068    }