Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from Wokdsem/1.3.0
Browse files Browse the repository at this point in the history
1.3.0
  • Loading branch information
Wokdsem authored Aug 7, 2017
2 parents ed89a36 + b1ab726 commit fd8785c
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 88 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## Kommander 1.3.0 (2017-08-07)
+ Deliverer instance is not global anymore, each Kommand can define its own one.

## Kommander 1.2.0 (2017-03-24)
+ Delay kommand execution
+ Remove pollSize instantiation
Expand Down
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@ to solve **Android UI Thread Issue**.
**Kommander** is designed to be really simple to use. First of all, you need to create a ```Kommander``` instance.

```java
Deliverer deliverer = getKommandDeliverer();
Kommander kommander = Kommander.getInstance(deliverer);
Kommander kommander = Kommander.getInstance();
```

Like code shows, you needs to provide a ```Deliverer``` instance to build a ```Kommander```. A ```Deliverer``` is just the way to define how and when
```Kommander``` will deliver the result of an asynchronous execution.
For instance, using ```Kommander``` on Android, you could use the next code to release the results on the Android's UI Thread.

```java
@Provides(singleton = true)
Kommander provideKommander() {
Handler handler = new Handler(Looper.getMainLooper());
return Kommander.getInstance(handler::post);
}
```
That was easy, right? Now, let's launch an asynchronous execution.

#### Kommands

Now, you have defined a ```Kommander``` instance, let's launch an asynchronous execution.

```java
kommander.makeKommand(() -> interactor.searchMovie("Titanic"))
.setOnCompleted(this::paintMovies)
Expand Down Expand Up @@ -69,7 +56,7 @@ This example is executing an asynchronous search of a movie and releases the res
}
```

In this step a ```Response.OnCompleted``` is being set up, that is who is going to handle the ```Action``` result. Also, in a similar way, is
In this step a ```Response.OnCompleted``` is being set up, that is who is going to handle the ```Action``` result. Also, in a similar way, is
possible setting up the ```Response.OnError``` when something is not going fine in the ```Action``` execution.

```java
Expand All @@ -91,14 +78,33 @@ This example is executing an asynchronous search of a movie and releases the res
A ```KommandToken``` is returned, you can use it to cancel the ```Kommand``` execution. A canceled ```Kommand```, will never be executed if the
execution has not started yet, try to stop the execution if that is running, or at least, the response is not delivered when the execution finish.

+ **Delay** execution
+ **Delay** You can define a delay time, in milliseconds, to kommand execution.

```java
long millisecondDelay = 60_000L;
kommand = kommand.delay(millisecondDelay);
```

+ A **Deliverer** is just a way to define how and when ```Kommander``` will deliver the result of an asynchronous execution.

```java
kommand = kommand.setDelivered(new Deliverer() {
@Override
public void deliver(Runnable runnable) {
// How do you want to deliver the response? Your code here!
}
});
```

You can define a delay time, in milliseconds, to kommand execution.
For instance, using ```Kommander``` on Android, you could use the next ```Deliverer``` to release the results on the Android's UI Thread.
```java
@Provides(singleton = true)
Deliverer provideAndroidDeliverer() {
Handler handler = new Handler(Looper.getMainLooper());
return handler::post;
}
```
#### KommandTokenBox
Expand Down Expand Up @@ -140,14 +146,14 @@ checking <a href="https://bintray.com/wokdsem/maven/kommander/view">here</a>.
<dependency>
<groupId>com.wokdsem.kommander</groupId>
<artifactId>kommander</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</dependency>
```
**Gradle:**
```groovy
compile 'com.wokdsem.kommander:kommander:1.2.0'
compile 'com.wokdsem.kommander:kommander:1.3.0'
```
## License
Expand Down
2 changes: 1 addition & 1 deletion kommander/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
apply plugin: 'java'
apply plugin: 'maven-publish'

def versionName = "1.2.0"
def versionName = "1.3.0"

sourceCompatibility = 1.7

Expand Down
19 changes: 19 additions & 0 deletions kommander/src/main/java/com/wokdsem/kommander/Deliverers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.wokdsem.kommander;

