Skip to content

Commit

Permalink
GH-548 Change Application#launch result to Result<T, Throwable> and r…
Browse files Browse the repository at this point in the history
…esolve #548
  • Loading branch information
dzikoysk committed Aug 19, 2020
1 parent 2a8da3c commit 9d0c4db
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jetbrains.annotations.Nullable;
import org.panda_lang.language.interpreter.logging.Logger;
import org.panda_lang.language.interpreter.logging.LoggerHolder;
import org.panda_lang.utilities.commons.function.Result;

/**
* Application is a group of bundled scripts
Expand All @@ -28,7 +29,7 @@ public interface Application extends LoggerHolder {
/**
* Launch application with a specified arguments
*/
@Nullable Object launch(String... arguments);
<T> Result<@Nullable T, Throwable> launch(String... arguments);

/**
* Get application environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,54 +32,68 @@ private Result(@Nullable V value, @Nullable E error) {
this.error = error;
}

public <R> Result<R, E> map(Function<V, R> function) {
return value != null ? Result.ok(function.apply(value)) : Result.error(error);
public boolean isOk() {
return value != null;
}

public Result<V, E> orElse(Function<E, Result<V, E>> orElse) {
return isDefined() ? this : orElse.apply(error);
public boolean isErr() {
return error != null;
}

public V orElseGet(Function<E, V> orElse) {
return isDefined() ? value : orElse.apply(error);
public V get() {
if (isErr()) {
throw new NoSuchElementException("No value present");
}

return value;
}

public <T extends Exception> V orElseThrow(ThrowingFunction<E, T, T> consumer) throws T {
if (isDefined()) {
return getValue();
public E getError() {
if (isOk()) {
throw new NoSuchElementException("No error present");
}

throw consumer.apply(getError());
return error;
}

public void onError(Consumer<E> consumer) {
if (containsError()) {
consumer.accept(error);
}
public <R> Result<R, E> map(Function<V, R> function) {
return isOk() ? Result.ok(function.apply(value)) : Result.error(error);
}

public boolean isDefined() {
return value != null;
public <R> Result<R, E> flatMap(Function<V, Result<R, E>> function) {
return isOk() ? function.apply(value) : Result.error(error);
}

public V getValue() {
if (value == null) {
throw new NoSuchElementException("No value present");
public Result<V, E> orElse(Function<E, Result<V, E>> orElse) {
return isOk() ? this : orElse.apply(error);
}

public V orElseGet(Function<E, V> orElse) {
return isOk() ? value : orElse.apply(error);
}

public <T extends Exception> V orElseThrow(ThrowingFunction<E, T, T> consumer) throws T {
if (isOk()) {
return get();
}

return value;
throw consumer.apply(getError());
}

public boolean containsError() {
return error != null;
public Result<V, E> peek(Consumer<V> consumer) {
if (isOk()) {
consumer.accept(value);
}

return this;
}

public E getError() {
if (error == null) {
throw new NoSuchElementException("No error present");
public Result<V, E> onError(Consumer<E> consumer) {
if (isErr()) {
consumer.accept(error);
}

return error;
return this;
}

public static <V, E> Result<V, E> ok(V value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class IOUtilsTest {
@Test
void getURLContent() {
Result<String, IOException> result = IOUtils.fetchContent("https://panda-lang.org/");
assertTrue(result.isDefined());
assertNotNull(result.getValue());
assertTrue(result.getValue().contains("<html"));
assertTrue(result.isOk());
assertNotNull(result.get());
assertTrue(result.get().contains("<html"));
}

@Test
Expand All @@ -47,7 +47,7 @@ void convertStringToStream() throws IOException {

@Test
void convertStreamToString() {
assertEquals(MESSAGE, IOUtils.convertStreamToString(IOUtils.convertStringToStream(MESSAGE)).getValue());
assertEquals(MESSAGE, IOUtils.convertStreamToString(IOUtils.convertStringToStream(MESSAGE)).get());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class ResultTest {

@Test
void map() {
assertEquals(7, Result.ok("7").map(Integer::parseInt).getValue());
assertEquals(7, Result.ok("7").map(Integer::parseInt).get());
}

@Test
void orElse() {
assertEquals(7, Result.error(-1).orElse(err -> Result.ok(7)).getValue());
assertEquals(7, Result.error(-1).orElse(err -> Result.ok(7)).get());
}

@Test
Expand All @@ -48,19 +48,19 @@ void onError() {

@Test
void isDefined() {
assertTrue(Result.ok("ok").isDefined());
assertFalse(Result.error("err").isDefined());
assertTrue(Result.ok("ok").isOk());
assertFalse(Result.error("err").isOk());
}

@Test
void getValue() {
assertEquals("value", Result.ok("value").getValue());
assertEquals("value", Result.ok("value").get());
}

@Test
void containsError() {
assertTrue(Result.error("err").containsError());
assertFalse(Result.ok("ok").containsError());
assertTrue(Result.error("err").isErr());
assertFalse(Result.ok("ok").isErr());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.panda_lang.language.architecture.Application;
import org.panda_lang.language.architecture.Environment;
import org.panda_lang.language.architecture.Script;
import org.panda_lang.language.runtime.Process;
import org.panda_lang.language.runtime.PandaProcess;
import org.panda_lang.language.runtime.Process;
import org.panda_lang.panda.language.resource.syntax.head.MainScope;
import org.panda_lang.utilities.commons.UnsafeUtils;
import org.panda_lang.utilities.commons.function.Result;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -40,14 +40,14 @@ public PandaApplication(Environment environment) {
}

@Override
public @Nullable Object launch(String... args) {
public <T> Result<@Nullable T, Throwable> launch(String... args) {
Process process = new PandaProcess(this, selectMain(), args);

try {
return process.execute();
return Result.ok(process.execute());
} catch (Throwable throwable) {
environment.getLogger().exception(throwable);
return UnsafeUtils.throwException(throwable);
return Result.error(throwable);
}
}

Expand Down
3 changes: 2 additions & 1 deletion panda/src/main/java/org/panda_lang/panda/manager/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ protected Object run(FrameworkController controller) throws IOException {
return environment.getInterpreter()
.interpret(PandaURLSource.fromFile(mainScript))
.orElseThrow(throwable -> new RuntimeException("Cannot launch application due to failures in interpretation process"))
.launch();
.launch()
.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void run() throws Exception {

panda.getLoader()
.load(script, script.getParentFile())
.map(Application::launch)
.flatMap(Application::launch)
.onError(throwable -> shell.getLogger().fatal("Cannot launch application due to failures in interpretation process"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.RunnerException;
import org.panda_lang.language.architecture.Application;
import org.panda_lang.panda.util.BenchmarkUtils;
import org.panda_lang.panda.util.PandaUtils;

Expand Down Expand Up @@ -51,7 +52,9 @@ public class CurrentTestBenchmark {

@Benchmark
public Object currentTest() {
return PandaUtils.load("./examples/tests", "./examples/tests/current_test.panda").getValue().launch();
return PandaUtils.load("./examples/tests", "./examples/tests/current_test.panda")
.flatMap(Application::launch)
.get();
}

public static void main(String[] args) throws RunnerException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class TenThousandExpressionsTest {

@Test
void launch() {
PandaUtils.load("../examples/tests/performance", "../examples/tests/performance/ten_thousand_expressions.panda").getValue().launch();
PandaUtils.load("../examples/tests/performance", "../examples/tests/performance/ten_thousand_expressions.panda").get().launch();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public class MatmulPerformanceTest {

@Setup
public void setup() {
this.matmulApplication = PandaUtils.load("./tests/performance", "./tests/performance/matmul.panda").getValue();
this.matmulApplication = PandaUtils.load("./tests/performance", "./tests/performance/matmul.panda").get();
}

@Benchmark
public void benchmarkMatmulPanda() {
matmulApplication.launch();
matmulApplication.launch().get();
}

@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.panda_lang.panda.examples.performance.matmul;

import org.junit.jupiter.api.Test;
import org.panda_lang.language.architecture.Application;
import org.panda_lang.panda.util.PandaUtils;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -26,7 +27,7 @@ class MatmulTest {
@Test
void testMatmul() {
for (int i = 0; i < 1; i++) {
assertEquals(-9.3358333, PandaUtils.load("../examples/tests/performance", "../examples/tests/performance/matmul.panda").getValue().launch());
assertEquals(-9.3358333, PandaUtils.load("../examples/tests/performance", "../examples/tests/performance/matmul.panda").flatMap(Application::launch).get());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,39 @@
package org.panda_lang.panda.examples.tests;

import org.junit.jupiter.api.Test;
import org.panda_lang.language.architecture.Application;
import org.panda_lang.panda.util.PandaUtils;

class CurrentTestExampleTest {

@Test
void helloWorld() {
PandaUtils.load("../examples", "../examples/hello_world.panda").getValue().launch();
PandaUtils.load("../examples", "../examples/hello_world.panda")
.flatMap(Application::launch)
.get();
}

@Test
void literalMethod() {
PandaUtils.load("../examples/tests", "../examples/tests/literal_methods.panda").getValue().launch();
PandaUtils.load("../examples/tests", "../examples/tests/literal_methods.panda")
.flatMap(Application::launch)
.get();
}

@Test
void testCurrentTest() {
for (int i = 0; i < 1; i++) {
PandaUtils.load("../examples/tests", "../examples/tests/current_test.panda").getValue().launch();
PandaUtils.load("../examples/tests", "../examples/tests/current_test.panda")
.flatMap(Application::launch)
.get();
}
}

@Test
void testClassTest() {
PandaUtils.load("../examples/tests", "../examples/tests/class_test.panda").getValue().launch();
PandaUtils.load("../examples/tests", "../examples/tests/class_test.panda")
.flatMap(Application::launch)
.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
final class SwingExample {

public static void main(String[] args) {
PandaUtils.load("", "./examples/swing.panda").getValue().launch();
PandaUtils.load("", "./examples/swing.panda").get().launch();
}

}

0 comments on commit 9d0c4db

Please sign in to comment.