实际代码is here
private Function<String, Mono<? extends Offer>> keyToOffer(RedisReactiveCommands<String, String> commands) {
return key -> {
Flux<KeyValue<String, String>> values = commands.hmget(key, "price", "description");
Map<String, String> map = new HashMap<>(3);
return values.reduce(map, (all, keyValue) -> {
all.put(keyValue.getKey(), keyValue.getValue());
return all;
})
.map(ConvertibleValues::of)
.flatMap(entries -> {
String description = entries.get("description", String.class).orElseThrow(() -> new IllegalStateException("No description"));
BigDecimal price = entries.get("price", BigDecimal.class).orElseThrow(() -> new IllegalStateException("No price"));
Flowable<Pet> findPetFlowable = petClient.find(key).toFlowable();
return Mono.from(findPetFlowable).map(pet -> new Offer(pet, description, price));
});
};
}
我已经尝试了各种不同的方式将上述转换为常规,到目前为止,所有尝试都不太顺利。我想知道有什么习惯使用groovy的人可以帮助
我的尝试没有发布,因为代码本身首先在Intelij中返回歧义代码块,其次看起来完全错误。
private Function<String, Mono<? extends Orders>> keyToOrder(RedisReactiveCommands<String, String> commands) {
return { key -> {
Flux<KeyValue<String, String>> values = commands.hmget(key, "price", "description");
Map map = [:]
return values.reduce(map, (all, keyValue)= {all.put(keyValue.getKey(), keyValue.getValue());
return all
}).map({entries -> ConvertibleValues.of(entries)})
.flatMap({entries -> {
String description = entries.get("description", String.class).orElseThrow({ new IllegalStateException("No description")});
BigDecimal price = entries.get("price", BigDecimal.class).orElseThrow({new IllegalStateException("No price")});
Flowable<Item> findItemFlowable = itemClient.find(key).toFlowable();
return Mono.from(findItemFlowable).map({item -> new Orders(item, description, price)});
}});
}}
}
当试图转换为常规时,最大的挣扎似乎在周围:
return values.reduce(map, (all, keyValue)= {all.put(keyValue.getKey(), keyValue.getValue());
return all
这与原始Java代码的外观完全不同,并且真的不确定它是否会按预期运行。我遇到的问题是找到任何以常规方式编写的RXJAVA Flux .reduce。
歧义代码块位于底部的整个flatMap段中
.flatMap({entries -> {
坦率地说,这很尴尬,我没有检查此更改,也没有发布。
我也遇到过: http://reactivex.io/documentation/operators/reduce.html#collapseRxGroovy
numbers.reduce({ a, b -> a+b }).
最后以:
Map<String, String> map = new HashMap<>(3);
return values.reduce({all, keyValue->
all.put(keyValue.getKey(), keyValue.getValue());
return all
}).map({entries -> ConvertibleValues.of(entries)})
但是这看起来又是错误的,并且与Java代码所做的并不完全匹配。
最后的编辑建议我让Intelij接受该代码为常规代码,但由于它甚至没有使用声明的map,所以不太确定这是否是Java代码的实际作用:
private Function<String, Mono<? extends Orders>> keyToOrder(RedisReactiveCommands<String, String> commands) {
Flux<KeyValue<String, String>> values = commands.hmget(key, "price", "description");
Map<String, String> map = new HashMap<>(3);
values.reduce({all, keyValue->
all.put(keyValue.getKey(), keyValue.getValue());
return all
}).map({entries -> ConvertibleValues.of(entries)})
.flatMap({entries -> bindEntry(entries)});
return values.key
}
private Mono<Orders> bindEntry(entries) {
String description = entries.get("description", String.class).orElseThrow({ new IllegalStateException("No description")});
BigDecimal price = entries.get("price", BigDecimal.class).orElseThrow({new IllegalStateException("No price")});
Flowable<Item> findItemFlowable = itemClient.find(key).toFlowable();
return Mono.from(findItemFlowable).map({item -> new Orders(item, description, price)});
}
答案 0 :(得分:2)
The likely issue you're facing is because Groovy doesn't support Java method references or lambdas.
The first line is returning a lambda
Java: return key -> {
Groovy: return { key - >
That is using a groovy closure that takes the key as the argument.
Other places that use method references need to be converted
Java: .map(ConvertibleValues::of)
Groovy: .map({ values -> ConvertibleValues.of(values) })
It seems like you have most of that worked out, however you specifically asked about the map not being used. That is because you simply aren't passing it to the method.
values.reduce({all, keyValue->
vs
values.reduce(map, {all, keyValue ->