Skip to content

Commit

Permalink
FilePreloader.with(Class<T>) method for calling from Java
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelMess committed Dec 21, 2018
1 parent 8e85bdc commit ed08bca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class FileMetadata(path: String): DataContainer(path) {
private static final FilePreloader FILE_PRELOADER = FilePreloader.INSTANCE;

public void preload(File externalDir) {
FILE_PRELOADER.with(FileMetadata.class, FileMetadata::new).preloadFrom(externalDir.getAbsolutePath());
FILE_PRELOADER.with(FileMetadata.class).preloadFrom(externalDir.getAbsolutePath());
}

public void load(File externalDir) {
FILE_PRELOADER.with(FileMetadata.class, FileMetadata::new).load(externalDir.getAbsolutePath(), (fileMetadatas) -> {
FILE_PRELOADER.with(FileMetadata.class).load(externalDir.getAbsolutePath(), (fileMetadatas) -> {
show(fileMetadatas); //Do something with the data
});
}
Expand Down
15 changes: 15 additions & 0 deletions lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.amaze.filepreloaderlibrary

import android.app.Activity
import com.amaze.filepreloaderlibrary.FilePreloader.with
import com.amaze.filepreloaderlibrary.datastructures.DataContainer
import com.amaze.filepreloaderlibrary.datastructures.FetcherFunction
import com.amaze.filepreloaderlibrary.utils.LIB_CONTEXT
Expand Down Expand Up @@ -39,6 +40,20 @@ object FilePreloader {
*
* @see [with].
*/
fun <D: DataContainer>with(clazz: Class<D>): SpecializedPreloader<D> {
//A constructor will obviously produce an instance of the type-parametrized type
// from which the constructor has been obtained
@Suppress("UNCHECKED_CAST")
return with(clazz, clazz.getConstructor(String::class.java)::newInstance as (String) -> D)
}

/**
* For compatibity with Java
*
* If you want to use another method, that's not the constructor, the type still has to be (String) -> D
*
* @see [with].
*/
fun <D: DataContainer>with(clazz: Class<D>, f: FetcherFunction<D>): SpecializedPreloader<D> {
val v = SpecializedPreloader(clazz, f)
weakList.add(WeakReference(v))
Expand Down

0 comments on commit ed08bca

Please sign in to comment.