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.resolve.diagnostics;
018    
019    import com.intellij.openapi.util.Condition;
020    import com.intellij.openapi.util.ModificationTracker;
021    import com.intellij.psi.PsiElement;
022    import com.intellij.util.containers.FilteringIterator;
023    import kotlin.collections.CollectionsKt;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.TestOnly;
026    import org.jetbrains.kotlin.diagnostics.Diagnostic;
027    import org.jetbrains.kotlin.resolve.BindingContext;
028    
029    import java.util.Collection;
030    import java.util.Iterator;
031    
032    public class DiagnosticsWithSuppression implements Diagnostics {
033        private final KotlinSuppressCache kotlinSuppressCache;
034        private final Collection<Diagnostic> diagnostics;
035        private final DiagnosticsElementsCache elementsCache;
036    
037        public DiagnosticsWithSuppression(@NotNull BindingContext context, @NotNull Collection<Diagnostic> diagnostics) {
038            this.diagnostics = diagnostics;
039            this.kotlinSuppressCache = new BindingContextSuppressCache(context);
040            this.elementsCache = new DiagnosticsElementsCache(this, kotlinSuppressCache.getFilter());
041        }
042    
043        @NotNull
044        @Override
045        public Diagnostics noSuppression() {
046            return new SimpleDiagnostics(diagnostics);
047        }
048    
049        @NotNull
050        @Override
051        public Iterator<Diagnostic> iterator() {
052            return new FilteringIterator<Diagnostic, Diagnostic>(diagnostics.iterator(), new Condition<Diagnostic>() {
053                @Override
054                public boolean value(Diagnostic diagnostic) {
055                    return kotlinSuppressCache.getFilter().invoke(diagnostic);
056                }
057            });
058        }
059    
060        @NotNull
061        @Override
062        public Collection<Diagnostic> all() {
063            return CollectionsKt.filter(diagnostics, kotlinSuppressCache.getFilter());
064        }
065    
066        @NotNull
067        @Override
068        public Collection<Diagnostic> forElement(@NotNull PsiElement psiElement) {
069            return elementsCache.getDiagnostics(psiElement);
070        }
071    
072        @Override
073        public boolean isEmpty() {
074            return all().isEmpty();
075        }
076    
077        @NotNull
078        @Override
079        public ModificationTracker getModificationTracker() {
080            throw new IllegalStateException("Trying to obtain modification tracker for readonly DiagnosticsWithSuppression.");
081        }
082    
083        @TestOnly
084        @NotNull
085        public Collection<Diagnostic> getDiagnostics() {
086            return diagnostics;
087        }
088    }