001    /*
002     * Copyright 2010-2015 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.kotlin.psi;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiClass;
021    import com.intellij.psi.PsiElement;
022    import com.intellij.psi.PsiEnumConstant;
023    import com.intellij.util.IncorrectOperationException;
024    import org.jetbrains.annotations.NonNls;
025    import org.jetbrains.annotations.NotNull;
026    import org.jetbrains.annotations.Nullable;
027    import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub;
028    import org.jetbrains.kotlin.psi.stubs.KotlinClassStub;
029    import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
030    
031    import java.util.Collections;
032    import java.util.List;
033    
034    public class KtEnumEntry extends KtClass {
035        public KtEnumEntry(@NotNull ASTNode node) {
036            super(node);
037        }
038    
039        public KtEnumEntry(@NotNull KotlinClassStub stub) {
040            super(stub);
041        }
042    
043        @Override
044        public String getName() {
045            KotlinClassOrObjectStub<? extends KtClassOrObject> classStub = getStub();
046            if (classStub != null) {
047                return classStub.getName();
048            }
049    
050            KtObjectDeclarationName nameAsDeclaration = getNameAsDeclaration();
051            return nameAsDeclaration == null ? "<Anonymous>" : nameAsDeclaration.getName();
052        }
053    
054        @Override
055        public PsiElement getNameIdentifier() {
056            KtObjectDeclarationName nameAsDeclaration = getNameAsDeclaration();
057            return nameAsDeclaration == null ? null : nameAsDeclaration.getNameIdentifier();
058        }
059    
060        @Override
061        public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
062            KtObjectDeclarationName nameAsDeclaration = getNameAsDeclaration();
063            return nameAsDeclaration == null ? null : nameAsDeclaration.setName(name);
064        }
065    
066        @NotNull
067        @Override
068        public List<KtDelegationSpecifier> getDelegationSpecifiers() {
069            KtInitializerList initializerList = getInitializerList();
070            if (initializerList == null) {
071                return Collections.emptyList();
072            }
073            return initializerList.getInitializers();
074        }
075    
076        public boolean hasInitializer() {
077            return !getDelegationSpecifiers().isEmpty();
078        }
079    
080        @Nullable
081        public KtInitializerList getInitializerList() {
082            return getStubOrPsiChild(KtStubElementTypes.INITIALIZER_LIST);
083        }
084    
085        @Override
086        public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
087            return visitor.visitEnumEntry(this, data);
088        }
089    
090        @Override
091        public boolean isEquivalentTo(@Nullable PsiElement another) {
092            if (another instanceof PsiEnumConstant) {
093                PsiEnumConstant enumConstant = (PsiEnumConstant) another;
094                PsiClass containingClass = enumConstant.getContainingClass();
095                if (containingClass != null) {
096                    String containingClassQName = containingClass.getQualifiedName();
097                    if (containingClassQName != null && enumConstant.getName() != null) {
098                        String theirFQName = containingClassQName + "." + enumConstant.getName();
099                        if (theirFQName.equals(getQualifiedName())) {
100                            return true;
101                        }
102                    }
103                }
104            }
105            return super.isEquivalentTo(another);
106        }
107    }