All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.diagnostics.DiagnosticFactory.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.diagnostics

import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer
import java.lang.IllegalArgumentException

abstract class DiagnosticFactory protected constructor(
    private var _name: String?,
    open val severity: Severity
) {
    open val name: String
        get() = _name!!

    fun initializeName(name: String) {
        _name = name
    }

    open var defaultRenderer: DiagnosticRenderer? = null

    protected constructor(severity: Severity) : this(null, severity)

    @Suppress("UNCHECKED_CAST")
    fun initDefaultRenderer(defaultRenderer: DiagnosticRenderer<*>?) {
        this.defaultRenderer = defaultRenderer as DiagnosticRenderer?
    }

    fun cast(diagnostic: UnboundDiagnostic): D {
        require(!(diagnostic.factory !== this)) { "Factory mismatch: expected " + this + " but was " + diagnostic.factory }
        @Suppress("UNCHECKED_CAST")
        return diagnostic as D
    }

    override fun toString(): String {
        return _name ?: ""
    }

    companion object {
        @SafeVarargs
        fun  cast(diagnostic: UnboundDiagnostic, vararg factories: DiagnosticFactory): D {
            return cast(diagnostic, listOf(*factories))
        }

        fun  cast(diagnostic: UnboundDiagnostic, factories: Collection>): D {
            for (factory in factories) {
                if (diagnostic.factory === factory) return factory.cast(diagnostic)
            }
            throw IllegalArgumentException("Factory mismatch: expected one of " + factories + " but was " + diagnostic.factory)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy