make vars final

This commit is contained in:
kageru 2020-07-05 00:38:43 +02:00
parent d0dc275914
commit 8c9348700a
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 12 additions and 12 deletions

View File

@ -87,16 +87,13 @@ public class Dao {
public String addStore(Store store) {
// just overwrite it if we already know that store
withRedis(redis -> {
redis.del(STORE_PREFIX + store.name());
withRedis(redis ->
redis.del(STORE_PREFIX + store.name()) + // <- this plus doesnt actually add anything.
// It just allows us to do both steps in one statement in the lambda body.
redis.rpush(
STORE_PREFIX + store.name(),
store.sections().map(Section::name).toJavaArray(String[]::new)
);
// There needs to be some kind of return type unless I make a second util function with Consumer<Jedis>.
// Why doesnt Java just have a `Unit` type?
return true;
}
)
);
return "Registered new store “%s” with the sections [%s]".formatted(store.name(), store.sections());
}

View File

@ -51,7 +51,7 @@ public class Nouritsu extends NanoHTTPD {
private static Resp processRequest(IHTTPSession session, Dao dao) {
// Type inference cant handle this :feelsBadMan:
BiFunction<Map<String, String>, Dao, Resp> handler =
final BiFunction<Map<String, String>, Dao, Resp> handler =
switch (session.getUri().replaceAll("/", "").toLowerCase()) {
case "addstore" -> Nouritsu::addStore;
case "additem" -> Nouritsu::addItem;
@ -103,11 +103,14 @@ public class Nouritsu extends NanoHTTPD {
return first.flatMap(f -> second.map(s -> Tuple.of(f, s)));
}
// This kind of breaks the style,
// but I cant come up with a more elegant solution,
// so lots of locals it is.
private static String sortBySection(HashSet<ShoppingItem> items, Store store) {
var itemsBySection = items.groupBy(ShoppingItem::section);
var sorted = store.sections().flatMap(s -> itemsBySection.get(s).getOrElse(HashSet.empty()));
var rest = items.removeAll(sorted);
var formattedItems = sorted.map(ShoppingItem::name).mkString(", ");
final var itemsBySection = items.groupBy(ShoppingItem::section);
final var sorted = store.sections().flatMap(s -> itemsBySection.get(s).getOrElse(HashSet.empty()));
final var rest = items.removeAll(sorted);
final var formattedItems = sorted.map(ShoppingItem::name).mkString(", ");
if (rest.isEmpty()) {
return formattedItems;
}