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;
018
019import com.intellij.lang.ASTNode;
020import com.intellij.psi.stubs.IStubElementType;
021import com.intellij.util.ArrayFactory;
022import org.jetbrains.annotations.NotNull;
023import org.jetbrains.annotations.Nullable;
024import org.jetbrains.jet.JetNodeTypes;
025import org.jetbrains.jet.lang.psi.stubs.PsiJetTypeParameterStub;
026import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
027import org.jetbrains.jet.lang.types.Variance;
028import org.jetbrains.jet.lexer.JetTokens;
029
030public class JetTypeParameter extends JetNamedDeclarationStub<PsiJetTypeParameterStub> {
031    public static final JetTypeParameter[] EMPTY_ARRAY = new JetTypeParameter[0];
032
033    public static final ArrayFactory<JetTypeParameter> ARRAY_FACTORY = new ArrayFactory<JetTypeParameter>() {
034        @Override
035        public JetTypeParameter[] create(int count) {
036            return count == 0 ? EMPTY_ARRAY : new JetTypeParameter[count];
037        }
038    };
039
040    public JetTypeParameter(@NotNull ASTNode node) {
041        super(node);
042    }
043
044    public JetTypeParameter(@NotNull PsiJetTypeParameterStub stub, @NotNull IStubElementType nodeType) {
045        super(stub, nodeType);
046    }
047
048    @Override
049    public void accept(@NotNull JetVisitorVoid visitor) {
050        visitor.visitTypeParameter(this);
051    }
052
053    @Override
054    public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
055        return visitor.visitTypeParameter(this, data);
056    }
057
058    @NotNull
059    public Variance getVariance() {
060        PsiJetTypeParameterStub stub = getStub();
061        if (stub != null) {
062            if (stub.isOutVariance()) return Variance.OUT_VARIANCE;
063            if (stub.isInVariance()) return Variance.IN_VARIANCE;
064            return Variance.INVARIANT;
065        }
066
067        JetModifierList modifierList = getModifierList();
068        if (modifierList == null) return Variance.INVARIANT;
069
070        if (modifierList.hasModifier(JetTokens.OUT_KEYWORD)) return Variance.OUT_VARIANCE;
071        if (modifierList.hasModifier(JetTokens.IN_KEYWORD)) return Variance.IN_VARIANCE;
072        return Variance.INVARIANT;
073    }
074
075    @Nullable
076    public JetTypeReference getExtendsBound() {
077        return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
078    }
079
080    @NotNull
081    @Override
082    public IStubElementType getElementType() {
083        return JetStubElementTypes.TYPE_PARAMETER;
084    }
085}