Convert error classes to kotlin

This commit is contained in:
Mike Cifelli 2018-03-18 12:45:57 -04:00
parent 5f0cb1aa86
commit 0fb64f0a2d
26 changed files with 134 additions and 201 deletions

View File

@ -26,6 +26,7 @@
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" /> <option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" /> <option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="IMPORT_NESTED_CLASSES" value="true" /> <option name="IMPORT_NESTED_CLASSES" value="true" />
<option name="CONTINUATION_INDENT_FOR_EXPRESSION_BODIES" value="false" />
<option name="CONTINUATION_INDENT_FOR_CHAINED_CALLS" value="false" /> <option name="CONTINUATION_INDENT_FOR_CHAINED_CALLS" value="false" />
<option name="WRAP_EXPRESSION_BODY_FUNCTIONS" value="1" /> <option name="WRAP_EXPRESSION_BODY_FUNCTIONS" value="1" />
</JetCodeStyleSettings> </JetCodeStyleSettings>

View File

@ -1,20 +0,0 @@
package error;
import error.ErrorManager.Severity;
import file.FilePosition;
import static error.ErrorManager.Severity.CRITICAL;
public abstract class CriticalLineColumnException extends LineColumnException {
private static final long serialVersionUID = 1L;
public CriticalLineColumnException(FilePosition position) {
super(position);
}
@Override
public Severity getSeverity() {
return CRITICAL;
}
}

View File

@ -0,0 +1,9 @@
package error
import error.Severity.CRITICAL
import file.FilePosition
abstract class CriticalLineColumnException(position: FilePosition) : LineColumnException(position) {
override val severity = CRITICAL
}

View File

@ -1,15 +0,0 @@
package error;
import error.ErrorManager.Severity;
import static error.ErrorManager.Severity.CRITICAL;
public abstract class CriticalLispException extends LispException {
private static final long serialVersionUID = 1L;
@Override
public Severity getSeverity() {
return CRITICAL;
}
}

View File

@ -0,0 +1,8 @@
package error
import error.Severity.CRITICAL
abstract class CriticalLispException : LispException() {
override val severity = CRITICAL
}

View File

@ -1,95 +0,0 @@
package error;
import environment.RuntimeEnvironment;
import java.io.PrintStream;
import static error.ErrorManager.Severity.CRITICAL;
import static error.ErrorManager.Severity.WARNING;
import static java.text.MessageFormat.format;
/**
* Prints error messages and potentially terminates the application.
*/
public class ErrorManager {
private RuntimeEnvironment environment;
public ErrorManager() {
this.environment = RuntimeEnvironment.getInstance();
}
public void handle(LispException lispException) {
printMessage(lispException);
if (isCritical(lispException))
environment.terminateExceptionally();
}
private void printMessage(LispException lispException) {
PrintStream output = selectOutputStream(lispException.getSeverity());
output.println(formatMessage(lispException));
}
private PrintStream selectOutputStream(Severity severity) {
if (severity == WARNING)
return environment.getOutput();
return environment.getErrorOutput();
}
private String formatMessage(LispException lispException) {
Severity severity = lispException.getSeverity();
String prefix = severity.toDisplayString();
String message = format("[{0}] {1}", prefix, lispException.getMessage());
return severity.decorate(message, environment);
}
private boolean isCritical(LispException lispException) {
return lispException.getSeverity() == CRITICAL;
}
public static enum Severity {
WARNING {
@Override
public String decorate(String warningOutput, RuntimeEnvironment environment) {
return environment.decorateWarningOutput(warningOutput);
}
@Override
public String toDisplayString() {
return "warning";
}
},
ERROR {
@Override
public String decorate(String errorOutput, RuntimeEnvironment environment) {
return environment.decorateErrorOutput(errorOutput);
}
@Override
public String toDisplayString() {
return "error";
}
},
CRITICAL {
@Override
public String decorate(String criticalOutput, RuntimeEnvironment environment) {
return environment.decorateCriticalOutput(criticalOutput);
}
@Override
public String toDisplayString() {
return "critical";
}
};
public abstract String decorate(String output, RuntimeEnvironment environment);
public abstract String toDisplayString();
}
}

