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    
017    package org.jetbrains.jet.lang.resolve;
018    
019    import com.intellij.openapi.util.AtomicNotNullLazyValue;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.util.containers.ConcurrentMultiMap;
022    import com.intellij.util.containers.MultiMap;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.jet.lang.diagnostics.Diagnostic;
026    
027    import java.util.Collection;
028    
029    public class DiagnosticsElementsCache {
030        private final Diagnostics diagnostics;
031    
032        private final AtomicNotNullLazyValue<MultiMap<PsiElement, Diagnostic>> elementToDiagnostic = new AtomicNotNullLazyValue<MultiMap<PsiElement, Diagnostic>>() {
033            @NotNull
034            @Override
035            protected MultiMap<PsiElement, Diagnostic> compute() {
036                return buildElementToDiagnosticCache(diagnostics);
037            }
038        };
039    
040        public DiagnosticsElementsCache(Diagnostics diagnostics) {
041            this.diagnostics = diagnostics;
042        }
043    
044        @NotNull
045        public Collection<Diagnostic> getDiagnostics(@NotNull PsiElement psiElement) {
046            return elementToDiagnostic.getValue().get(psiElement);
047        }
048    
049        private static MultiMap<PsiElement, Diagnostic> buildElementToDiagnosticCache(Diagnostics diagnostics) {
050            MultiMap<PsiElement, Diagnostic> elementToDiagnostic = new ConcurrentMultiMap<PsiElement, Diagnostic>();
051            for (Diagnostic diagnostic : diagnostics) {
052                elementToDiagnostic.putValue(diagnostic.getPsiElement(), diagnostic);
053            }
054    
055            return elementToDiagnostic;
056        }
057    }