Convert DiagnosticFactory.java to Kotlin
This commit is contained in:
@@ -13,82 +13,42 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
package org.jetbrains.kotlin.diagnostics
|
||||||
|
|
||||||
package org.jetbrains.kotlin.diagnostics;
|
import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer
|
||||||
|
import java.lang.IllegalArgumentException
|
||||||
|
import java.lang.SafeVarargs
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
abstract class DiagnosticFactory<D : Diagnostic> protected constructor(
|
||||||
import org.jetbrains.annotations.Nullable;
|
open var name: String?,
|
||||||
import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer;
|
open val severity: Severity
|
||||||
|
) {
|
||||||
|
|
||||||
import java.util.Arrays;
|
open var defaultRenderer: DiagnosticRenderer<D>? = null
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
public abstract class DiagnosticFactory<D extends Diagnostic> {
|
protected constructor(severity: Severity) : this(null, severity)
|
||||||
|
|
||||||
private String name = null;
|
fun cast(diagnostic: Diagnostic): D {
|
||||||
private final Severity severity;
|
require(!(diagnostic.factory !== this)) { "Factory mismatch: expected " + this + " but was " + diagnostic.factory }
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
private DiagnosticRenderer<D> defaultRenderer;
|
return diagnostic as D
|
||||||
|
|
||||||
protected DiagnosticFactory(@NotNull Severity severity) {
|
|
||||||
this.severity = severity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DiagnosticFactory(@NotNull String name, @NotNull Severity severity) {
|
override fun toString(): String {
|
||||||
this.name = name;
|
return name ?: "<Anonymous DiagnosticFactory>"
|
||||||
this.severity = severity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ void setName(@NotNull String name) {
|
companion object {
|
||||||
this.name = name;
|
@SafeVarargs
|
||||||
}
|
fun <D : Diagnostic> cast(diagnostic: Diagnostic, vararg factories: DiagnosticFactory<out D>): D {
|
||||||
|
return cast(diagnostic, listOf(*factories))
|
||||||
@NotNull
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public Severity getSeverity() {
|
|
||||||
return severity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public DiagnosticRenderer<D> getDefaultRenderer() {
|
|
||||||
return defaultRenderer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setDefaultRenderer(@Nullable DiagnosticRenderer<D> defaultRenderer) {
|
|
||||||
this.defaultRenderer = defaultRenderer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public D cast(@NotNull Diagnostic diagnostic) {
|
|
||||||
if (diagnostic.getFactory() != this) {
|
|
||||||
throw new IllegalArgumentException("Factory mismatch: expected " + this + " but was " + diagnostic.getFactory());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (D) diagnostic;
|
fun <D : Diagnostic> cast(diagnostic: Diagnostic, factories: Collection<DiagnosticFactory<out D>>): D {
|
||||||
}
|
for (factory in factories) {
|
||||||
|
if (diagnostic.factory === factory) return factory.cast(diagnostic)
|
||||||
@NotNull
|
}
|
||||||
@SafeVarargs
|
throw IllegalArgumentException("Factory mismatch: expected one of " + factories + " but was " + diagnostic.factory)
|
||||||
public static <D extends Diagnostic> D cast(@NotNull Diagnostic diagnostic, @NotNull DiagnosticFactory<? extends D>... factories) {
|
|
||||||
return cast(diagnostic, Arrays.asList(factories));
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static <D extends Diagnostic> D cast(@NotNull Diagnostic diagnostic, @NotNull Collection<? extends DiagnosticFactory<? extends D>> factories) {
|
|
||||||
for (DiagnosticFactory<? extends D> factory : factories) {
|
|
||||||
if (diagnostic.getFactory() == factory) return factory.cast(diagnostic);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IllegalArgumentException("Factory mismatch: expected one of " + factories + " but was " + diagnostic.getFactory());
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return getName();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user