Skip to content

Commit

Permalink
Convert DiagnosticFactory.java to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
mglukhikh committed Nov 26, 2020
1 parent 84f3a4b commit d47e163
Showing 1 changed file with 26 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,82 +13,42 @@
* See the License for the specific language governing permissions and
* 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;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer;
abstract class DiagnosticFactory<D : Diagnostic> protected constructor(
open var name: String?,
open val severity: Severity
) {

import java.util.Arrays;
import java.util.Collection;
open var defaultRenderer: DiagnosticRenderer<D>? = null

public abstract class DiagnosticFactory<D extends Diagnostic> {
protected constructor(severity: Severity) : this(null, severity)

private String name = null;
private final Severity severity;

private DiagnosticRenderer<D> defaultRenderer;

protected DiagnosticFactory(@NotNull Severity severity) {
this.severity = severity;
}

protected DiagnosticFactory(@NotNull String name, @NotNull Severity severity) {
this.name = name;
this.severity = severity;
}

/*package*/ void setName(@NotNull String name) {
this.name = name;
}

@NotNull
public String getName() {
return name;
}

@NotNull
public Severity getSeverity() {
return severity;
fun cast(diagnostic: Diagnostic): D {
require(!(diagnostic.factory !== this)) { "Factory mismatch: expected " + this + " but was " + diagnostic.factory }
@Suppress("UNCHECKED_CAST")
return diagnostic as D
}

@Nullable
public DiagnosticRenderer<D> getDefaultRenderer() {
return defaultRenderer;
override fun toString(): String {
return name ?: "<Anonymous DiagnosticFactory>"
}

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());
companion object {
@SafeVarargs
fun <D : Diagnostic> cast(diagnostic: Diagnostic, vararg factories: DiagnosticFactory<out D>): D {
return cast(diagnostic, listOf(*factories))
}

return (D) diagnostic;
}

@NotNull
@SafeVarargs
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);
fun <D : Diagnostic> cast(diagnostic: Diagnostic, factories: Collection<DiagnosticFactory<out D>>): 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)
}

throw new IllegalArgumentException("Factory mismatch: expected one of " + factories + " but was " + diagnostic.getFactory());
}

@Override
public String toString() {
return getName();
}
}
}

0 comments on commit d47e163

Please sign in to comment.