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.diagnostics;
018    
019    import org.jetbrains.annotations.NotNull;
020    
021    import java.util.Arrays;
022    import java.util.Collection;
023    
024    public abstract class DiagnosticFactory<D extends Diagnostic> {
025    
026        private String name = null;
027        private final Severity severity;
028    
029        protected DiagnosticFactory(@NotNull Severity severity) {
030            this.severity = severity;
031        }
032    
033        protected DiagnosticFactory(@NotNull String name, @NotNull Severity severity) {
034            this.name = name;
035            this.severity = severity;
036        }
037    
038        /*package*/ void setName(@NotNull String name) {
039            this.name = name;
040        }
041    
042        @NotNull
043        public String getName() {
044            return name;
045        }
046    
047        @NotNull
048        public Severity getSeverity() {
049            return severity;
050        }
051    
052        @NotNull
053        public D cast(@NotNull Diagnostic diagnostic) {
054            if (diagnostic.getFactory() != this) {
055                throw new IllegalArgumentException("Factory mismatch: expected " + this + " but was " + diagnostic.getFactory());
056            }
057    
058            //noinspection unchecked
059            return (D) diagnostic;
060        }
061    
062        @NotNull
063        public static <D extends Diagnostic> D cast(@NotNull Diagnostic diagnostic, @NotNull DiagnosticFactory<? extends D>... factories) {
064            return cast(diagnostic, Arrays.asList(factories));
065        }
066    
067        @NotNull
068        public static <D extends Diagnostic> D cast(@NotNull Diagnostic diagnostic, @NotNull Collection<? extends DiagnosticFactory<? extends D>> factories) {
069            for (DiagnosticFactory<? extends D> factory : factories) {
070                if (diagnostic.getFactory() == factory) return factory.cast(diagnostic);
071            }
072    
073            throw new IllegalArgumentException("Factory mismatch: expected one of " + factories + " but was " + diagnostic.getFactory());
074        }
075    
076        @Override
077        public String toString() {
078            return getName();
079        }
080    }