|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.function.kotlin.arity |
| 18 | + |
| 19 | +import kotlinx.coroutines.delay |
| 20 | +import kotlinx.coroutines.flow.Flow |
| 21 | +import kotlinx.coroutines.flow.flow |
| 22 | +import org.springframework.messaging.Message |
| 23 | +import org.springframework.messaging.support.MessageBuilder |
| 24 | +import org.springframework.stereotype.Component |
| 25 | +import reactor.core.publisher.Flux |
| 26 | +import reactor.core.publisher.Mono |
| 27 | +import java.time.Duration |
| 28 | +import java.util.UUID |
| 29 | + |
| 30 | +/** |
| 31 | + * Examples of implementing suppliers using Kotlin's function type. |
| 32 | + * |
| 33 | + * ## List of Combinations Implemented: |
| 34 | + * --- Coroutine --- |
| 35 | + * 1. () -> R -> supplierKotlinPlain |
| 36 | + * 2. () -> Flow<R> -> supplierKotlinFlow |
| 37 | + * 3. suspend () -> R -> supplierKotlinSuspendPlain |
| 38 | + * 4. suspend () -> Flow<R> -> supplierKotlinSuspendFlow |
| 39 | + * --- Reactor --- |
| 40 | + * 5. () -> Mono<R> -> supplierKotlinMono |
| 41 | + * 6. () -> Flux<R> -> supplierKotlinFlux |
| 42 | + * --- Message<T> --- |
| 43 | + * 7. () -> Message<R> -> supplierKotlinMessage |
| 44 | + * 8. () -> Mono<Message<R>> -> supplierKotlinMonoMessage |
| 45 | + * 9. suspend () -> Message<R> -> supplierKotlinSuspendMessage |
| 46 | + * 10. () -> Flux<Message<R>> -> supplierKotlinFluxMessage |
| 47 | + * 11. () -> Flow<Message<R>> -> supplierKotlinFlowMessage |
| 48 | + * 12. suspend () -> Flow<Message<R>> -> supplierKotlinSuspendFlowMessage |
| 49 | + * |
| 50 | + * @author Adrien Poupard |
| 51 | + */ |
| 52 | +class KotlinSupplierKotlinExamples |
| 53 | + |
| 54 | +/** 1) () -> R */ |
| 55 | +@Component |
| 56 | +class SupplierKotlinPlain : () -> Int { |
| 57 | + override fun invoke(): Int { |
| 58 | + return 42 |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** 2) () -> Flow<R> */ |
| 63 | +@Component |
| 64 | +class SupplierKotlinFlow : () -> Flow<String> { |
| 65 | + override fun invoke(): Flow<String> { |
| 66 | + return flow { |
| 67 | + emit("A") |
| 68 | + emit("B") |
| 69 | + emit("C") |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** 3) suspend () -> R */ |
| 75 | +@Component |
| 76 | +class SupplierKotlinSuspendPlain : suspend () -> String { |
| 77 | + override suspend fun invoke(): String { |
| 78 | + return "Hello from suspend" |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** 4) suspend () -> Flow<R> */ |
| 83 | +@Component |
| 84 | +class SupplierKotlinSuspendFlow : suspend () -> Flow<String> { |
| 85 | + override suspend fun invoke(): Flow<String> { |
| 86 | + return flow { |
| 87 | + emit("x") |
| 88 | + emit("y") |
| 89 | + emit("z") |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** 5) () -> Mono<R> */ |
| 95 | +@Component |
| 96 | +class SupplierKotlinMono : () -> Mono<String> { |
| 97 | + override fun invoke(): Mono<String> { |
| 98 | + return Mono.just("Hello from Mono").delayElement(Duration.ofMillis(50)) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** 6) () -> Flux<R> */ |
| 103 | +@Component |
| 104 | +class SupplierKotlinFlux : () -> Flux<String> { |
| 105 | + override fun invoke(): Flux<String> { |
| 106 | + return Flux.just("Alpha", "Beta", "Gamma").delayElements(Duration.ofMillis(20)) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** 7) () -> Message<R> */ |
| 111 | +@Component |
| 112 | +class SupplierKotlinMessage : () -> Message<String> { |
| 113 | + override fun invoke(): Message<String> { |
| 114 | + return MessageBuilder.withPayload("Hello from Message") |
| 115 | + .setHeader("messageId", UUID.randomUUID().toString()) |
| 116 | + .build() |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** 8) () -> Mono<Message<R>> */ |
| 121 | +@Component |
| 122 | +class SupplierKotlinMonoMessage : () -> Mono<Message<String>> { |
| 123 | + override fun invoke(): Mono<Message<String>> { |
| 124 | + return Mono.just( |
| 125 | + MessageBuilder.withPayload("Hello from Mono Message") |
| 126 | + .setHeader("monoMessageId", UUID.randomUUID().toString()) |
| 127 | + .setHeader("source", "mono") |
| 128 | + .build() |
| 129 | + ).delayElement(Duration.ofMillis(40)) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** 9) suspend () -> Message<R> */ |
| 134 | +@Component |
| 135 | +class SupplierKotlinSuspendMessage : suspend () -> Message<String> { |
| 136 | + override suspend fun invoke(): Message<String> { |
| 137 | + return MessageBuilder.withPayload("Hello from Suspend Message") |
| 138 | + .setHeader("suspendMessageId", UUID.randomUUID().toString()) |
| 139 | + .setHeader("wasSuspended", true) |
| 140 | + .build() |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/** 10) () -> Flux<Message<R>> */ |
| 145 | +@Component |
| 146 | +class SupplierKotlinFluxMessage : () -> Flux<Message<String>> { |
| 147 | + override fun invoke(): Flux<Message<String>> { |
| 148 | + return Flux.just("Msg1", "Msg2") |
| 149 | + .delayElements(Duration.ofMillis(30)) |
| 150 | + .map { payload -> |
| 151 | + MessageBuilder.withPayload(payload) |
| 152 | + .setHeader("fluxMessageId", UUID.randomUUID().toString()) |
| 153 | + .build() |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/** 11) () -> Flow<Message<R>> */ |
| 159 | +@Component |
| 160 | +class SupplierKotlinFlowMessage : () -> Flow<Message<String>> { |
| 161 | + override fun invoke(): Flow<Message<String>> { |
| 162 | + return flow { |
| 163 | + listOf("FlowMsg1", "FlowMsg2").forEach { payload -> |
| 164 | + emit( |
| 165 | + MessageBuilder.withPayload(payload) |
| 166 | + .setHeader("flowMessageId", UUID.randomUUID().toString()) |
| 167 | + .build() |
| 168 | + ) |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +/** 12) suspend () -> Flow<Message<R>> */ |
| 175 | +@Component |
| 176 | +class SupplierKotlinSuspendFlowMessage : suspend () -> Flow<Message<String>> { |
| 177 | + override suspend fun invoke(): Flow<Message<String>> { |
| 178 | + return flow { |
| 179 | + listOf("SuspendFlowMsg1", "SuspendFlowMsg2").forEach { payload -> |
| 180 | + emit( |
| 181 | + MessageBuilder.withPayload(payload) |
| 182 | + .setHeader("suspendFlowMessageId", UUID.randomUUID().toString()) |
| 183 | + .build() |
| 184 | + ) |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments