From 8c9348700a9dd64beb69369f0911dd33ba2200f9 Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 5 Jul 2020 00:38:43 +0200 Subject: [PATCH] make vars final --- src/main/java/nouritsu/Dao.java | 11 ++++------- src/main/java/nouritsu/Nouritsu.java | 13 ++++++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/nouritsu/Dao.java b/src/main/java/nouritsu/Dao.java index e2bc6a6..f7f254a 100644 --- a/src/main/java/nouritsu/Dao.java +++ b/src/main/java/nouritsu/Dao.java @@ -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 doesn’t 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. - // Why doesn’t Java just have a `Unit` type? - return true; - } + ) ); return "Registered new store “%s” with the sections [%s]".formatted(store.name(), store.sections()); } diff --git a/src/main/java/nouritsu/Nouritsu.java b/src/main/java/nouritsu/Nouritsu.java index 29027c0..9942831 100644 --- a/src/main/java/nouritsu/Nouritsu.java +++ b/src/main/java/nouritsu/Nouritsu.java @@ -51,7 +51,7 @@ public class Nouritsu extends NanoHTTPD { private static Resp processRequest(IHTTPSession session, Dao dao) { // Type inference can’t handle this :feelsBadMan: - BiFunction, Dao, Resp> handler = + final BiFunction, 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 can’t come up with a more elegant solution, + // so lots of locals it is. private static String sortBySection(HashSet 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; }