This file contains hidden or 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
//This code snippet assumes the JSON file content is an array of MODEL_CLASS objects. | |
var content = emptyArray<MODEL_CLASS>() | |
fun readJsonFile(context: Context) { | |
//get an input stream from assets folder for the file | |
val inputStream = context.assets.open("FILE_NAME.json") | |
//get input stream size | |
val size: Int = inputStream.available() | |
//create a byte array of corresponing size | |
val buffer = ByteArray(size) |
This file contains hidden or 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
@FragmentScope | |
@Component( | |
dependencies = [ApplicationComponent::class], | |
modules = [ExampleModule::class] | |
) | |
interface ExampleComponent { | |
fun inject(fragment: ExampleFragment) | |
} |
This file contains hidden or 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
fun interface Factory<T> { | |
fun create(): T | |
} | |
fun <T> createInstance(factory: Factory<T>): T { | |
return factory.create() | |
} | |
fun main() { | |
val instance = createInstance(Factory { |
This file contains hidden or 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
class SomeClass { | |
fun doTask() { | |
for (i in 1..10) { | |
println(i) | |
} | |
} | |
} | |
fun <T> createClassInstance(someClass: Class<T>): T{ | |
return someClass.getDeclaredConstructor().newInstance() |
This file contains hidden or 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
class LRUCache(private val capacity: Int) { | |
inner class Node( | |
val key: Int, | |
val value: Int, | |
var next: Node? = null, | |
var prev: Node? = null | |
) | |
private val cache = mutableMapOf<Int, Node>() |