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