Skip to content

Commit

Permalink
Refactor for concise Kotlin code style
Browse files Browse the repository at this point in the history
Adjusted formatting for better readability by aligning indentation and replacing multiline function bodies with single-line expressions. Updated enumeration and class definitions to include trailing commas, where appropriate, to prepare for future modifications.
  • Loading branch information
kpavlov authored and kpavlov committed Oct 28, 2024
1 parent cd23d84 commit 6746c3e
Show file tree
Hide file tree
Showing 25 changed files with 527 additions and 471 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,87 +19,86 @@ import java.util.concurrent.atomic.AtomicReference
public abstract class AbstractIso8583Connector<
C : ConnectorConfiguration,
B : AbstractBootstrap<B, *>,
M : IsoMessage>
protected constructor(
configuration: C,
isoMessageFactory: MessageFactory<M>,
protected val messageHandler: CompositeIsoMessageHandler<M> = CompositeIsoMessageHandler()
) {
M : IsoMessage,
>
protected constructor(
configuration: C,
isoMessageFactory: MessageFactory<M>,
protected val messageHandler: CompositeIsoMessageHandler<M> = CompositeIsoMessageHandler(),
) {
protected val logger: Logger = LoggerFactory.getLogger(javaClass)

protected val logger: Logger = LoggerFactory.getLogger(javaClass)
public val isoMessageFactory: MessageFactory<M> = isoMessageFactory
private val channelRef = AtomicReference<Channel>()
protected val configuration: C = configuration
public var configurer: ConnectorConfigurer<C, B>? = null
protected lateinit var bossEventLoopGroup: EventLoopGroup
private set
protected lateinit var workerEventLoopGroup: EventLoopGroup
protected lateinit var bootstrap: B

public val isoMessageFactory: MessageFactory<M> = isoMessageFactory
private val channelRef = AtomicReference<Channel>()
protected val configuration: C = configuration
public var configurer: ConnectorConfigurer<C, B>? = null
protected lateinit var bossEventLoopGroup: EventLoopGroup
private set
protected lateinit var workerEventLoopGroup: EventLoopGroup
protected lateinit var bootstrap: B

public fun addMessageListener(handler: IsoMessageListener<M>) {
messageHandler.addListener(handler)
}

public fun removeMessageListener(handler: IsoMessageListener<M>) {
messageHandler.removeListener(handler)
}
public fun addMessageListener(handler: IsoMessageListener<M>) {
messageHandler.addListener(handler)
}

/**
* Making connector ready to create a connection / bind to port.
* Creates a Bootstrap
*
* @see AbstractBootstrap
*/
public fun init() {
logger.info("Initializing")
bossEventLoopGroup = createBossEventLoopGroup()
workerEventLoopGroup = createWorkerEventLoopGroup()
bootstrap = createBootstrap()
}
public fun removeMessageListener(handler: IsoMessageListener<M>) {
messageHandler.removeListener(handler)
}

public open fun shutdown() {
workerEventLoopGroup.shutdownGracefully()
bossEventLoopGroup.shutdownGracefully()
}
/**
* Making connector ready to create a connection / bind to port.
* Creates a Bootstrap
*
* @see AbstractBootstrap
*/
public fun init() {
logger.info("Initializing")
bossEventLoopGroup = createBossEventLoopGroup()
workerEventLoopGroup = createWorkerEventLoopGroup()
bootstrap = createBootstrap()
}

protected fun configureBootstrap(bootstrap: B) {
bootstrap.option(
ChannelOption.TCP_NODELAY,
parseBoolean(
System.getProperty(
"nfs.rpc.tcp.nodelay", "true"
)
)
)
.option(ChannelOption.AUTO_READ, true)
configurer?.configureBootstrap(bootstrap, configuration)
}
public open fun shutdown() {
workerEventLoopGroup.shutdownGracefully()
bossEventLoopGroup.shutdownGracefully()
}

protected abstract fun createBootstrap(): B
protected fun configureBootstrap(bootstrap: B) {
bootstrap
.option(
ChannelOption.TCP_NODELAY,
parseBoolean(
System.getProperty(
"nfs.rpc.tcp.nodelay",
"true",
),
),
).option(ChannelOption.AUTO_READ, true)
configurer?.configureBootstrap(bootstrap, configuration)
}

protected fun createBossEventLoopGroup(): EventLoopGroup {
return NioEventLoopGroup()
}
protected abstract fun createBootstrap(): B

protected fun createWorkerEventLoopGroup(): EventLoopGroup {
val group = NioEventLoopGroup(configuration.workerThreadsCount)
logger.debug(
"Created worker EventLoopGroup with {} executor threads",
group.executorCount()
)
return group
}
protected fun createBossEventLoopGroup(): EventLoopGroup = NioEventLoopGroup()

protected var channel: Channel?
get() = channelRef.get()
protected set(channel) {
channelRef.set(channel)
protected fun createWorkerEventLoopGroup(): EventLoopGroup {
val group = NioEventLoopGroup(configuration.workerThreadsCount)
logger.debug(
"Created worker EventLoopGroup with {} executor threads",
group.executorCount(),
)
return group
}

init {
if (configuration.shouldAddEchoMessageListener()) {
messageHandler.addListener(EchoMessageListener(isoMessageFactory))
protected var channel: Channel?
get() = channelRef.get()
protected set(channel) {
channelRef.set(channel)
}

init {
if (configuration.shouldAddEchoMessageListener()) {
messageHandler.addListener(EchoMessageListener(isoMessageFactory))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ private const val DEFAULT_FRAME_LENGTH_FIELD_ADJUST = 0
*/
private const val DEFAULT_FRAME_LENGTH_FIELD_OFFSET = 0

public abstract class ConnectorConfiguration protected constructor(b: Builder<*>) {

public abstract class ConnectorConfiguration protected constructor(
b: Builder<*>,
) {
public val addEchoMessageListener: Boolean

/**
Expand Down Expand Up @@ -122,9 +123,7 @@ public abstract class ConnectorConfiguration protected constructor(b: Builder<*>
*
* @return true if [EchoMessageListener] should be added to [CompositeIsoMessageHandler]
*/
public fun shouldAddEchoMessageListener(): Boolean {
return addEchoMessageListener
}
public fun shouldAddEchoMessageListener(): Boolean = addEchoMessageListener

/**
* Returns true is [IsoMessageLoggingHandler]
Expand All @@ -133,18 +132,14 @@ public abstract class ConnectorConfiguration protected constructor(b: Builder<*>
*
* @return true if [IsoMessageLoggingHandler] should be added.
*/
public fun addLoggingHandler(): Boolean {
return addLoggingHandler
}
public fun addLoggingHandler(): Boolean = addLoggingHandler

/**
* Whether to reply with administrative message in case of message syntax errors. Default value is `false.`
*
* @return true if reply message should be sent in case of error parsing the message.
*/
public fun replyOnError(): Boolean {
return replyOnError
}
public fun replyOnError(): Boolean = replyOnError

/**
* Returns `true` if sensitive information like PAN, CVV/CVV2, and Track2 should be printed to log.
Expand All @@ -155,13 +150,9 @@ public abstract class ConnectorConfiguration protected constructor(b: Builder<*>
*
* @return `true` if sensitive data should be printed to log
*/
public fun logSensitiveData(): Boolean {
return logSensitiveData
}
public fun logSensitiveData(): Boolean = logSensitiveData

public fun logFieldDescription(): Boolean {
return logFieldDescription
}
public fun logFieldDescription(): Boolean = logFieldDescription

Check warning on line 155 in src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt#L155

The function logFieldDescription is missing documentation.

/**
* Returns <code>true</code> if the length header is to be encoded as a String,
Expand Down Expand Up @@ -212,31 +203,36 @@ public abstract class ConnectorConfiguration protected constructor(b: Builder<*>
/**
* @param shouldAddEchoMessageListener `true` to add echo message handler.
*/
public fun addEchoMessageListener(shouldAddEchoMessageListener: Boolean = true): B = apply {
addEchoMessageListener = shouldAddEchoMessageListener
} as B
public fun addEchoMessageListener(shouldAddEchoMessageListener: Boolean = true): B =
apply {
addEchoMessageListener = shouldAddEchoMessageListener
} as B

/**
* @param length Maximum frame length.
*/
public fun maxFrameLength(length: Int): B = apply {
maxFrameLength = length
} as B
public fun maxFrameLength(length: Int): B =
apply {
maxFrameLength = length
} as B

public fun idleTimeout(timeout: Int): B = apply {
idleTimeout = timeout
} as B
public fun idleTimeout(timeout: Int): B =

Check warning on line 219 in src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt#L219

The function idleTimeout is missing documentation.
apply {
idleTimeout = timeout
} as B

public fun replyOnError(doReply: Boolean = true): B = apply {
replyOnError = doReply
} as B
public fun replyOnError(doReply: Boolean = true): B =

Check warning on line 224 in src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt#L224

The function replyOnError is missing documentation.
apply {
replyOnError = doReply
} as B

/**
* @param addLoggingHandler `true` if [IsoMessageLoggingHandler] should be added to Netty pipeline.
*/
public fun addLoggingHandler(value: Boolean = true): B = apply {
addLoggingHandler = value
} as B
public fun addLoggingHandler(value: Boolean = true): B =
apply {
addLoggingHandler = value
} as B

/**
* Should log sensitive data (unmasked) or not.
Expand All @@ -246,42 +242,50 @@ public abstract class ConnectorConfiguration protected constructor(b: Builder<*>
*
* @param logSensitiveData `true` to log sensitive data via logger
*/
public fun logSensitiveData(logSensitiveData: Boolean = true): B = apply {
this.logSensitiveData = logSensitiveData
} as B
public fun logSensitiveData(logSensitiveData: Boolean = true): B =
apply {
this.logSensitiveData = logSensitiveData
} as B

/**
* @param logFieldDescription `true` to print ISO field descriptions in the log
*/
public fun describeFieldsInLog(shouldDescribe: Boolean = true): B = apply {
logFieldDescription = shouldDescribe
} as B
public fun describeFieldsInLog(shouldDescribe: Boolean = true): B =
apply {
logFieldDescription = shouldDescribe
} as B

/**
* @param sensitiveDataFields Array of sensitive fields
*/
public fun sensitiveDataFields(vararg sensitiveDataFields: Int): B = apply {
this.sensitiveDataFields = sensitiveDataFields
} as B

public fun frameLengthFieldLength(frameLengthFieldLength: Int): B = apply {
this.frameLengthFieldLength = frameLengthFieldLength
} as B

public fun frameLengthFieldOffset(frameLengthFieldOffset: Int): B = apply {
this.frameLengthFieldOffset = frameLengthFieldOffset
} as B

public fun frameLengthFieldAdjust(frameLengthFieldAdjust: Int): B = apply {
this.frameLengthFieldAdjust = frameLengthFieldAdjust
} as B

public fun encodeFrameLengthAsString(encodeFrameLengthAsString: Boolean): B = apply {
this.encodeFrameLengthAsString = encodeFrameLengthAsString
} as B

public fun workerThreadsCount(numberOfThreads: Int): B = apply {
workerThreadsCount = numberOfThreads
} as B
public fun sensitiveDataFields(vararg sensitiveDataFields: Int): B =
apply {
this.sensitiveDataFields = sensitiveDataFields
} as B

public fun frameLengthFieldLength(frameLengthFieldLength: Int): B =

Check warning on line 266 in src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt#L266

The function frameLengthFieldLength is missing documentation.
apply {
this.frameLengthFieldLength = frameLengthFieldLength
} as B

public fun frameLengthFieldOffset(frameLengthFieldOffset: Int): B =
apply {
this.frameLengthFieldOffset = frameLengthFieldOffset
} as B

public fun frameLengthFieldAdjust(frameLengthFieldAdjust: Int): B =

Check warning on line 276 in src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt#L276

The function frameLengthFieldAdjust is missing documentation.
apply {
this.frameLengthFieldAdjust = frameLengthFieldAdjust
} as B

public fun encodeFrameLengthAsString(encodeFrameLengthAsString: Boolean): B =

Check warning on line 281 in src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt#L281

The function encodeFrameLengthAsString is missing documentation.
apply {
this.encodeFrameLengthAsString = encodeFrameLengthAsString
} as B

public fun workerThreadsCount(numberOfThreads: Int): B =

Check warning on line 286 in src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/kotlin/com/github/kpavlov/jreactive8583/ConnectorConfiguration.kt#L286

The function workerThreadsCount is missing documentation.
apply {
workerThreadsCount = numberOfThreads
} as B
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public interface ConnectorConfigurer<C : ConnectorConfiguration, B : AbstractBoo
* @param bootstrap AbstractBootstrap to configure
* @param configuration A [ConnectorConfiguration] to use
*/
public fun configureBootstrap(bootstrap: B, configuration: C) {
public fun configureBootstrap(
bootstrap: B,
configuration: C,
) {
// this method was intentionally left blank
}

Expand All @@ -37,7 +40,10 @@ public interface ConnectorConfigurer<C : ConnectorConfiguration, B : AbstractBoo
* @param pipeline A [ChannelPipeline] being configured
* @param configuration A [ConnectorConfiguration] to use
*/
public fun configurePipeline(pipeline: ChannelPipeline, configuration: C) {
public fun configurePipeline(
pipeline: ChannelPipeline,
configuration: C,
) {
// this method was intentionally left blank
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public interface IsoMessageListener<T : IsoMessage> {
* @param isoMessage received isoMessage. Not null.
* @return true if message should be handled by subsequent message listeners
*/
public fun onMessage(ctx: ChannelHandlerContext, isoMessage: T): Boolean
public fun onMessage(
ctx: ChannelHandlerContext,
isoMessage: T,
): Boolean
}
Loading

0 comments on commit 6746c3e

Please sign in to comment.