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.PsiComment;
021    import com.intellij.psi.PsiElement;
022    import com.intellij.psi.PsiWhiteSpace;
023    import com.intellij.psi.util.PsiTreeUtil;
024    import com.intellij.util.IncorrectOperationException;
025    import org.jetbrains.annotations.NotNull;
026    import org.jetbrains.kotlin.lexer.KtTokens;
027    import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
028    import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
029    
030    import java.util.Arrays;
031    import java.util.List;
032    import java.util.concurrent.atomic.AtomicLong;
033    
034    public class KtSuperTypeList extends KtElementImplStub<KotlinPlaceHolderStub<KtSuperTypeList>> {
035        private final AtomicLong modificationStamp = new AtomicLong();
036    
037        public KtSuperTypeList(@NotNull ASTNode node) {
038            super(node);
039        }
040    
041        public KtSuperTypeList(@NotNull KotlinPlaceHolderStub<KtSuperTypeList> stub) {
042            super(stub, KtStubElementTypes.SUPER_TYPE_LIST);
043        }
044    
045        @Override
046        public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
047            return visitor.visitSuperTypeList(this, data);
048        }
049    
050        @NotNull
051        public KtSuperTypeListEntry addEntry(@NotNull KtSuperTypeListEntry entry) {
052            return EditCommaSeparatedListHelper.INSTANCE.addItem(this, getEntries(), entry);
053        }
054    
055        public void removeEntry(@NotNull KtSuperTypeListEntry entry) {
056            EditCommaSeparatedListHelper.INSTANCE.removeItem(entry);
057            if (getEntries().isEmpty()) {
058                delete();
059            }
060        }
061    
062        @Override
063        public void delete() throws IncorrectOperationException {
064            PsiElement left = PsiTreeUtil.skipSiblingsBackward(this, PsiWhiteSpace.class, PsiComment.class);
065            if (left == null || left.getNode().getElementType() != KtTokens.COLON) left = this;
066            getParent().deleteChildRange(left, this);
067        }
068    
069        public List<KtSuperTypeListEntry> getEntries() {
070            return Arrays.asList(getStubOrPsiChildren(KtStubElementTypes.SUPER_TYPE_LIST_ENTRIES, KtSuperTypeListEntry.ARRAY_FACTORY));
071        }
072    
073    
074        @Override
075        public void subtreeChanged() {
076            super.subtreeChanged();
077            modificationStamp.getAndIncrement();
078        }
079    
080        public long getModificationStamp() {
081            return modificationStamp.get();
082        }
083    }