Skip to content

Commit

Permalink
refactor: remove string base client factory
Browse files Browse the repository at this point in the history
  • Loading branch information
namjug-kim committed Jul 10, 2019
1 parent 668026a commit 61b8c1c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,4 @@ class ExchangeClientFactoryTest {
assertThat(exchangeWebsocketClient).isInstanceOf(ExchangeWebsocketClient::class.java)
assertThat(exchangeWebsocketClient).isExactlyInstanceOf(BinanceWebsocketClient::class.java)
}

@Test
fun `create binance websocket client using string`() {
val exchangeWebsocketClient = ExchangeClientFactory.websocket("BINANCE")

assertThat(exchangeWebsocketClient).isNotNull
assertThat(exchangeWebsocketClient).isInstanceOf(ExchangeWebsocketClient::class.java)
assertThat(exchangeWebsocketClient).isExactlyInstanceOf(BinanceWebsocketClient::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,44 @@ class ExchangeClientFactory {
ExchangeVendor.values()
.forEach { exchangeVendor ->
ReactiveCryptoPlugins.customClientFactory
.addHttpCustomFactory(exchangeVendor.name) { http(exchangeVendor) }
.addHttpCustomFactory(exchangeVendor) { defaultHttpFactory(exchangeVendor) }
ReactiveCryptoPlugins.customClientFactory
.addWsCustomFactory(exchangeVendor.name) { websocket(exchangeVendor) }
.addWsCustomFactory(exchangeVendor) { defaultWsFactory(exchangeVendor) }
}
}

@JvmStatic
fun websocket(exchangeVendor: ExchangeVendor): ExchangeWebsocketClient {
return websocket(exchangeVendor.websocketClientName)
}

@JvmStatic
fun websocket(exchangeVendor: String): ExchangeWebsocketClient {
val customFactory: FactoryFunction<ExchangeWebsocketClient>? =
ReactiveCryptoPlugins.customClientFactory.customWsFactory()[exchangeVendor]
ReactiveCryptoPlugins.customClientFactory.getCustomWsFactory(exchangeVendor)

return if (customFactory != null) {
customFactory(exchangeVendor)
} else {
val websocketClientClass = Class.forName(exchangeVendor)?.kotlin
websocketClientClass?.createInstance() as ExchangeWebsocketClient
defaultWsFactory(exchangeVendor)
}
}

@JvmStatic
fun http(exchangeVendor: ExchangeVendor): ExchangeHttpClient {
return http(exchangeVendor.httpClientName)
private fun defaultWsFactory(exchangeVendor: ExchangeVendor): ExchangeWebsocketClient {
val websocketClientClass = Class.forName(exchangeVendor.websocketClientName)?.kotlin
return websocketClientClass?.createInstance() as ExchangeWebsocketClient
}

@JvmStatic
fun http(exchangeVendor: String): ExchangeHttpClient {
fun http(exchangeVendor: ExchangeVendor): ExchangeHttpClient {
val customFactory: FactoryFunction<ExchangeHttpClient>? =
ReactiveCryptoPlugins.customClientFactory.customHttpFactory()[exchangeVendor]
ReactiveCryptoPlugins.customClientFactory.getCustomHttpFactory(exchangeVendor)

return if (customFactory != null) {
customFactory(exchangeVendor)
} else {
val httpClientClass = Class.forName(exchangeVendor)?.kotlin
return httpClientClass?.createInstance() as ExchangeHttpClient
defaultHttpFactory(exchangeVendor)
}
}

private fun defaultHttpFactory(exchangeVendor: ExchangeVendor): ExchangeHttpClient {
val httpClientClass = Class.forName(exchangeVendor.httpClientName)?.kotlin
return httpClientClass?.createInstance() as ExchangeHttpClient
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,7 @@
package com.njkim.reactivecrypto.core.plugin

import com.njkim.reactivecrypto.core.plugin.strategy.CustomClientFactory
import java.util.concurrent.atomic.AtomicReference

object ReactiveCryptoPlugins {
private val customClientFactoryReference: AtomicReference<CustomClientFactory> = AtomicReference()
val customClientFactory: CustomClientFactory
get() {
val customClientFactory: CustomClientFactory? = customClientFactoryReference.get()

if (customClientFactory == null) {
this.customClientFactoryReference.compareAndSet(null, CustomClientFactory())
}

return this.customClientFactoryReference.get()
}
val customClientFactory = CustomClientFactory()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@

package com.njkim.reactivecrypto.core.plugin.strategy

import com.njkim.reactivecrypto.core.common.model.ExchangeVendor
import com.njkim.reactivecrypto.core.http.ExchangeHttpClient
import com.njkim.reactivecrypto.core.websocket.ExchangeWebsocketClient

class CustomClientFactory {
private val customWsFactory: MutableMap<String, FactoryFunction<ExchangeWebsocketClient>> = hashMapOf()
private val customHttpFactory: MutableMap<String, FactoryFunction<ExchangeHttpClient>> = hashMapOf()
private val customWsFactory: MutableMap<ExchangeVendor, FactoryFunction<ExchangeWebsocketClient>> = hashMapOf()
private val customHttpFactory: MutableMap<ExchangeVendor, FactoryFunction<ExchangeHttpClient>> = hashMapOf()

fun addWsCustomFactory(exchangeName: String, factory: FactoryFunction<ExchangeWebsocketClient>) {
customWsFactory[exchangeName] = factory
fun addWsCustomFactory(exchangeVendor: ExchangeVendor, factory: FactoryFunction<ExchangeWebsocketClient>) {
customWsFactory[exchangeVendor] = factory
}

fun addHttpCustomFactory(exchangeName: String, factory: FactoryFunction<ExchangeHttpClient>) {
customHttpFactory[exchangeName] = factory
fun addHttpCustomFactory(exchangeVendor: ExchangeVendor, factory: FactoryFunction<ExchangeHttpClient>) {
customHttpFactory[exchangeVendor] = factory
}

fun customWsFactory(): Map<String, FactoryFunction<ExchangeWebsocketClient>> {
return this.customWsFactory.toMap()
fun getCustomHttpFactory(exchangeVendor: ExchangeVendor): FactoryFunction<ExchangeHttpClient>? {
return this.customHttpFactory[exchangeVendor]
}

fun customHttpFactory(): Map<String, FactoryFunction<ExchangeHttpClient>> {
return this.customHttpFactory.toMap()
fun getCustomWsFactory(exchangeVendor: ExchangeVendor): FactoryFunction<ExchangeWebsocketClient>? {
return this.customWsFactory[exchangeVendor]
}
}

typealias FactoryFunction<T> = (String) -> T
typealias FactoryFunction<T> = (ExchangeVendor) -> T
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.njkim.reactivecrypto.core.plugin.strategy

import com.njkim.reactivecrypto.core.common.model.ExchangeVendor
import com.njkim.reactivecrypto.core.common.model.currency.CurrencyPair
import com.njkim.reactivecrypto.core.common.model.order.OrderBook
import com.njkim.reactivecrypto.core.common.model.order.TickData
Expand All @@ -38,16 +39,16 @@ class CustomClientFactoryTest {
}

val customClientFactory = CustomClientFactory()
val exchangeName = "TEST-EXCHANGE"
customClientFactory.addWsCustomFactory(exchangeName) { _ ->
val testExchangeVendor = ExchangeVendor("TEST-EXCHANGE")
customClientFactory.addWsCustomFactory(testExchangeVendor) { _ ->
customExchangeWebsocketClient
}

// WHEN
val factoryFunction = customClientFactory.customWsFactory()[exchangeName]
val factoryFunction = customClientFactory.getCustomWsFactory(testExchangeVendor)

// THEN
assertThat(factoryFunction).isNotNull
assertThat(factoryFunction!!(exchangeName)).isEqualTo(customExchangeWebsocketClient)
assertThat(factoryFunction!!(testExchangeVendor)).isEqualTo(customExchangeWebsocketClient)
}
}

0 comments on commit 61b8c1c

Please sign in to comment.