From 7603c8139b225a22fa88ad1348940803f0b86e36 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sun, 21 Apr 2024 22:23:32 -0700 Subject: [PATCH] chore(deps): update plugin org.jlleitschuh.gradle.ktlint to v12 --- build.gradle.kts | 2 +- .../rahulsom/grooves/EventApplyOutcome.kt | 3 +- .../com/github/rahulsom/grooves/EventType.kt | 5 +- .../github/rahulsom/grooves/GroovesQuery.kt | 19 ++-- .../rahulsom/grooves/functions/Deprecator.kt | 5 +- .../grooves/functions/EventHandler.kt | 5 +- .../grooves/functions/EventsProvider.kt | 2 +- .../grooves/functions/ExceptionHandler.kt | 6 +- .../grooves/functions/SnapshotProvider.kt | 5 +- .../grooves/functions/SnapshotVersioner.kt | 5 +- .../rahulsom/grooves/impl/GroovesQueryImpl.kt | 58 +++++++----- .../grooves/logging/IndentedLogging.kt | 8 +- .../rahulsom/grooves/asciidoctor/Aggregate.kt | 43 ++++----- .../rahulsom/grooves/asciidoctor/Constants.kt | 12 +-- .../rahulsom/grooves/asciidoctor/Event.kt | 49 ++++++----- .../rahulsom/grooves/asciidoctor/EventType.kt | 7 +- .../grooves/asciidoctor/EventsBlock.kt | 18 ++-- .../grooves/asciidoctor/SvgBuilder.kt | 39 ++++---- .../grooves/example/push/Application.kt | 88 ++++++++++--------- .../kotlin/grooves/example/push/Balance.kt | 25 +++--- .../grooves/example/push/BankingModule.kt | 4 +- .../example/push/ContextAwareScheduler.kt | 8 +- .../grooves/example/push/ContextManager.kt | 2 +- .../kotlin/grooves/example/push/Database.kt | 23 ++--- .../grooves/example/push/EventService.kt | 26 +++--- .../grooves/example/push/Transaction.kt | 10 +-- .../grooves/example/pushstyle/PushTest.kt | 1 - 27 files changed, 273 insertions(+), 205 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 380319dc5..ed2a56c1a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("com.github.rahulsom.waena.root").version("0.6.1") id("org.jetbrains.kotlin.jvm").version("1.8.22").apply(false) id("org.jetbrains.kotlin.plugin.allopen").version("1.8.22").apply(false) - id("org.jlleitschuh.gradle.ktlint").version("11.6.1").apply(false) + id("org.jlleitschuh.gradle.ktlint").version("12.1.0").apply(false) id("org.springframework.boot").version("3.0.4").apply(false) id("io.spring.dependency-management").version("1.1.4").apply(false) id("org.sonarqube").version("4.4.1.3373") diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/EventApplyOutcome.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/EventApplyOutcome.kt index 578b49f28..67e0f69d1 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/EventApplyOutcome.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/EventApplyOutcome.kt @@ -4,5 +4,6 @@ package com.github.rahulsom.grooves * The outcome of applying an event to a snapshot. */ enum class EventApplyOutcome { - RETURN, CONTINUE + RETURN, + CONTINUE, } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/EventType.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/EventType.kt index 15dba31e2..c8f461418 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/EventType.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/EventType.kt @@ -1,5 +1,8 @@ package com.github.rahulsom.grooves enum class EventType { - Normal, Revert, Deprecates, DeprecatedBy + Normal, + Revert, + Deprecates, + DeprecatedBy, } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/GroovesQuery.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/GroovesQuery.kt index 5314f1354..b69fd53a4 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/GroovesQuery.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/GroovesQuery.kt @@ -1,12 +1,17 @@ package com.github.rahulsom.grooves interface GroovesQuery { - fun computeSnapshot(aggregate: Aggregate, at: VersionOrTimestamp?, redirect: Boolean): - GroovesResult + fun computeSnapshot( + aggregate: Aggregate, + at: VersionOrTimestamp?, + redirect: Boolean, + ): GroovesResult - fun computeSnapshot(aggregate: Aggregate, at: VersionOrTimestamp?) = - ( - computeSnapshot(aggregate, at, true) - as GroovesResult.Success - ).snapshot + fun computeSnapshot( + aggregate: Aggregate, + at: VersionOrTimestamp?, + ) = ( + computeSnapshot(aggregate, at, true) + as GroovesResult.Success + ).snapshot } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/Deprecator.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/Deprecator.kt index 99c2aeecb..df20a7c6b 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/Deprecator.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/Deprecator.kt @@ -7,5 +7,8 @@ import com.github.rahulsom.grooves.logging.Trace */ interface Deprecator { @Trace - fun invoke(snapshot: Snapshot, deprecatingAggregate: Aggregate) + fun invoke( + snapshot: Snapshot, + deprecatingAggregate: Aggregate, + ) } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/EventHandler.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/EventHandler.kt index b412b7654..f243d69b7 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/EventHandler.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/EventHandler.kt @@ -8,5 +8,8 @@ import com.github.rahulsom.grooves.logging.Trace */ interface EventHandler { @Trace - fun invoke(event: Event, snapshot: Snapshot): EventApplyOutcome + fun invoke( + event: Event, + snapshot: Snapshot, + ): EventApplyOutcome } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/EventsProvider.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/EventsProvider.kt index 52a43e872..f92b014c2 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/EventsProvider.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/EventsProvider.kt @@ -12,6 +12,6 @@ interface EventsProvider { fun invoke( aggregates: List, versionOrTimestamp: VersionOrTimestamp?, - lastSnapshot: Snapshot + lastSnapshot: Snapshot, ): Stream } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/ExceptionHandler.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/ExceptionHandler.kt index e61d0563e..797191fbd 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/ExceptionHandler.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/ExceptionHandler.kt @@ -8,5 +8,9 @@ import com.github.rahulsom.grooves.logging.Trace */ interface ExceptionHandler { @Trace - fun invoke(exception: Exception, snapshot: Snapshot, event: Event): EventApplyOutcome + fun invoke( + exception: Exception, + snapshot: Snapshot, + event: Event, + ): EventApplyOutcome } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/SnapshotProvider.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/SnapshotProvider.kt index b4d7393c5..2ee903e0d 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/SnapshotProvider.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/SnapshotProvider.kt @@ -7,5 +7,8 @@ import com.github.rahulsom.grooves.logging.Trace */ interface SnapshotProvider { @Trace - fun invoke(aggregate: Aggregate, versionOrTimestamp: VersionOrTimestamp?): Snapshot? + fun invoke( + aggregate: Aggregate, + versionOrTimestamp: VersionOrTimestamp?, + ): Snapshot? } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/SnapshotVersioner.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/SnapshotVersioner.kt index 5bdb94677..0373e3fcd 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/SnapshotVersioner.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/functions/SnapshotVersioner.kt @@ -7,5 +7,8 @@ import com.github.rahulsom.grooves.logging.Trace */ interface SnapshotVersioner { @Trace - fun invoke(snapshot: Snapshot, versionOrTimestamp: VersionOrTimestamp) + fun invoke( + snapshot: Snapshot, + versionOrTimestamp: VersionOrTimestamp, + ) } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/impl/GroovesQueryImpl.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/impl/GroovesQueryImpl.kt index d24a30ee3..b3478a9b0 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/impl/GroovesQueryImpl.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/impl/GroovesQueryImpl.kt @@ -39,13 +39,16 @@ class GroovesQueryImpl( private val snapshotVersioner: SnapshotVersioner, private val deprecatedByProvider: DeprecatedByProvider, private val revertedEventProvider: RevertedEventProvider, - private val eventIdProvider: EventIdProvider + private val eventIdProvider: EventIdProvider, ) : GroovesQuery { - private val log = LoggerFactory.getLogger(javaClass) @Trace - override fun computeSnapshot(aggregate: Aggregate, at: VersionOrTimestamp?, redirect: Boolean): GroovesResult { + override fun computeSnapshot( + aggregate: Aggregate, + at: VersionOrTimestamp?, + redirect: Boolean, + ): GroovesResult { val providedSnapshot = snapshotProvider.invoke(aggregate, at) val snapshot = providedSnapshot ?: emptySnapshotProvider.invoke(aggregate) val events = eventsProvider.invoke(listOf(aggregate), at, snapshot).collect(Collectors.toList()) @@ -67,18 +70,20 @@ class GroovesQueryImpl( aggregates: List, at: VersionOrTimestamp?, redirect: Boolean, - beforeReturn: (CallIdentifier, Snapshot?) -> Unit + beforeReturn: (CallIdentifier, Snapshot?) -> Unit, ): GroovesResult { val indent = IndentedLogging.indent() - val callIdentifier = CallIdentifier( - "${indent}computeSnapshotImpl(<... ${events.size} items>, $snapshot, $aggregates, $at)" - ) + val callIdentifier = + CallIdentifier( + "${indent}computeSnapshotImpl(<... ${events.size} items>, $snapshot, $aggregates, $at)", + ) log.trace(callIdentifier.data) IndentedLogging.stepIn() - val (revertEvents, forwardEvents) = events - .partition { eventClassifier.invoke(it) == Revert } - .let { it.first.toMutableList() to it.second.toMutableList() } + val (revertEvents, forwardEvents) = + events + .partition { eventClassifier.invoke(it) == Revert } + .let { it.first.toMutableList() to it.second.toMutableList() } if (revertsExistOutsideEvents(revertEvents, indent, forwardEvents)) { val snapshot1 = emptySnapshotProvider.invoke(aggregates[0]) @@ -110,14 +115,16 @@ class GroovesQueryImpl( DeprecatedBy -> { val ret = deprecatedByProvider.invoke(event) log.debug("$indent ...The aggregate was deprecated by ${ret.aggregate}. Recursing to compute snapshot for it...") - val refEvent = eventsProvider.invoke(listOf(ret.aggregate), null, emptySnapshotProvider.invoke(ret.aggregate)) - .collect(Collectors.toList()) - .find { eventIdProvider.invoke(it) == ret.eventId } + val refEvent = + eventsProvider.invoke(listOf(ret.aggregate), null, emptySnapshotProvider.invoke(ret.aggregate)) + .collect(Collectors.toList()) + .find { eventIdProvider.invoke(it) == ret.eventId } val redirectVersion = eventVersioner.invoke(refEvent!!) val otherSnapshot = snapshotProvider.invoke(ret.aggregate, redirectVersion) ?: emptySnapshotProvider.invoke(ret.aggregate) - val newEvents = eventsProvider.invoke(listOf(ret.aggregate) + aggregates, redirectVersion, otherSnapshot) - .collect(Collectors.toList()) + val newEvents = + eventsProvider.invoke(listOf(ret.aggregate) + aggregates, redirectVersion, otherSnapshot) + .collect(Collectors.toList()) @Suppress("LiftReturnOrAssignment") if (redirect) { return computeSnapshotImpl(newEvents, otherSnapshot, aggregates + listOf(ret.aggregate), at, redirect) { c, s -> @@ -152,7 +159,11 @@ class GroovesQueryImpl( return GroovesResult.Success(snapshot) } - private fun revertsExistOutsideEvents(revertEvents: MutableList, indent: String, forwardEvents: MutableList): Boolean { + private fun revertsExistOutsideEvents( + revertEvents: MutableList, + indent: String, + forwardEvents: MutableList, + ): Boolean { while (revertEvents.isNotEmpty()) { val mostRecentRevert = revertEvents.removeLast() val revertedEvent = revertedEventProvider.invoke(mostRecentRevert) @@ -172,10 +183,13 @@ class GroovesQueryImpl( return false } - private inline fun tryRunning(snapshot: Snapshot, it: Event, code: () -> EventApplyOutcome) = - try { - code() - } catch (e: Exception) { - exceptionHandler.invoke(e, snapshot, it) - } + private inline fun tryRunning( + snapshot: Snapshot, + it: Event, + code: () -> EventApplyOutcome, + ) = try { + code() + } catch (e: Exception) { + exceptionHandler.invoke(e, snapshot, it) + } } \ No newline at end of file diff --git a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/logging/IndentedLogging.kt b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/logging/IndentedLogging.kt index a51472178..1dc7c16ff 100644 --- a/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/logging/IndentedLogging.kt +++ b/grooves-core/src/main/kotlin/com/github/rahulsom/grooves/logging/IndentedLogging.kt @@ -11,18 +11,24 @@ class IndentedLogging { companion object { private val indentLevel = ThreadLocal() private const val INITIAL_INDENT = 1 + fun stepIn() = indentLevel.set(indentLevel.getOrSet { INITIAL_INDENT } + 1) + fun stepOut() = indentLevel.set(indentLevel.getOrSet { INITIAL_INDENT } - 1) @JvmStatic fun indent() = "".padStart(indentLevel.getOrSet { INITIAL_INDENT } * 2) + private fun eventsToString(it: List<*>): Any = "<... ${it.size} item(s)>" } @Suppress("unused", "UNUSED_PARAMETER") @Around(value = "@annotation(trace)", argNames = "trace") @ExperimentalStdlibApi - fun around(joinPoint: ProceedingJoinPoint, trace: Trace): Any? { + fun around( + joinPoint: ProceedingJoinPoint, + trace: Trace, + ): Any? { val signature = joinPoint.signature val classWithFunction = joinPoint.target.javaClass val loggerName = classWithFunction.name.replace(Regex("\\\$\\\$Lambda.*$"), "") diff --git a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Aggregate.kt b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Aggregate.kt index af6037e6b..4098576e2 100644 --- a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Aggregate.kt +++ b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Aggregate.kt @@ -1,11 +1,11 @@ package com.github.rahulsom.grooves.asciidoctor -import com.github.rahulsom.grooves.asciidoctor.Constants.aggregateHeight -import com.github.rahulsom.grooves.asciidoctor.Constants.aggregateWidth -import com.github.rahulsom.grooves.asciidoctor.Constants.eventLineHeight -import com.github.rahulsom.grooves.asciidoctor.Constants.eventSpace -import com.github.rahulsom.grooves.asciidoctor.Constants.offset -import com.github.rahulsom.grooves.asciidoctor.Constants.textLineHeight +import com.github.rahulsom.grooves.asciidoctor.Constants.AGGREGATE_HEIGHT +import com.github.rahulsom.grooves.asciidoctor.Constants.AGGREGATE_WIDTH +import com.github.rahulsom.grooves.asciidoctor.Constants.EVENT_LINE_HEIGHT +import com.github.rahulsom.grooves.asciidoctor.Constants.EVENT_SPACE +import com.github.rahulsom.grooves.asciidoctor.Constants.OFFSET +import com.github.rahulsom.grooves.asciidoctor.Constants.TEXT_LINE_HEIGHT import com.github.rahulsom.svg.G import com.github.rahulsom.svg.ObjectFactory import java.util.Date @@ -25,39 +25,40 @@ class Aggregate(private val counter: Int, var type: String, var id: String, var } fun buildSvg(dates: Map): G { - val y = index * eventLineHeight + offset - val yMid = index * eventLineHeight + offset + aggregateHeight / 2 + val y = index * EVENT_LINE_HEIGHT + OFFSET + val yMid = index * EVENT_LINE_HEIGHT + OFFSET + AGGREGATE_HEIGHT / 2 val objectFactory = ObjectFactory() - val g = objectFactory.createG() - .withId("aggregate_$counter") - .withClazz("aggregate") + val g = + objectFactory.createG() + .withId("aggregate_$counter") + .withClazz("aggregate") return g.withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( objectFactory.createRect( objectFactory.createRect().withX("10").withY(y.toString()) - .withWidth("$aggregateWidth").withHeight("$aggregateHeight") + .withWidth("$AGGREGATE_WIDTH").withHeight("$AGGREGATE_HEIGHT"), ), objectFactory.createText( - objectFactory.createText().withX("15").withY("${y + textLineHeight}") + objectFactory.createText().withX("15").withY("${y + TEXT_LINE_HEIGHT}") .withClazz("type") - .withContent(type) + .withContent(type), ), objectFactory.createText( - objectFactory.createText().withX("15").withY("${y + textLineHeight * 2}") + objectFactory.createText().withX("15").withY("${y + TEXT_LINE_HEIGHT * 2}") .withClazz("id") - .withContent(id) + .withContent(id), ), objectFactory.createText( objectFactory.createText().withX("10").withY("${y - 5}") .withClazz("description") - .withContent(description) + .withContent(description), ), objectFactory.createLine( - objectFactory.createLine().withX1("${10 + aggregateWidth}").withY1("$yMid") - .withX2("${dates.values.maxOrNull()!! * eventSpace + 3 * aggregateWidth}") + objectFactory.createLine().withX1("${10 + AGGREGATE_WIDTH}").withY1("$yMid") + .withX2("${dates.values.maxOrNull()!! * EVENT_SPACE + 3 * AGGREGATE_WIDTH}") .withY2("$yMid") - .withClazz("eventLine").withMarkerEnd("url(#triangle)") - ) + .withClazz("eventLine").withMarkerEnd("url(#triangle)"), + ), ) } } \ No newline at end of file diff --git a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Constants.kt b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Constants.kt index 4ce6a4a87..d8fbab701 100644 --- a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Constants.kt +++ b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Constants.kt @@ -6,12 +6,12 @@ package com.github.rahulsom.grooves.asciidoctor * @author Rahul Somasunderam */ object Constants { - const val eventLineHeight: Int = 100 - const val aggregateHeight: Int = 40 - const val aggregateWidth: Int = 100 - const val eventSpace: Int = 50 - const val offset: Int = 45 - const val textLineHeight: Int = 18 + const val EVENT_LINE_HEIGHT: Int = 100 + const val AGGREGATE_HEIGHT: Int = 40 + const val AGGREGATE_WIDTH: Int = 100 + const val EVENT_SPACE: Int = 50 + const val OFFSET: Int = 45 + const val TEXT_LINE_HEIGHT: Int = 18 val CSS: String by lazy { Constants::class.java.getResourceAsStream("/esdiag.css").reader().readText() diff --git a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Event.kt b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Event.kt index 84faa912e..c9fa664d3 100644 --- a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Event.kt +++ b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/Event.kt @@ -1,10 +1,10 @@ package com.github.rahulsom.grooves.asciidoctor -import com.github.rahulsom.grooves.asciidoctor.Constants.aggregateHeight -import com.github.rahulsom.grooves.asciidoctor.Constants.aggregateWidth -import com.github.rahulsom.grooves.asciidoctor.Constants.eventLineHeight -import com.github.rahulsom.grooves.asciidoctor.Constants.eventSpace -import com.github.rahulsom.grooves.asciidoctor.Constants.offset +import com.github.rahulsom.grooves.asciidoctor.Constants.AGGREGATE_HEIGHT +import com.github.rahulsom.grooves.asciidoctor.Constants.AGGREGATE_WIDTH +import com.github.rahulsom.grooves.asciidoctor.Constants.EVENT_LINE_HEIGHT +import com.github.rahulsom.grooves.asciidoctor.Constants.EVENT_SPACE +import com.github.rahulsom.grooves.asciidoctor.Constants.OFFSET import com.github.rahulsom.svg.Circle import com.github.rahulsom.svg.G import com.github.rahulsom.svg.ObjectFactory @@ -19,7 +19,6 @@ import kotlin.math.sqrt * @author Rahul Somasunderam */ class Event(private val counter: Int, var id: String, var date: Date, var description: String, var type: EventType) { - override fun toString(): String { val sdf = SimpleDateFormat("yyyy-MM-dd") return " - $id ${sdf.format(date)} $description ($type)" @@ -29,7 +28,10 @@ class Event(private val counter: Int, var id: String, var date: Date, var descri var x: Int = 0 var y: Int = 0 - fun buildSvg(index: Int, svgBuilder: SvgBuilder): G { + fun buildSvg( + index: Int, + svgBuilder: SvgBuilder, + ): G { if (description == ".") { return G() } @@ -37,11 +39,12 @@ class Event(private val counter: Int, var id: String, var date: Date, var descri val xOffset = svgBuilder.dates[date]!! // builder.mkp.comment(toString()) val revertedClass = if (this.reverted) "reverted" else "" - val g = G().withId("event_$counter") - .withClazz("event ${this.type.name} $revertedClass") + val g = + G().withId("event_$counter") + .withClazz("event ${this.type.name} $revertedClass") - var x = (10 + aggregateWidth * 2 + xOffset * eventSpace).toInt() - var y = index * eventLineHeight + offset + aggregateHeight / 2 + var x = (10 + AGGREGATE_WIDTH * 2 + xOffset * EVENT_SPACE).toInt() + var y = index * EVENT_LINE_HEIGHT + OFFSET + AGGREGATE_HEIGHT / 2 while (svgBuilder.allEvents.find { it.x == x && it.y == y } != null) { y -= (20 * sqrt(3.0) / 2).toInt() @@ -58,8 +61,8 @@ class Event(private val counter: Int, var id: String, var date: Date, var descri val y1 = y - ((this.x - reverted.x) / 3) g.withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( ObjectFactory().createPath( - ObjectFactory().createPath().withD("M$x $y Q $x1 $y1, ${reverted.x + 15} ${reverted.y - 15}") - ) + ObjectFactory().createPath().withD("M$x $y Q $x1 $y1, ${reverted.x + 15} ${reverted.y - 15}"), + ), ) } @@ -68,7 +71,7 @@ class Event(private val counter: Int, var id: String, var date: Date, var descri val other = svgBuilder.allEvents.find { it.id == otherId }!! if (other.x > 0 && other.y > 0) { - val x1 = (if (type == EventType.Disjoin) -30 else 30) * abs(other.y - y) / eventLineHeight + val x1 = (if (type == EventType.Disjoin) -30 else 30) * abs(other.y - y) / EVENT_LINE_HEIGHT val xContactOffset = if (type == EventType.Disjoin) -10 else 10 val y1 = (y + other.y) / 2 @@ -80,9 +83,9 @@ class Event(private val counter: Int, var id: String, var date: Date, var descri g.withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( ObjectFactory().createPath( ObjectFactory().createPath().withD( - "M${this.x} $y Q ${x1 + this.x} $y1, ${other.x + xContactOffset} ${other.y + yOffset}" - ) - ) + "M${this.x} $y Q ${x1 + this.x} $y1, ${other.x + xContactOffset} ${other.y + yOffset}", + ), + ), ) } } @@ -92,8 +95,8 @@ class Event(private val counter: Int, var id: String, var date: Date, var descri ObjectFactory().createCircle( Circle().withCx("${this.x}").withCy("$y").withR("14") .withStrokeDasharray("2, 5") - .withClazz("eventCreated ${this.type.name}") - ) + .withClazz("eventCreated ${this.type.name}"), + ), ) } @@ -101,14 +104,14 @@ class Event(private val counter: Int, var id: String, var date: Date, var descri ObjectFactory().createCircle( ObjectFactory().createCircle().withCx("${this.x}").withCy("${this.y}") .withR("10") - .withClazz("event ${this.type.name} $revertedClass") - ) + .withClazz("event ${this.type.name} $revertedClass"), + ), ) g.withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( ObjectFactory().createText( ObjectFactory().createText().withX("${this.x}").withY("${this.y}") - .withClazz("eventId").withContent(this.id) - ) + .withClazz("eventId").withContent(this.id), + ), ) return g } diff --git a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/EventType.kt b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/EventType.kt index 009e0c4e0..cf67fa903 100644 --- a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/EventType.kt +++ b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/EventType.kt @@ -6,5 +6,10 @@ package com.github.rahulsom.grooves.asciidoctor * @author Rahul Somasunderam */ enum class EventType { - Normal, Revert, Deprecates, DeprecatedBy, Join, Disjoin + Normal, + Revert, + Deprecates, + DeprecatedBy, + Join, + Disjoin, } \ No newline at end of file diff --git a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/EventsBlock.kt b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/EventsBlock.kt index 75273a73c..0d4967533 100644 --- a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/EventsBlock.kt +++ b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/EventsBlock.kt @@ -14,8 +14,11 @@ import java.security.MessageDigest */ class EventsBlock(name: String) : BlockProcessor(name, mapOf("contexts" to listOf(":literal"), "content_model" to ":simple")) { - - override fun process(parent: StructuralNode, reader: Reader, attributes: MutableMap?): Any { + override fun process( + parent: StructuralNode, + reader: Reader, + attributes: MutableMap?, + ): Any { val projectDirAttr = parent.document.attributes["gradle-projectdir"] as String val outDir = File(projectDirAttr, "build/docs/asciidoc") @@ -26,11 +29,12 @@ class EventsBlock(name: String) : SvgBuilder(input).write(File(outDir, filename)) - val newAttributes = mapOf( - "type" to ":image", - "target" to filename, - "format" to "svg" - ) + val newAttributes = + mapOf( + "type" to ":image", + "target" to filename, + "format" to "svg", + ) return createBlock(parent, "image", input, newAttributes, attributes?.mapKeys { it }) } diff --git a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/SvgBuilder.kt b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/SvgBuilder.kt index 0d0f61b59..58bd22b90 100644 --- a/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/SvgBuilder.kt +++ b/grooves-diagrams/src/main/kotlin/com/github/rahulsom/grooves/asciidoctor/SvgBuilder.kt @@ -1,9 +1,9 @@ package com.github.rahulsom.grooves.asciidoctor +import com.github.rahulsom.grooves.asciidoctor.Constants.AGGREGATE_WIDTH import com.github.rahulsom.grooves.asciidoctor.Constants.CSS -import com.github.rahulsom.grooves.asciidoctor.Constants.aggregateWidth -import com.github.rahulsom.grooves.asciidoctor.Constants.eventLineHeight -import com.github.rahulsom.grooves.asciidoctor.Constants.eventSpace +import com.github.rahulsom.grooves.asciidoctor.Constants.EVENT_LINE_HEIGHT +import com.github.rahulsom.grooves.asciidoctor.Constants.EVENT_SPACE import com.github.rahulsom.svg.ObjectFactory import com.github.rahulsom.svg.Path import com.github.rahulsom.svg.Rect @@ -96,20 +96,21 @@ class SvgBuilder(private val input: String) { fun write(file: File) { init() - val svg = Svg().withHeight("${aggregates.size * eventLineHeight}") - .withWidth("${dates.values.maxOrNull()!! * eventSpace + 4 * aggregateWidth}") + val svg = + Svg().withHeight("${aggregates.size * EVENT_LINE_HEIGHT}") + .withWidth("${dates.values.maxOrNull()!! * EVENT_SPACE + 4 * AGGREGATE_WIDTH}") svg.withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( ObjectFactory().createStyle( - SVGStyleClass().withContent("/* */") - ) + SVGStyleClass().withContent("/* */"), + ), ) svg.withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( ObjectFactory().createRect( - Rect().withX("0").withY("0").withHeight("${aggregates.size * eventLineHeight}").withWidth( - "${dates.values.maxOrNull()!! * eventSpace + 4 * aggregateWidth}" - ).withClazz("background") + Rect().withX("0").withY("0").withHeight("${aggregates.size * EVENT_LINE_HEIGHT}").withWidth( + "${dates.values.maxOrNull()!! * EVENT_SPACE + 4 * AGGREGATE_WIDTH}", + ).withClazz("background"), ), ObjectFactory().createDefs( ObjectFactory().createDefs().withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( @@ -119,24 +120,24 @@ class SvgBuilder(private val input: String) { .withMarkerWidth("10").withMarkerHeight("10") .withOrient("auto").withMarkerUnits("userSpaceOnUse") .withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( - ObjectFactory().createPath(Path().withD("M 0 0 L 10 5 L 0 10 z")) - ) - ) - ) - ) + ObjectFactory().createPath(Path().withD("M 0 0 L 10 5 L 0 10 z")), + ), + ), + ), + ), ) aggregates.forEach { aggregate -> svg.withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( ObjectFactory().createG( aggregate.buildSvg( - dates - ) - ) + dates, + ), + ), ) aggregate.events.forEach { event -> svg.withSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass( - ObjectFactory().createG(event.buildSvg(aggregate.index, this)) + ObjectFactory().createG(event.buildSvg(aggregate.index, this)), ) } } diff --git a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Application.kt b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Application.kt index 0dd67a57f..daf5e3bfd 100644 --- a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Application.kt +++ b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Application.kt @@ -10,62 +10,70 @@ import org.slf4j.LoggerFactory.getLogger import java.util.Date import java.util.UUID -class Application @Inject constructor( - private val eventBus: EventBus, - private val eventService: EventService, - private val database: Database, - private val dslContext: DSLContext -) : AbstractService() { +class Application + @Inject + constructor( + private val eventBus: EventBus, + private val eventService: EventService, + private val database: Database, + private val dslContext: DSLContext, + ) : AbstractService() { + public override fun doStop() { + dslContext.dropTable(BALANCE) + eventBus.unregister(eventService) + log.error("======== Shutting down ========") + } - public override fun doStop() { - dslContext.dropTable(BALANCE) - eventBus.unregister(eventService) - log.error("======== Shutting down ========") - } + public override fun doStart() { + log.error("") + log.error("") + log.error("") + log.error("========= Starting up =========") + dslContext.createSchemaIfNotExists(Public.PUBLIC).execute() + dslContext.createTableIfNotExists(BALANCE) + .column(BALANCE.B_ID) + .column(BALANCE.B_VERSION) + .column(BALANCE.B_TIME) + .column(BALANCE.B_ACCOUNT) + .column(BALANCE.BALANCE_) + .execute() + eventBus.register(eventService) + log.error("doStart complete") + } - public override fun doStart() { - log.error("") - log.error("") - log.error("") - log.error("========= Starting up =========") - dslContext.createSchemaIfNotExists(Public.PUBLIC).execute() - dslContext.createTableIfNotExists(BALANCE) - .column(BALANCE.B_ID) - .column(BALANCE.B_VERSION) - .column(BALANCE.B_TIME) - .column(BALANCE.B_ACCOUNT) - .column(BALANCE.BALANCE_) - .execute() - eventBus.register(eventService) - log.error("doStart complete") - } + private val log = getLogger(this.javaClass) - private val log = getLogger(this.javaClass) - - fun deposit(accountId: String, position: Long, atmId: String, amount: Long) = - eventBus.post( + fun deposit( + accountId: String, + position: Long, + atmId: String, + amount: Long, + ) = eventBus.post( Transaction.Deposit( UUID.randomUUID().toString(), Account(accountId), Date(), position, atmId, - amount - ) + amount, + ), ) - fun withdraw(accountId: String, position: Long, atmId: String, amount: Long) = - eventBus.post( + fun withdraw( + accountId: String, + position: Long, + atmId: String, + amount: Long, + ) = eventBus.post( Transaction.Withdraw( UUID.randomUUID().toString(), Account(accountId), Date(), position, atmId, - amount - ) + amount, + ), ) - fun getBalance(accountId: String) = - database.getBalance(Account(accountId), null)?.balance -} \ No newline at end of file + fun getBalance(accountId: String) = database.getBalance(Account(accountId), null)?.balance + } \ No newline at end of file diff --git a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Balance.kt b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Balance.kt index aa0495bc2..2adeaf0e9 100644 --- a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Balance.kt +++ b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Balance.kt @@ -7,7 +7,8 @@ import org.reactivestreams.Publisher import java.text.SimpleDateFormat import java.time.OffsetDateTime import java.time.ZoneId -import java.util.* +import java.util.Date +import java.util.UUID class Balance() : Snapshot { override var id: String? = UUID.randomUUID().toString() @@ -24,7 +25,7 @@ class Balance() : Snapshot { aggregate: Account?, balance: Long, lastEventPosition: Long, - lastEventTimestamp: Date + lastEventTimestamp: Date, ) : this() { this.id = id this.aggregate = aggregate @@ -39,7 +40,7 @@ class Balance() : Snapshot { Account(balance.bAccount), balance.balance, balance.bVersion, - Date.from(balance.bTime.toInstant()) + Date.from(balance.bTime.toInstant()), ) fun toBalanceRecord(): BalanceRecord { @@ -48,18 +49,15 @@ class Balance() : Snapshot { lastEventPosition, OffsetDateTime.ofInstant(lastEventTimestamp?.toInstant(), ZoneId.systemDefault()), aggregate?.id, - balance + balance, ) } - override fun getAggregateObservable(): Publisher = - Flowable.fromIterable(listOfNotNull(aggregate)) + override fun getAggregateObservable(): Publisher = Flowable.fromIterable(listOfNotNull(aggregate)) - override fun getDeprecatesObservable() = - Flowable.fromIterable(deprecates) + override fun getDeprecatesObservable() = Flowable.fromIterable(deprecates) - override fun getDeprecatedByObservable(): Publisher = - Flowable.fromIterable(listOfNotNull(deprecatedBy)) + override fun getDeprecatedByObservable(): Publisher = Flowable.fromIterable(listOfNotNull(deprecatedBy)) override fun setAggregate(aggregate: Account) { this.aggregate = aggregate @@ -72,9 +70,10 @@ class Balance() : Snapshot { override fun toString(): String { val idPart = id?.substring(24) val aggPart = aggregate?.id - val ts = lastEventTimestamp.let { - SimpleDateFormat("HH:mm:ss,SSS").format(it) - } + val ts = + lastEventTimestamp.let { + SimpleDateFormat("HH:mm:ss,SSS").format(it) + } return "Balance(id=$idPart, aggregate=$aggPart, lastEventTimestamp=$ts, " + "version=$lastEventPosition, balance=$balance)" } diff --git a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/BankingModule.kt b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/BankingModule.kt index 5b3a798fa..142ac896c 100644 --- a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/BankingModule.kt +++ b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/BankingModule.kt @@ -8,7 +8,6 @@ import org.jooq.impl.DSL import java.sql.DriverManager.getConnection object BankingModule : AbstractModule() { - override fun configure() { bind(Application::class.java) bind(EventBus::class.java) @@ -17,6 +16,5 @@ object BankingModule : AbstractModule() { } @Provides - fun dslContext() = - DSL.using(getConnection("jdbc:h2:mem:app", "sa", ""), H2) + fun dslContext() = DSL.using(getConnection("jdbc:h2:mem:app", "sa", ""), H2) } \ No newline at end of file diff --git a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/ContextAwareScheduler.kt b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/ContextAwareScheduler.kt index a875f9e9c..71f3c17c8 100644 --- a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/ContextAwareScheduler.kt +++ b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/ContextAwareScheduler.kt @@ -16,16 +16,18 @@ import java.util.concurrent.TimeUnit * that works for RxJava even though RxJava manages threads differently. */ object ContextAwareScheduler : Scheduler() { - private val worker = NewThreadWorker(RxThreadFactory("ContextAwareScheduler")) override fun createWorker(): Worker = ContextAwareWorker(worker) internal class ContextAwareWorker(private val worker: NewThreadWorker) : Worker() { - private val tracking = CompositeDisposable() - override fun schedule(runnable: Runnable, delay: Long, unit: TimeUnit): Disposable { + override fun schedule( + runnable: Runnable, + delay: Long, + unit: TimeUnit, + ): Disposable { if (isDisposed) { return Disposables.disposed() } diff --git a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/ContextManager.kt b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/ContextManager.kt index edba7832c..2dc528d75 100644 --- a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/ContextManager.kt +++ b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/ContextManager.kt @@ -7,9 +7,9 @@ package grooves.example.push * */ object ContextManager { - private val ctx = ThreadLocal>() fun get() = ctx.get() + fun set(context: Map) = ctx.set(context) } \ No newline at end of file diff --git a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Database.kt b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Database.kt index 6cebc5cac..1bea00e23 100644 --- a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Database.kt +++ b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Database.kt @@ -7,22 +7,25 @@ import org.jooq.DSLContext import org.slf4j.LoggerFactory class Database { - private val log = LoggerFactory.getLogger(this.javaClass) @Inject lateinit var dslContext: DSLContext - fun getBalance(account: Account, version: Long?): BalanceRecord? { + fun getBalance( + account: Account, + version: Long?, + ): BalanceRecord? { log.info("getBalance($account, $version)") - val balanceRecord = dslContext - .select() - .from(Tables.BALANCE) - .where(Tables.BALANCE.B_ACCOUNT.eq(account.id)) - .and(Tables.BALANCE.B_VERSION.le(version ?: Long.MAX_VALUE)) - .orderBy(Tables.BALANCE.B_VERSION.desc()) - .limit(1) - .fetchAny() as BalanceRecord? + val balanceRecord = + dslContext + .select() + .from(Tables.BALANCE) + .where(Tables.BALANCE.B_ACCOUNT.eq(account.id)) + .and(Tables.BALANCE.B_VERSION.le(version ?: Long.MAX_VALUE)) + .orderBy(Tables.BALANCE.B_VERSION.desc()) + .limit(1) + .fetchAny() as BalanceRecord? log.info("<-- \n$balanceRecord") return balanceRecord } diff --git a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/EventService.kt b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/EventService.kt index d9a7f7b6b..b5d5f86e9 100644 --- a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/EventService.kt +++ b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/EventService.kt @@ -12,7 +12,6 @@ import org.jooq.DSLContext import org.slf4j.LoggerFactory class EventService { - @Inject lateinit var database: Database @@ -45,7 +44,8 @@ class EventService { } } .withApplyEvents { balance -> true } // <5> - .withDeprecator { balance, deprecatingAccount -> /* No op */ } // <6> + .withDeprecator { balance, deprecatingAccount -> // <6> + } .withExceptionHandler { exception, balance, transaction -> // <7> log.warn("$exception occurred") @@ -54,17 +54,19 @@ class EventService { .withEventHandler(this::updateBalance) // <8> .build() // <9> - private fun updateBalance(transaction: Transaction, balance: Balance) = - when (transaction) { - is Transaction.Deposit -> { - balance.balance += transaction.amount - just(CONTINUE) - } - is Transaction.Withdraw -> { - balance.balance -= transaction.amount - just(CONTINUE) - } + private fun updateBalance( + transaction: Transaction, + balance: Balance, + ) = when (transaction) { + is Transaction.Deposit -> { + balance.balance += transaction.amount + just(CONTINUE) } + is Transaction.Withdraw -> { + balance.balance -= transaction.amount + just(CONTINUE) + } + } // end::documented[] @Suppress("unused") diff --git a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Transaction.kt b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Transaction.kt index 6959a6445..23726551f 100644 --- a/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Transaction.kt +++ b/grooves-example-pushstyle/src/main/kotlin/grooves/example/push/Transaction.kt @@ -11,11 +11,9 @@ sealed class Transaction( override var aggregate: Account?, override var timestamp: Date, override var position: Long, - override var revertedBy: RevertEvent? = null + override var revertedBy: RevertEvent? = null, ) : BaseEvent { - - override fun getAggregateObservable(): Publisher = - Flowable.fromIterable(listOfNotNull(aggregate)) + override fun getAggregateObservable(): Publisher = Flowable.fromIterable(listOfNotNull(aggregate)) data class Deposit( override val id: String, @@ -23,7 +21,7 @@ sealed class Transaction( override var timestamp: Date, override var position: Long, val atmId: String, - val amount: Long + val amount: Long, ) : Transaction(id, aggregate, timestamp, position) data class Withdraw( @@ -32,6 +30,6 @@ sealed class Transaction( override var timestamp: Date, override var position: Long, val atmId: String, - val amount: Long + val amount: Long, ) : Transaction(id, aggregate, timestamp, position) } \ No newline at end of file diff --git a/grooves-example-pushstyle/src/test/kotlin/grooves/example/pushstyle/PushTest.kt b/grooves-example-pushstyle/src/test/kotlin/grooves/example/pushstyle/PushTest.kt index 8397f0db3..6a9e662f5 100644 --- a/grooves-example-pushstyle/src/test/kotlin/grooves/example/pushstyle/PushTest.kt +++ b/grooves-example-pushstyle/src/test/kotlin/grooves/example/pushstyle/PushTest.kt @@ -11,7 +11,6 @@ import org.junit.jupiter.api.Test import java.util.concurrent.TimeUnit.SECONDS class PushTest { - private val injector = Guice.createInjector(BankingModule) private val application = injector.getInstance(Application::class.java)