Last active
July 13, 2019 13:22
-
-
Save AlonsoFloo/2d70b277c585f2bfc829aae76e922ec7 to your computer and use it in GitHub Desktop.
Asynchronous factory for convertir some models JAVA/Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.os.Handler; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by Florian ALONSO | |
*/ | |
public abstract class BaseFactory<ENTER, EXIT> { | |
public interface FactoryAsynchronous<BENTER, BEXIT> { | |
@Nullable | |
BEXIT run(@NonNull BENTER data); | |
void finish(@Nullable BEXIT data); | |
} | |
interface FactoryServiceCallBack<T> { | |
void asyncResult(@Nullable T value); | |
} | |
@NonNull | |
private Context context; | |
public BaseFactory(@NonNull Context context) { | |
this.context = context; | |
} | |
synchronized public <BENTER, BEXIT> void runAsynchronous(@NonNull final BENTER data, @NonNull final FactoryAsynchronous<BENTER, BEXIT> asynchronous) { | |
new Thread(() -> { | |
final BEXIT object = asynchronous.run(data); | |
Handler mainHandler = new Handler(context.getMainLooper()); | |
Runnable myRunnable = () -> { | |
if (asynchronous != null) { | |
asynchronous.finish(object); | |
} | |
}; | |
mainHandler.post(myRunnable); | |
}).start(); | |
} | |
@Nullable | |
abstract public EXIT convert(@NonNull ENTER enter); | |
@NonNull | |
public List<EXIT> convertList(@NonNull List<ENTER> enterList) { | |
List<EXIT> exitList = new ArrayList<>(); | |
for (ENTER enter : enterList) { | |
EXIT data = convert(enter); | |
if (data != null) { | |
exitList.add(data); | |
} | |
} | |
return exitList; | |
} | |
public void convertList(@NonNull List<ENTER> enterList, @NonNull final FactoryServiceCallBack<List<EXIT>> callBack) { | |
this.runAsynchronous(enterList, new FactoryAsynchronous<List<ENTER>, List<EXIT>>() { | |
@Override | |
public List<EXIT> run(@NonNull List<ENTER> data) { | |
return convertList(data); | |
} | |
@Override | |
public void finish(List<EXIT> data) { | |
callBack.asyncResult(data); | |
} | |
}); | |
} | |
public void convert(@NonNull ENTER enter, @NonNull final FactoryServiceCallBack<EXIT> callBack) { | |
this.runAsynchronous(enter, new FactoryAsynchronous<ENTER, EXIT>() { | |
@Override | |
public EXIT run(@NonNull ENTER data) { | |
return convert(data); | |
} | |
@Override | |
public void finish(EXIT data) { | |
callBack.asyncResult(data); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class BaseFactory<in ENTER, out EXIT: Any> { | |
abstract fun convert(enter: ENTER) : EXIT? | |
fun convertList(enterList: List<ENTER>) : List<EXIT> = | |
enterList.mapNotNull { | |
convert(it) | |
} | |
fun convertList(enterList: List<ENTER>, callback: (List<EXIT>) -> Unit) { | |
GlobalScope.launch(Dispatchers.Main) { | |
val result = async(Dispatchers.Default) { | |
convertList(enterList) | |
}.await() | |
callback(result) | |
} | |
} | |
fun convert(enter: ENTER, callback: (EXIT?) -> Unit) { | |
GlobalScope.launch(Dispatchers.Main) { | |
val result = async(Dispatchers.Default) { | |
convert(enter) | |
}.await() | |
callback(result) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Florian ALONSO | |
*/ | |
class TextFactory | |
constructor() : BaseFactory<String, Int>() { | |
override fun convert(enter: String): Int = | |
enter.toInt() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment