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.diagnostics.rendering;
018
019import com.intellij.psi.PsiElement;
020import org.jetbrains.annotations.NotNull;
021import org.jetbrains.annotations.Nullable;
022import org.jetbrains.jet.lang.diagnostics.*;
023
024import java.util.HashMap;
025import java.util.Map;
026
027public final class DiagnosticFactoryToRendererMap {
028    private final Map<AbstractDiagnosticFactory, DiagnosticRenderer<?>> map =
029            new HashMap<AbstractDiagnosticFactory, DiagnosticRenderer<?>>();
030    private boolean immutable = false;
031
032    private void checkMutability() {
033        if (immutable) {
034            throw new IllegalStateException("factory to renderer map is already immutable");
035        }
036    }
037
038    public <E extends PsiElement> void put(@NotNull DiagnosticFactory0<E> factory, @NotNull String message) {
039        checkMutability();
040        map.put(factory, new SimpleDiagnosticRenderer(message));
041    }
042
043    public <E extends PsiElement, A> void put(@NotNull DiagnosticFactory1<E, A> factory, @NotNull String message, @Nullable Renderer<? super A> rendererA) {
044        checkMutability();
045        map.put(factory, new DiagnosticWithParameters1Renderer<A>(message, rendererA));
046    }
047
048    public <E extends PsiElement, A, B> void put(@NotNull DiagnosticFactory2<E, A, B> factory,
049            @NotNull String message,
050            @Nullable Renderer<? super A> rendererA,
051            @Nullable Renderer<? super B> rendererB) {
052        checkMutability();
053        map.put(factory, new DiagnosticWithParameters2Renderer<A, B>(message, rendererA, rendererB));
054    }
055
056    public <E extends PsiElement, A, B, C> void put(@NotNull DiagnosticFactory3<E, A, B, C> factory,
057            @NotNull String message,
058            @Nullable Renderer<? super A> rendererA,
059            @Nullable Renderer<? super B> rendererB,
060            @Nullable  Renderer<? super C> rendererC) {
061        checkMutability();
062        map.put(factory, new DiagnosticWithParameters3Renderer<A, B, C>(message, rendererA, rendererB, rendererC));
063    }
064
065    @Nullable
066    public DiagnosticRenderer<?> get(@NotNull AbstractDiagnosticFactory factory) {
067        return map.get(factory);
068    }
069
070    public void setImmutable() {
071        immutable = false;
072    }
073}