View File

@ -0,0 +1,41 @@
package error
import environment.RuntimeEnvironment
import error.Severity.CRITICAL
import error.Severity.WARNING
import java.io.PrintStream
import java.text.MessageFormat.format
/**
* Prints error messages and potentially terminates the application.
*/
class ErrorManager {
private val environment: RuntimeEnvironment = RuntimeEnvironment.getInstance()
fun handle(lispException: LispException) {
printMessage(lispException)
if (isCritical(lispException))
environment.terminateExceptionally()
}
private fun printMessage(lispException: LispException) {
val output = selectOutputStream(lispException.severity)
output.println(formatMessage(lispException))
}
private fun selectOutputStream(severity: Severity): PrintStream {
return if (severity === WARNING) environment.output else environment.errorOutput
}
private fun formatMessage(lispException: LispException): String {
val severity = lispException.severity
val prefix = severity.toDisplayString()
val message = format("[{0}] {1}", prefix, lispException.message)
return severity.decorate(message, environment)
}
private fun isCritical(lispException: LispException) = lispException.severity == CRITICAL
}

View File

@ -1,25 +0,0 @@
package error;
import file.FilePosition;
import static java.text.MessageFormat.format;
public abstract class LineColumnException extends LispException {
private static final long serialVersionUID = 1L;
private FilePosition position;
public LineColumnException(FilePosition position) {
this.position = position;
}
@Override
public String getMessage() {
return format("{0} - line {1}, column {2}",
getMessagePrefix(),
position.getLineNumber(),
position.getColumnNumber());
}
public abstract String getMessagePrefix();
}

View File

@ -0,0 +1,12 @@
package error
import file.FilePosition
import java.text.MessageFormat.format
abstract class LineColumnException(private val position: FilePosition) : LispException() {
abstract val messagePrefix: String
override val message: String
get() = format("{0} - line {1}, column {2}", messagePrefix, position.lineNumber, position.columnNumber)
}

View File

@ -1,14 +0,0 @@
package error;
import error.ErrorManager.Severity;
import static error.ErrorManager.Severity.ERROR;
public abstract class LispException extends RuntimeException {
private static final long serialVersionUID = 1L;
public Severity getSeverity() {
return ERROR;
}
}

View File

@ -0,0 +1,8 @@
package error
import error.Severity.ERROR
abstract class LispException : RuntimeException() {
open val severity = ERROR
}

View File

@ -1,15 +0,0 @@
package error;
import error.ErrorManager.Severity;
import static error.ErrorManager.Severity.WARNING;
public abstract class LispWarning extends LispException {
private static final long serialVersionUID = 1L;
@Override
public Severity getSeverity() {
return WARNING;
}
}

View File

@ -0,0 +1,8 @@
package error
import error.Severity.WARNING
abstract class LispWarning : LispException() {
override val severity = WARNING
}

View File

@ -0,0 +1,31 @@
package error
import environment.RuntimeEnvironment
enum class Severity {
WARNING {
override fun decorate(output: String, environment: RuntimeEnvironment): String =
environment.decorateWarningOutput(output)
override fun toDisplayString() = "warning"
},
ERROR {
override fun decorate(output: String, environment: RuntimeEnvironment): String =
environment.decorateErrorOutput(output)
override fun toDisplayString() = "error"
},
CRITICAL {
override fun decorate(output: String, environment: RuntimeEnvironment): String =
environment.decorateCriticalOutput(output)
override fun toDisplayString() = "critical"
};
abstract fun decorate(output: String, environment: RuntimeEnvironment): String
abstract fun toDisplayString(): String
}

View File

@ -1,7 +1,6 @@
package error; package error;
import environment.RuntimeEnvironment; import environment.RuntimeEnvironment;
import error.ErrorManager.Severity;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -11,9 +10,9 @@ import java.io.PrintStream;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import static error.ErrorManager.Severity.CRITICAL; import static error.Severity.CRITICAL;
import static error.ErrorManager.Severity.ERROR; import static error.Severity.ERROR;
import static error.ErrorManager.Severity.WARNING; import static error.Severity.WARNING;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