class Deliverers {

static Deliverer DEFAULT_DELIVERER;

static {
DEFAULT_DELIVERER = new Deliverer() {
@Override
public void deliver(Runnable runnable) {
runnable.run();
}
};
}

private Deliverers() {
}

}
6 changes: 2 additions & 4 deletions kommander/src/main/java/com/wokdsem/kommander/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

class Dispatcher {

private final Deliverer deliverer;
private final Executor executor;
private ScheduledExecutorService scheduledExecutor;

Dispatcher(Deliverer deliverer, Executor executor) {
this.deliverer = deliverer;
Dispatcher(Executor executor) {
this.executor = executor;
}

Expand All @@ -34,7 +32,7 @@ private synchronized ScheduledExecutorService getScheduledExecutor() {
}

private <T> KommandToken rawKommand(RunnableActionBundle<T> bundle) {
RunnableAction<T> runnableAction = new RunnableAction<>(bundle, deliverer);
RunnableAction<T> runnableAction = new RunnableAction<>(bundle);
executor.execute(runnableAction);
return KommandTokens.newWeakKommandToken(runnableAction);
}
Expand Down
16 changes: 14 additions & 2 deletions kommander/src/main/java/com/wokdsem/kommander/Kommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

/**
* A {@code Kommand} is an asynchronous context builder of an {@link Action} and the environment where the Action will
* be executed. {@code Kommand} makes easy setting up the response (successful or erroneous) handlers to the future
* be executed and delivered. {@code Kommand} makes easy setting up the response (successful or erroneous) handlers to the future
* calculation from the {@link Action} execution. When a Kommand is required to launch an asynchronous execution, takes
* the current context and prepares a request to launch to its asynchronous executor.
* the current context and prepares a request to launch to its asynchronous executor. If any {@code Deliverer} has not been provided,
* a default one (responses are released in the same executor thread) will be used.
* When a {@code Kommand} is launched can be canceled with a {@link KommandToken}, where: the {@code Kommand} is not
* executed if the computation has not started yet, trying to stop the computation when is running, or at least, the
* computation is not delivered when that is finished. You can lean on {@link KommandTokenBox} to get an advanced
Expand Down Expand Up @@ -43,6 +44,17 @@ public Kommand<T> setOnError(Response.OnError onError) {
return this;
}

/**
* Sets with a {@code Deliverer} instance how the responses should be delivered.
*
* @param deliverer used by kommander to deliver the responses
* @return
*/
public Kommand<T> setDelivered(Deliverer deliverer) {
bundleBuilder.deliverer(deliverer);
return this;
}

/**
* Sets the time to delay the kommand execution.
*
Expand Down
24 changes: 9 additions & 15 deletions kommander/src/main/java/com/wokdsem/kommander/Kommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,36 @@

/**
* {@code Kommander} is the entry point to the <b>Kommander</b> tool. You need to use a {@code Kommander} instance to build the key
* component, the {@link Kommand}. Built Kommands will be executed on the {@link Executor} and delivered on the {@link Deliverer}
* of {@code Kommander}.
* component, the {@link Kommand}.
*/
public class Kommander {

private final Dispatcher dispatcher;

private Kommander(Deliverer deliverer, Executor executor) {
this.dispatcher = new Dispatcher(deliverer, executor);
private Kommander(Executor executor) {
this.dispatcher = new Dispatcher(executor);
}

/**
* Builds a new Kommander instance with the default Kommander's {@link Executor} on the given {@link Deliverer}.
* Builds a new Kommander instance with the default Kommander's {@link Executor}.
*
* @return the {@code Kommander} instance
* @throws IllegalArgumentException when the given {@link Deliverer} is null
*/
public static Kommander getInstance(Deliverer deliverer) {
return getInstance(deliverer, KommandExecutor.newInstance());
public static Kommander getInstance() {
return getInstance(KommandExecutor.newInstance());
}

/**
* Builds a new Kommander instance with the given {@link Executor} and {@link Deliverer}.
* Builds a new Kommander instance with the given {@link Executor}.
*
* @return the {@code Kommander} instance
* @throws IllegalArgumentException when the given {@link Deliverer} is null
* @throws IllegalArgumentException when the given {@link Executor} is null
*/
public static Kommander getInstance(Deliverer deliverer, Executor executor) {
if (deliverer == null) {
throw new IllegalArgumentException("Illegal null deliverer to instantiate Kommander.");
}
public static Kommander getInstance(Executor executor) {
if (executor == null) {
throw new IllegalArgumentException("Illegal null executor to instantiate Kommander.");
}
return new Kommander(deliverer, executor);
return new Kommander(executor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class RunnableAction<T> implements Runnable, Cancelable {
private Throwable error;
private T result;

RunnableAction(RunnableActionBundle<T> bundle, Deliverer deliverer) {
RunnableAction(RunnableActionBundle<T> bundle) {
this.action = bundle.action;
this.deliverer = deliverer;
this.deliverer = bundle.deliverer;
this.onCompleted = bundle.onCompleted;
this.onError = bundle.onError;
this.state = RunnableState.NEW;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
package com.wokdsem.kommander;

class RunnableActionBundle<T> {
import static com.wokdsem.kommander.Deliverers.DEFAULT_DELIVERER;

class RunnableActionBundle<T> {

final Action<T> action;
final Response.OnCompleted<T> onCompleted;
final Response.OnError onError;

private RunnableActionBundle(Builder<T> builder) {
final Deliverer deliverer;

private RunnableActionBundle(Builder<T> builder, Deliverer deliverer) {
this.action = builder.action;
this.onCompleted = builder.onCompleted;
this.onError = builder.onError;
this.deliverer = deliverer;
}

static class Builder<T> {

private final Action<T> action;
private Response.OnCompleted<T> onCompleted;
private Response.OnError onError;

private Deliverer deliverer;

Builder(Action<T> action) {
this.action = action;
}


Builder<T> deliverer(Deliverer deliverer) {
this.deliverer = deliverer;
return this;
}

Builder<T> onCompleted(Response.OnCompleted<T> onCompleted) {
this.onCompleted = onCompleted;
return this;
}

Builder<T> onError(Response.OnError onError) {
this.onError = onError;
return this;
}

RunnableActionBundle<T> build() {
return new RunnableActionBundle<>(this);
Deliverer actionDeliverer = deliverer == null ? DEFAULT_DELIVERER : deliverer;
return new RunnableActionBundle<>(this, actionDeliverer);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Test;

import static com.wokdsem.kommander.toolbox.Deliverers.getDefaultDeliverer;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
Expand All @@ -16,7 +15,7 @@ public class DispatcherTest {
private final Dispatcher dispatcher;

public DispatcherTest() {
this.dispatcher = new Dispatcher(getDefaultDeliverer(), newSingleThreadExecutor());
this.dispatcher = new Dispatcher(newSingleThreadExecutor());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wokdsem.kommander;

import com.wokdsem.kommander.toolbox.Deliverers;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
Expand All @@ -13,7 +12,7 @@ public class KommandTest {
private final Dispatcher dispatcher;

public KommandTest() {
this.dispatcher = new Dispatcher(Deliverers.getDefaultDeliverer(), KommandExecutor.newInstance());
this.dispatcher = new Dispatcher(KommandExecutor.newInstance());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.wokdsem.kommander;

import com.wokdsem.kommander.toolbox.Deliverers;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

import static com.wokdsem.kommander.toolbox.Deliverers.getDefaultDeliverer;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
Expand All @@ -24,7 +22,7 @@ public void getInstance_nullDeliver_exceptionThrown() {
@Test
public void getInstance_nullExecutor_exceptionThrown() {
try {
Kommander.getInstance(Deliverers.getDefaultDeliverer(), null);
Kommander.getInstance(null);
fail();
} catch (Exception ignored) {
}
Expand All @@ -34,7 +32,7 @@ public void getInstance_nullExecutor_exceptionThrown() {
public void makeKommand_voidAction_actionExecuted() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
Kommander
.getInstance(getDefaultDeliverer())
.getInstance()
.makeKommand(new Action<Void>() {
@Override
public Void action() throws Throwable {
Expand Down
Loading

0 comments on commit fd8785c

Please sign in to comment.