Skip to content

Commit

Permalink
Made the Function API even more elegant
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander James Wallar committed Jan 21, 2014
1 parent 5c64739 commit edd4564
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
6 changes: 3 additions & 3 deletions app/Locabean/src/com/locaudio/api/Locaudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Locaudio(String ipAddress, int port) {
}

public AsyncGetRequest<SoundLocation[]> getSoundLocations(
final String soundName, final Function<SoundLocation[]> callback) {
final String soundName, final Function<SoundLocation[], Void> callback) {

AsyncGetRequest<SoundLocation[]> agr = new AsyncGetRequest<SoundLocation[]>(
SoundLocation[].class, LOCATIONS_ROUTE, soundName) {
Expand Down Expand Up @@ -45,7 +45,7 @@ public void runOnceReceivedResponse(SoundLocation[] response) {
return agr.getResponse(this);
}

public AsyncGetRequest<String[]> getNames(final Function<String[]> callback) {
public AsyncGetRequest<String[]> getNames(final Function<String[], Void> callback) {

AsyncGetRequest<String[]> agr = new AsyncGetRequest<String[]>(
String[].class, NAMES_ROUTE) {
Expand Down Expand Up @@ -76,7 +76,7 @@ public void runOnceReceivedResponse(String[] response) {
}

public AsyncPostRequest<NotifyResponse> notifyEvent(final NotifyForm event,
final Function<NotifyResponse> callback) {
final Function<NotifyResponse, Void> callback) {

AsyncPostRequest<NotifyResponse> apr = new AsyncPostRequest<NotifyResponse>(
NotifyResponse.class, event.toMap(), NOTIFY_ROUTE) {
Expand Down
18 changes: 4 additions & 14 deletions app/Locabean/src/com/locaudio/functional/Function.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
package com.locaudio.functional;

public abstract class Function<T> {
public abstract void body(T input);
public abstract class Function<T, R> {
public abstract R body(T input);

public void call(T input) {
body(input);
}
@SuppressWarnings("rawtypes")
public static Function getEmptyFunction() {
return new Function() {

@Override
public void body(Object input) {
}

};
public R call(T input) {
return body(input);
}
}
8 changes: 5 additions & 3 deletions app/Locabean/src/com/locaudio/functional/UIFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

import android.app.Activity;

public abstract class UIFunction<T> extends Function<T> {
public abstract class UIFunction<T, R> extends Function<T, R> {

private Activity activity;

public UIFunction(Activity activity) {
this.activity = activity;
}

public abstract void body(T input);
public abstract R body(T input);

public void call(final T input) {
public R call(final T input) {
this.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
body(input);
}
});

return null;
}

}
6 changes: 4 additions & 2 deletions app/Locabean/src/com/locaudio/locabean/NodeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,14 @@ public void onClick(View v) {
postForm.setY(0);

locaudio.notifyEvent(postForm,
new UIFunction<NotifyResponse>(self) {
new UIFunction<NotifyResponse, Void>(self) {

@Override
public void body(NotifyResponse nr) {
public Void body(NotifyResponse nr) {
nameTextView.setText(nr.name);
confidenceTextView.setText("" + nr.confidence);

return null;
}

});
Expand Down

0 comments on commit edd4564

Please sign in to comment.