From d895deb0e3584ccbacca403130d3c239e3ad4f43 Mon Sep 17 00:00:00 2001 From: ramabit Date: Thu, 21 Jan 2016 12:01:50 -0300 Subject: [PATCH 1/3] #8 improving syntax --- .../src/main/java/com/inaka/killertask/Example.kt | 13 ++++--------- .../main/java/com/inaka/killertask/KillerTask.kt | 4 ++-- .../src/main/java/com/inaka/killertask/WhenDone.kt | 9 ++------- .../killertask/KillerMainTest_KotlinVersion.kt | 14 ++++---------- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/library/src/main/java/com/inaka/killertask/Example.kt b/library/src/main/java/com/inaka/killertask/Example.kt index fffeb9d..ab5c48e 100644 --- a/library/src/main/java/com/inaka/killertask/Example.kt +++ b/library/src/main/java/com/inaka/killertask/Example.kt @@ -15,20 +15,15 @@ private class Example { signal.countDown() } - val onFailed: (Exception) -> Unit = { - e: Exception -> + val onFailed: (Exception?) -> Unit = { + e: Exception? -> Log.wtf("result", e.toString()) - e.printStackTrace() + e?.printStackTrace() signal.countDown() } init { - KillerTask( - doWork(), - WhenDone(mapOf( - WhenDone.success to onSuccess, - WhenDone.failed to onFailed - ))).go() + KillerTask(doWork(), WhenDone(onSuccess, onFailed)).go() signal.await() } diff --git a/library/src/main/java/com/inaka/killertask/KillerTask.kt b/library/src/main/java/com/inaka/killertask/KillerTask.kt index 18dd6dc..327526e 100644 --- a/library/src/main/java/com/inaka/killertask/KillerTask.kt +++ b/library/src/main/java/com/inaka/killertask/KillerTask.kt @@ -3,7 +3,7 @@ package com.inaka.killertask import android.os.AsyncTask import android.util.Log -class KillerTask(val action: T, val callback: WhenDone) : AsyncTask() { +class KillerTask(val action: T, val callback: WhenDone) : AsyncTask() { private var exception: Exception? = null @@ -30,7 +30,7 @@ class KillerTask(val action: T, val callback: WhenDone) : AsyncTask(val map: Map) { +class WhenDone(val success: (T) -> Any, val failure: (Exception?) -> Any) { + - val success: (T) -> T by map - val failure: (Exception?) -> T by map - companion object { - public val success = "success" - public val failed = "failure" - } } diff --git a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt index 2f37f2a..bca1c57 100644 --- a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt +++ b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt @@ -9,13 +9,7 @@ import org.junit.Test class KillerMainTest_KotlinVersion { @Test fun createKillerTask() { - - KillerTask( - doWork(), - WhenDone(mapOf( - WhenDone.success to onSuccess, - WhenDone.failed to onFailed - ))).go() + KillerTask(doWork(), WhenDone(onSuccess, onFailed)).go() } fun doWork(): String { @@ -27,9 +21,9 @@ class KillerMainTest_KotlinVersion { assert(result.equals("test")) } - val onFailed: (Exception) -> Unit = { - e: Exception -> - e.printStackTrace() + val onFailed: (Exception?) -> Unit = { + e: Exception? -> + e?.printStackTrace() } } \ No newline at end of file From ce5bc71edb2035e5fcb501e3a9f83275badf8175 Mon Sep 17 00:00:00 2001 From: ramabit Date: Thu, 21 Jan 2016 12:12:58 -0300 Subject: [PATCH 2/3] #8 delete WhenDone class and simplify syntax --- library/src/main/java/com/inaka/killertask/Example.kt | 2 +- library/src/main/java/com/inaka/killertask/KillerTask.kt | 8 ++++---- library/src/main/java/com/inaka/killertask/WhenDone.kt | 8 -------- .../com/inaka/killertask/KillerMainTest_KotlinVersion.kt | 2 +- 4 files changed, 6 insertions(+), 14 deletions(-) delete mode 100644 library/src/main/java/com/inaka/killertask/WhenDone.kt diff --git a/library/src/main/java/com/inaka/killertask/Example.kt b/library/src/main/java/com/inaka/killertask/Example.kt index ab5c48e..5b8736d 100644 --- a/library/src/main/java/com/inaka/killertask/Example.kt +++ b/library/src/main/java/com/inaka/killertask/Example.kt @@ -23,7 +23,7 @@ private class Example { } init { - KillerTask(doWork(), WhenDone(onSuccess, onFailed)).go() + KillerTask(doWork(), onSuccess, onFailed).go() signal.await() } diff --git a/library/src/main/java/com/inaka/killertask/KillerTask.kt b/library/src/main/java/com/inaka/killertask/KillerTask.kt index 327526e..c94a78a 100644 --- a/library/src/main/java/com/inaka/killertask/KillerTask.kt +++ b/library/src/main/java/com/inaka/killertask/KillerTask.kt @@ -3,7 +3,7 @@ package com.inaka.killertask import android.os.AsyncTask import android.util.Log -class KillerTask(val action: T, val callback: WhenDone) : AsyncTask() { +class KillerTask(val action: T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask() { private var exception: Exception? = null @@ -27,14 +27,14 @@ class KillerTask(val action: T, val callback: WhenDone) : AsyncTask(val success: (T) -> Any, val failure: (Exception?) -> Any) { - - - - -} diff --git a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt index bca1c57..728f421 100644 --- a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt +++ b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt @@ -9,7 +9,7 @@ import org.junit.Test class KillerMainTest_KotlinVersion { @Test fun createKillerTask() { - KillerTask(doWork(), WhenDone(onSuccess, onFailed)).go() + KillerTask(doWork(), onSuccess, onFailed).go() } fun doWork(): String { From 6bd5e38f461d8d92c2baf726e1e8f01146978944 Mon Sep 17 00:00:00 2001 From: ramabit Date: Thu, 21 Jan 2016 14:09:17 -0300 Subject: [PATCH 3/3] #8 #2 add example of blocks direct usage having another example with functions refactor --- ...ainTest.java => KillerTaskBlocksTest.java} | 7 +++-- .../killertask/KillerTaskFunctionsTest.java | 17 +++++++++++ .../inaka/killertask/ExampleAllTogether.kt | 28 +++++++++++++++++++ ...Example.kt => ExampleFunctionsRefactor.kt} | 5 ++-- .../KillerMainTest_KotlinVersion.kt | 15 +++++++++- 5 files changed, 65 insertions(+), 7 deletions(-) rename library/src/androidTest/java/com/inaka/killertask/{KillerMainTest.java => KillerTaskBlocksTest.java} (55%) create mode 100644 library/src/androidTest/java/com/inaka/killertask/KillerTaskFunctionsTest.java create mode 100644 library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt rename library/src/main/java/com/inaka/killertask/{Example.kt => ExampleFunctionsRefactor.kt} (89%) diff --git a/library/src/androidTest/java/com/inaka/killertask/KillerMainTest.java b/library/src/androidTest/java/com/inaka/killertask/KillerTaskBlocksTest.java similarity index 55% rename from library/src/androidTest/java/com/inaka/killertask/KillerMainTest.java rename to library/src/androidTest/java/com/inaka/killertask/KillerTaskBlocksTest.java index 1c8203b..5bcc3c2 100644 --- a/library/src/androidTest/java/com/inaka/killertask/KillerMainTest.java +++ b/library/src/androidTest/java/com/inaka/killertask/KillerTaskBlocksTest.java @@ -7,10 +7,11 @@ /** * Created by inaka on 1/20/16. */ -public class KillerMainTest extends AndroidTestCase { +public class KillerTaskBlocksTest extends AndroidTestCase { @UiThreadTest - public void testLibrary(){ - new Example(); + public void testLibraryWithBlocks() { + new ExampleAllTogether(); } + } diff --git a/library/src/androidTest/java/com/inaka/killertask/KillerTaskFunctionsTest.java b/library/src/androidTest/java/com/inaka/killertask/KillerTaskFunctionsTest.java new file mode 100644 index 0000000..d0d15f5 --- /dev/null +++ b/library/src/androidTest/java/com/inaka/killertask/KillerTaskFunctionsTest.java @@ -0,0 +1,17 @@ +package com.inaka.killertask; + +import android.test.AndroidTestCase; +import android.test.UiThreadTest; + + +/** + * Created by inaka on 1/21/16. + */ +public class KillerTaskFunctionsTest extends AndroidTestCase { + + @UiThreadTest + public void testLibraryFunctionsRefactor() { + new ExampleFunctionsRefactor(); + } + +} diff --git a/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt b/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt new file mode 100644 index 0000000..0fb517b --- /dev/null +++ b/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt @@ -0,0 +1,28 @@ +package com.inaka.killertask + +import android.util.Log +import java.util.concurrent.CountDownLatch + +/** + * Created by inaka on 1/20/16. + */ +private class ExampleAllTogether { + val signal = CountDownLatch(1); + + init { + KillerTask( + "test", { + result: String -> + Log.wtf("result", result) + signal.countDown() + }, { + e: Exception? -> + Log.wtf("result", e.toString()) + e?.printStackTrace() + signal.countDown() + }).go() + + signal.await() + } + +} \ No newline at end of file diff --git a/library/src/main/java/com/inaka/killertask/Example.kt b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt similarity index 89% rename from library/src/main/java/com/inaka/killertask/Example.kt rename to library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt index 5b8736d..7f63a27 100644 --- a/library/src/main/java/com/inaka/killertask/Example.kt +++ b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt @@ -4,9 +4,9 @@ import android.util.Log import java.util.concurrent.CountDownLatch /** - * Created by inaka on 1/20/16. + * Created by inaka on 1/21/16. */ -private class Example { +private class ExampleFunctionsRefactor { val signal = CountDownLatch(1); val onSuccess: (String) -> Unit = { @@ -30,5 +30,4 @@ private class Example { fun doWork(): String { return "test" } - } \ No newline at end of file diff --git a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt index 728f421..61c1d4e 100644 --- a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt +++ b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt @@ -1,6 +1,5 @@ package com.inaka.killertask -import android.util.Log import org.junit.Test /** @@ -10,6 +9,19 @@ class KillerMainTest_KotlinVersion { @Test fun createKillerTask() { KillerTask(doWork(), onSuccess, onFailed).go() + + /* + That is the same as: + + KillerTask("test", { + result: String -> + assert(result.equals("test")) + }, { + e: Exception? -> + e?.printStackTrace() + print(e?.message) + }).go() + */ } fun doWork(): String { @@ -24,6 +36,7 @@ class KillerMainTest_KotlinVersion { val onFailed: (Exception?) -> Unit = { e: Exception? -> e?.printStackTrace() + print(e?.message) } } \ No newline at end of file