Flux<String> items = ... здесь исходный Flux ... ;
Flux<Integer> numbers = Flux.generate(
() -> 0,
(state, sink) -> {
sink.next(state);
return state + 1;
}
);
items.zipWith(numbers, (i,n) -> n + i)
.map(item-> item.replace("0{", "{").replaceFirst("^[0-9]+", ", "))
.startWith("[")
.concatWithValues("]")
.subscribe(System.out::println);
public class Test {
static Mono<String> getFromHttp() {
return Mono.just("from HTTP").delayElement(Duration.ofSeconds(2));
}
static Mono<String> getFromDb() {
return Mono.just("from DB").delayElement(Duration.ofSeconds(1));
}
public static void main(String[] args) {
String fromHttp = "---";
String fromDb = "===";
long start = System.currentTimeMillis();
Mono<String> httpReq = getFromHttp();
Mono<String> dbReq = getFromDb();
Tuple2<String, String> values = Mono.zip(httpReq, dbReq).block();
fromHttp = values.getT1();
fromDb = values.getT2();
System.out.println(System.currentTimeMillis() - start);
System.out.println(fromHttp);
System.out.println(fromDb);
}
}
public class Test {
public static void main(String[] args) {
MyClass myClass = new MyClass();
Stream.generate(ToProcess::new).limit(10).map(myClass::process);
}
}
class MyClass{
ToProcess process(ToProcess obj){
// processing
return obj;
}
}
class ToProcess {
}
// GET /api/reports/download/{id}
public Publisher<Void> download(HttpServerRequest req, HttpServerResponse resp) {
try {
String filePath = reportService.getFilePath(Integer.parseInt(req.param("id")));
if (filePath == null) return resp.status(HttpResponseStatus.NOT_FOUND);
Path file = Path.of(filePath);
String filename =
URLEncoder.encode(file.getFileName().toString(), StandardCharsets.UTF_8).replace("+", "%20");
return resp
.status(HttpResponseStatus.OK)
.addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*") // CORS
.addHeader(ACCESS_CONTROL_EXPOSE_HEADERS, "filename")
.addHeader("filename", filename)
.addHeader(CONTENT_TYPE, "application/octet-stream")
.sendFileChunked(file, 0, file.toFile().length()) // or Files.size(file) ??
;
} catch (Exception e) {
return getErrorResponse(resp, e);
}
}
часть методов появляется из-за строгой типизации: для разных типов приходится писать разные методы
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
public class test {
public static void main(String[] args) {
List<Item> listA = new ArrayList<>();
List<Item> listB = new ArrayList<>();
List<Item> coupled = couple(listA, listB);
}
static List<Item> couple(List<Item> listA, List<Item> listB){
return listA.stream()
.peek(itemA -> {
Item complimentary = listB.stream().filter(itemB -> itemB.getId() == itemA.getId()).findFirst().orElse(null);
if (complimentary == null) throw new IllegalArgumentException("Can't find pair");
itemA.setLow(complimentary.getLow());
})
.toList();
}
}
@Getter
@Setter
class Item{
private int id;
private int low;
private int high;
}