View File

@ -12,7 +12,7 @@ import sexpression.Nil;
import sexpression.SExpression; import sexpression.SExpression;
import sexpression.Symbol; import sexpression.Symbol;
import static error.ErrorManager.Severity.ERROR; import static error.Severity.ERROR;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

View File

@ -1,6 +1,6 @@
package function; package function;
import error.ErrorManager; import error.Severity;
import function.ArgumentValidator.TooFewArgumentsException; import function.ArgumentValidator.TooFewArgumentsException;
import function.ArgumentValidator.TooManyArgumentsException; import function.ArgumentValidator.TooManyArgumentsException;
import function.UserDefinedFunction.IllegalKeywordRestPositionException; import function.UserDefinedFunction.IllegalKeywordRestPositionException;
@ -74,6 +74,6 @@ public class UserDefinedFunctionTest {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0); assertTrue(e.getMessage().length() > 0);
assertEquals(ErrorManager.Severity.ERROR, e.getSeverity()); assertEquals(Severity.ERROR, e.getSeverity());
} }
} }

View File

@ -7,7 +7,7 @@ import function.builtin.SYMBOL_FUNCTION.UndefinedSymbolFunctionException;
import org.junit.Test; import org.junit.Test;
import testutil.SymbolAndFunctionCleaner; import testutil.SymbolAndFunctionCleaner;
import static error.ErrorManager.Severity.ERROR; import static error.Severity.ERROR;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

View File

@ -11,7 +11,7 @@ import java.io.PrintStream;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import static error.ErrorManager.Severity.CRITICAL; import static error.Severity.CRITICAL;
import static interpreter.InteractiveLispInterpreter.PROMPT; import static interpreter.InteractiveLispInterpreter.PROMPT;
import static java.text.MessageFormat.format; import static java.text.MessageFormat.format;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;

View File

@ -10,7 +10,7 @@ import token.TokenFactory.BadCharacterException;
import java.io.InputStream; import java.io.InputStream;
import static error.ErrorManager.Severity.ERROR; import static error.Severity.ERROR;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;

View File

@ -6,7 +6,7 @@ import stream.UncheckedIOException;
import java.io.InputStream; import java.io.InputStream;
import static error.ErrorManager.Severity.CRITICAL; import static error.Severity.CRITICAL;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

View File

@ -20,7 +20,7 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static error.ErrorManager.Severity.ERROR; import static error.Severity.ERROR;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

View File

@ -7,7 +7,7 @@ import sexpression.LispNumber.InvalidNumberException;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Locale; import java.util.Locale;
import static error.ErrorManager.Severity.ERROR; import static error.Severity.ERROR;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

View File

@ -1,6 +1,6 @@
package table; package table;
import error.ErrorManager; import error.Severity;
import function.FunctionNames; import function.FunctionNames;
import function.LispFunction; import function.LispFunction;
import org.junit.After; import org.junit.After;
@ -146,7 +146,7 @@ public class FunctionTableTest {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0); assertTrue(e.getMessage().length() > 0);
assertEquals(ErrorManager.Severity.CRITICAL, e.getSeverity()); assertEquals(Severity.CRITICAL, e.getSeverity());
} }
@Test @Test

View File

@ -11,7 +11,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Arrays; import java.util.Arrays;
import static error.ErrorManager.Severity.ERROR; import static error.Severity.ERROR;
import static function.builtin.EVAL.eval; import static function.builtin.EVAL.eval;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.Matchers.isEmptyOrNullString;

View File

@ -6,7 +6,7 @@ import org.junit.Test;
import token.TokenFactory.BadCharacterException; import token.TokenFactory.BadCharacterException;
import token.TokenFactory.EmptyTokenTextException; import token.TokenFactory.EmptyTokenTextException;
import static error.ErrorManager.Severity.CRITICAL; import static error.Severity.CRITICAL;
import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;