001    /*
002     * Copyright 2010-2014 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;
018    
019    import com.google.common.collect.Lists;
020    import com.intellij.lang.ASTNode;
021    import com.intellij.psi.PsiElement;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.jet.JetNodeTypes;
025    import org.jetbrains.jet.lexer.JetKeywordToken;
026    import org.jetbrains.jet.lexer.JetModifierKeywordToken;
027    import org.jetbrains.jet.lexer.JetToken;
028    
029    import java.util.ArrayList;
030    import java.util.Collections;
031    import java.util.List;
032    
033    public class JetModifierList extends JetElementImpl {
034        public JetModifierList(@NotNull ASTNode node) {
035            super(node);
036        }
037    
038        @Override
039        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
040            return visitor.visitModifierList(this, data);
041        }
042    
043        @NotNull
044        public List<JetAnnotation> getAnnotations() {
045            return findChildrenByType(JetNodeTypes.ANNOTATION);
046        }
047    
048        @NotNull
049        public List<JetAnnotationEntry> getAnnotationEntries() {
050            List<JetAnnotationEntry> entries = findChildrenByType(JetNodeTypes.ANNOTATION_ENTRY);
051            List<JetAnnotationEntry> answer = entries.isEmpty() ? null : Lists.newArrayList(entries);
052            for (JetAnnotation annotation : getAnnotations()) {
053                if (answer == null) answer = new ArrayList<JetAnnotationEntry>();
054                answer.addAll(annotation.getEntries());
055            }
056            return answer != null ? answer : Collections.<JetAnnotationEntry>emptyList();
057        }
058    
059        @NotNull
060        public List<ASTNode> getModifierNodes() {
061            List<ASTNode> modifierNodes = new ArrayList<ASTNode>();
062    
063            ASTNode node = getNode().getFirstChildNode();
064            while (node != null) {
065                if (node.getElementType() instanceof JetModifierKeywordToken) {
066                    modifierNodes.add(node);
067                }
068                node = node.getTreeNext();
069            }
070            return modifierNodes;
071        }
072    
073        public boolean hasModifier(JetModifierKeywordToken token) {
074            return getModifierNode(token) != null;
075        }
076    
077        @Nullable
078        public PsiElement getModifier(JetModifierKeywordToken token) {
079            return findChildByType(token);
080        }
081    
082        @Nullable
083        public ASTNode getModifierNode(JetToken token) {
084            ASTNode node = getNode().getFirstChildNode();
085            while (node != null) {
086                if (node.getElementType() == token) return node;
087                node = node.getTreeNext();
088            }
089            return null;
090        }
091    
092    
093    }