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.google.common.collect.Lists;
020import com.intellij.lang.ASTNode;
021import org.jetbrains.annotations.NotNull;
022import org.jetbrains.annotations.Nullable;
023import org.jetbrains.jet.JetNodeTypes;
024import org.jetbrains.jet.lexer.JetToken;
025
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.List;
029
030public class JetModifierList extends JetElementImpl {
031    public JetModifierList(@NotNull ASTNode node) {
032        super(node);
033    }
034
035    @Override
036    public void accept(@NotNull JetVisitorVoid visitor) {
037        visitor.visitModifierList(this);
038    }
039
040    @Override
041    public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
042        return visitor.visitModifierList(this, data);
043    }
044
045    @NotNull
046    public List<JetAnnotation> getAnnotations() {
047        return findChildrenByType(JetNodeTypes.ANNOTATION);
048    }
049
050    @NotNull
051    public List<JetAnnotationEntry> getAnnotationEntries() {
052        List<JetAnnotationEntry> entries = findChildrenByType(JetNodeTypes.ANNOTATION_ENTRY);
053        List<JetAnnotationEntry> answer = entries.isEmpty() ? null : Lists.newArrayList(entries);
054        for (JetAnnotation annotation : getAnnotations()) {
055            if (answer == null) answer = new ArrayList<JetAnnotationEntry>();
056            answer.addAll(annotation.getEntries());
057        }
058        return answer != null ? answer : Collections.<JetAnnotationEntry>emptyList();
059    }
060
061    public boolean hasModifier(JetToken token) {
062        return getModifierNode(token) != null;
063    }
064
065    @Nullable
066    public ASTNode getModifierNode(JetToken token) {
067        ASTNode node = getNode().getFirstChildNode();
068        while (node != null) {
069            if (node.getElementType() == token) return node;
070            node = node.getTreeNext();
071        }
072        return null;
073    }
074}