Add a Java-style builder for Driver parameters

This commit is contained in:
Andras Slemmer 2017-09-01 12:21:35 +01:00
parent 8ae09f1fb3
commit b92306b82b

View File

@ -244,17 +244,19 @@ sealed class PortAllocation {
*/ */
@JvmOverloads @JvmOverloads
fun <A> driver( fun <A> driver(
isDebug: Boolean = false, defaultParameters: DriverParameters = DriverParameters(),
driverDirectory: Path = Paths.get("build", getTimestampAsDirectoryName()), isDebug: Boolean = defaultParameters.isDebug,
portAllocation: PortAllocation = PortAllocation.Incremental(10000), driverDirectory: Path = defaultParameters.driverDirectory,
debugPortAllocation: PortAllocation = PortAllocation.Incremental(5005), portAllocation: PortAllocation = defaultParameters.portAllocation,
systemProperties: Map<String, String> = emptyMap(), debugPortAllocation: PortAllocation = defaultParameters.debugPortAllocation,
useTestClock: Boolean = false, systemProperties: Map<String, String> = defaultParameters.systemProperties,
initialiseSerialization: Boolean = true, useTestClock: Boolean = defaultParameters.useTestClock,
networkMapStartStrategy: NetworkMapStartStrategy = NetworkMapStartStrategy.Dedicated(startAutomatically = true), initialiseSerialization: Boolean = defaultParameters.initialiseSerialization,
startNodesInProcess: Boolean = false, networkMapStartStrategy: NetworkMapStartStrategy = defaultParameters.networkMapStartStrategy,
startNodesInProcess: Boolean = defaultParameters.startNodesInProcess,
dsl: DriverDSLExposedInterface.() -> A dsl: DriverDSLExposedInterface.() -> A
) = genericDriver( ): A {
return genericDriver(
driverDsl = DriverDSL( driverDsl = DriverDSL(
portAllocation = portAllocation, portAllocation = portAllocation,
debugPortAllocation = debugPortAllocation, debugPortAllocation = debugPortAllocation,
@ -268,8 +270,41 @@ fun <A> driver(
coerce = { it }, coerce = { it },
dsl = dsl, dsl = dsl,
initialiseSerialization = initialiseSerialization initialiseSerialization = initialiseSerialization
)
}
fun <A> driver(
builder: DriverBuilder,
dsl: DriverDSLExposedInterface.() -> A
): A {
return driver(defaultParameters = builder.parameters, dsl = dsl)
}
data class DriverParameters(
val isDebug: Boolean = false,
val driverDirectory: Path = Paths.get("build", getTimestampAsDirectoryName()),
val portAllocation: PortAllocation = PortAllocation.Incremental(10000),
val debugPortAllocation: PortAllocation = PortAllocation.Incremental(5005),
val systemProperties: Map<String, String> = emptyMap(),
val useTestClock: Boolean = false,
val initialiseSerialization: Boolean = true,
val networkMapStartStrategy: NetworkMapStartStrategy = NetworkMapStartStrategy.Dedicated(startAutomatically = true),
val startNodesInProcess: Boolean = false
) )
class DriverBuilder(val parameters: DriverParameters) {
constructor() : this(DriverParameters())
fun setIsDebug(newValue: Boolean) = DriverBuilder(parameters.copy(isDebug = newValue))
fun setDriverDirectory(newValue: Path) = DriverBuilder(parameters.copy(driverDirectory = newValue))
fun setPortAllocation(newValue: PortAllocation) = DriverBuilder(parameters.copy(portAllocation = newValue))
fun setDebugPortAllocation(newValue: PortAllocation) = DriverBuilder(parameters.copy(debugPortAllocation = newValue))
fun setSystemProperties(newValue: Map<String, String>) = DriverBuilder(parameters.copy(systemProperties = newValue))
fun setUseTestClock(newValue: Boolean) = DriverBuilder(parameters.copy(useTestClock = newValue))
fun setInitialiseSerialization(newValue: Boolean) = DriverBuilder(parameters.copy(initialiseSerialization = newValue))
fun setNetworkMapStartStrategy(newValue: NetworkMapStartStrategy) = DriverBuilder(parameters.copy(networkMapStartStrategy = newValue))
fun setStartNodesInProcess(newValue: Boolean) = DriverBuilder(parameters.copy(startNodesInProcess = newValue))
}
/** /**
* This is a helper method to allow extending of the DSL, along the lines of * This is a helper method to allow extending of the DSL, along the lines of
* interface SomeOtherExposedDSLInterface : DriverDSLExposedInterface * interface SomeOtherExposedDSLInterface : DriverDSLExposedInterface