From ed08bca5c01152ced3f1b8cce26ca4dc5e17d388 Mon Sep 17 00:00:00 2001 From: Emmanuel M Date: Fri, 21 Dec 2018 18:47:00 -0300 Subject: [PATCH] FilePreloader.with(Class) method for calling from Java --- README.md | 4 ++-- .../amaze/filepreloaderlibrary/FilePreloader.kt | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a0b43aa..f6c1569 100644 --- a/README.md +++ b/README.md @@ -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 }); } diff --git a/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt b/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt index a1d9c03..9ca86c8 100644 --- a/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt +++ b/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt @@ -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 @@ -39,6 +40,20 @@ object FilePreloader { * * @see [with]. */ + fun with(clazz: Class): SpecializedPreloader { + //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 with(clazz: Class, f: FetcherFunction): SpecializedPreloader { val v = SpecializedPreloader(clazz, f) weakList.add(WeakReference(v))