Skip to content

Instantly share code, notes, and snippets.

View rommansabbir's full-sized avatar
👓
Only Development

Romman Sabbir rommansabbir

👓
Only Development
View GitHub Profile
@rommansabbir
rommansabbir / BinarySearch.kt
Last active June 1, 2022 06:41
BinarySearch
class BinarySearch {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println("Index position: " + binarySearch(mutableListOf(1,15,34,45,65,76,87), 76))
}
private fun binarySearch(list: List<Int>, itemToFind: Int): Int {
//Starting point
var left = 0
@rommansabbir
rommansabbir / *ToHashMap.kt
Created May 28, 2022 07:20
Set,List to HashMap (Kotlin)
/*Transform a *Set* into a *HashMap**/
suspend fun <T> Set<T>.toHashMap(): HashMap<Int, T> = withContext(Dispatchers.IO) {
val hashMap: HashMap<Int, T> = HashMap()
var index = 0
[email protected] {
if (hashMap.isEmpty()) hashMap[0] = it else index = hashMap.size; hashMap[index++] = it
}
hashMap
}
{
"apps": [
{
"serverURL": "http://192.168.17.131:1337/parse",
"appId": "test_app_id",
"masterKey": "test_master_key",
"allowInsecureHTTP": "true",
"appName": "MyApp1"
}
],
{
"appName": "ParseServer",
"databaseURI": "mongodb://localhost:27017/parsedb",
"appId": "test_app_id",
"masterKey": "test_master_key",
"serverURL": "https://localhost:1337/parse",
"publicServerURL": "https://0.0.0.0:1337/parse",
"port": 1337
}
@rommansabbir
rommansabbir / Failure.kt
Created March 22, 2022 04:02
Sealed Class that return HTTP Failure (4xx, 5xx)
sealed class Failure {
/*Network Error*/
class NetworkConnection(var additionalData: Any? = null) : Failure()
/*Exception*/
class Exception(var additionalData: Any? = null) : Failure()
/**
* Although the HTTP standard specifies "unauthorized",
* semantically this response means "unauthenticated".
@rommansabbir
rommansabbir / Hilt.kt
Last active November 10, 2023 21:08
Android - Hilt: Inject multiple instances of SameType Object to the Dependent Module
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import javax.inject.Named
@rommansabbir
rommansabbir / IteratorPatternExample.kt
Created March 12, 2022 13:16
Iterator Pattern Example
class IteratorPatternExample {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val holder = VerifyTickets.getTicketHolder()
/*Printing several tickets*/
println("Printing next tickets...")
holder.ticketIterator.nextTicket()
holder.ticketIterator.nextTicket()
@rommansabbir
rommansabbir / MultithreadedDataHolder.kt
Created March 7, 2022 07:44
Volatile - Marks the JVM backing field of the annotated property as volatile, meaning that writes to this field are immediately made visible to other threads.
/**
* We have a data holder class called [MultithreadedDataHolder] that hold some data
* which will be used by different clients from different [Thread]s. Multiple clients
* can access the same object simultaneously.
*
* Any object that is annotated with Annotation [Volatile] make sure that the changes
* made in one thread are immediately reflect in other thread.
*/
object MultithreadedDataHolder {
/*It's guaranteed that all reader threads will see the updated value of the
@rommansabbir
rommansabbir / CommandPatternExample.kt
Created March 6, 2022 12:38
Command Design Pattern
class CommandPatternExample {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val remoteControl: BaseRemoteControl = RemoteControl()
println("-----Testing onButtonPressed on RemoteControl for Car-----")
val car = Car()
val carMoveCommand: Command = CarMoveCommand(car)
remoteControl.onButtonPressed(carMoveCommand)
println("-----Testing offButtonPressed on RemoteControl for Car-----")
@rommansabbir
rommansabbir / ChainOfResponsibilityExample.kt
Created March 3, 2022 09:05
Design Pattern - Chain of Responsibility
class ChainOfResponsibilityExample {
companion object {
@JvmStatic
fun main(args: Array<String>) {
SupportCenterClient.handlerChain.apply {
println(".....")
receiveRequest(AbstractSupportCenter.Constants.GENERAL, "I'm having general issue.")
println(".....")
receiveRequest(AbstractSupportCenter.Constants.TECHNICAL, "I'm having technical issue.")
println(".....")