From bf6e1c3cbc00db8fa16c52288ac347754728af42 Mon Sep 17 00:00:00 2001 From: Ryan Chew Date: Mon, 30 Sep 2019 23:21:48 +0800 Subject: [PATCH] Cleanup some checkstyle/IntelliJ checks. --- src/main/java/org/duke/Duke.java | 13 ++++++- src/main/java/org/duke/cmd/Command.java | 4 +-- .../java/org/duke/cmd/CommandDispatcher.java | 35 +++++++++++++++---- src/main/java/org/duke/cmd/HelpHandler.java | 4 +-- src/main/java/org/duke/json/ArrayHandler.java | 8 ++++- src/main/java/org/duke/json/JsonWriter.java | 4 +-- src/main/java/org/duke/json/Registry.java | 12 ++++--- src/main/java/org/duke/json/ValueHandler.java | 2 +- src/main/java/org/duke/ui/javafx/DukeFx.java | 20 ++++++----- src/main/java/org/duke/ui/javafx/Message.java | 22 ------------ .../java/org/duke/ui/javafx/UserInfo.java | 10 ------ src/main/java/org/duke/util/DateParser.java | 35 ++++++++++++++----- 12 files changed, 101 insertions(+), 68 deletions(-) diff --git a/src/main/java/org/duke/Duke.java b/src/main/java/org/duke/Duke.java index 49567eb3a7..d3c2d3e053 100644 --- a/src/main/java/org/duke/Duke.java +++ b/src/main/java/org/duke/Duke.java @@ -1,6 +1,17 @@ package org.duke; -import org.duke.cmd.*; +import org.duke.cmd.ByeHandler; +import org.duke.cmd.Command; +import org.duke.cmd.CommandDispatcher; +import org.duke.cmd.DeadlineTaskHandler; +import org.duke.cmd.DeleteHandler; +import org.duke.cmd.DoneHandler; +import org.duke.cmd.EventTaskHandler; +import org.duke.cmd.FindHandler; +import org.duke.cmd.Handler; +import org.duke.cmd.HelpHandler; +import org.duke.cmd.ListHandler; +import org.duke.cmd.TodoTaskHandler; import org.duke.task.Task; import org.duke.ui.DukeIO; diff --git a/src/main/java/org/duke/cmd/Command.java b/src/main/java/org/duke/cmd/Command.java index 50dc9a9b59..7a998d0916 100644 --- a/src/main/java/org/duke/cmd/Command.java +++ b/src/main/java/org/duke/cmd/Command.java @@ -23,11 +23,11 @@ private Command(String type, String arguments, Map namedArgument /** * From user input, parse it into a {@link Command}. - *

+ * * First word is the main command type. * The main arguments are the remainder of words, * up til the first named argument or end of string. - *

+ * * Each named argument starts with a switch "/(name)". * The argument spans up til the next named argument or end of string. * diff --git a/src/main/java/org/duke/cmd/CommandDispatcher.java b/src/main/java/org/duke/cmd/CommandDispatcher.java index f738583841..30e45e6103 100644 --- a/src/main/java/org/duke/cmd/CommandDispatcher.java +++ b/src/main/java/org/duke/cmd/CommandDispatcher.java @@ -3,8 +3,12 @@ import org.duke.Duke; import org.duke.DukeException; -import java.lang.reflect.InvocationTargetException; -import java.util.*; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; public class CommandDispatcher { private final Set handlers @@ -19,7 +23,7 @@ public CommandDispatcher(Duke duke) { } /** - * Bind a command handler, for a type of command + * Bind a command handler, for a type of command. * * @param command Type of command * @param handler Handler for command @@ -30,7 +34,7 @@ public void bindCommand(String command, Handler handler) { } /** - * Bind a fallback command handler, for unknown command types + * Bind a fallback command handler, for unknown command types. * * @param handler Fallback handler for unknown commands */ @@ -38,11 +42,19 @@ public void setUnknownCommandHandler(Handler handler) { this.defaultHandler = handler; } + /** + * Bind several command handlers at once. + * + * This uses the {@link Handler.Binding} annotations on each class + * to autodiscover command types. + * @param handlers Handlers to bind. + */ public final void bindCommands(Handler... handlers) { - for(Handler handler : handlers) { - Handler.Binding[] binds = handler.getClass().getAnnotationsByType(Handler.Binding.class); + for (Handler handler : handlers) { + Handler.Binding[] binds = handler.getClass() + .getAnnotationsByType(Handler.Binding.class); this.handlers.add(handler); - for(Handler.Binding bind : binds) { + for (Handler.Binding bind : binds) { this.commandMap.put(bind.value(), handler); } } @@ -67,10 +79,19 @@ public boolean dispatchCommand(String userInput) { } } + /** + * Returns the set of currently bound handlers. + * @return Currently bound handlers. + */ public Set getHandlers() { return Collections.unmodifiableSet(this.handlers); } + /** + * Returns the handler bound to this command type. + * @param cmd Command type + * @return The bound handler, or null if no binding is found. + */ public Handler getHandlerFor(String cmd) { return commandMap.get(cmd); } diff --git a/src/main/java/org/duke/cmd/HelpHandler.java b/src/main/java/org/duke/cmd/HelpHandler.java index 77fe0a152b..18f4c11644 100644 --- a/src/main/java/org/duke/cmd/HelpHandler.java +++ b/src/main/java/org/duke/cmd/HelpHandler.java @@ -10,14 +10,14 @@ public class HelpHandler extends Handler { @Override protected void handleNoExit(Duke duke, Command command) { DukeIO io = duke.getIo(); - if(command.getArguments().isEmpty()) { + if (command.getArguments().isEmpty()) { for (Handler handler : duke.getDispatcher().getHandlers()) { io.sayBriefCommand(handler); } return; } Handler handler = duke.getDispatcher().getHandlerFor(command.getArguments()); - if(handler == null) { + if (handler == null) { throw new DukeException("No such command!"); } io.sayCommand(handler); diff --git a/src/main/java/org/duke/json/ArrayHandler.java b/src/main/java/org/duke/json/ArrayHandler.java index 27f6cb0850..3106e7d9c3 100644 --- a/src/main/java/org/duke/json/ArrayHandler.java +++ b/src/main/java/org/duke/json/ArrayHandler.java @@ -9,8 +9,14 @@ * @param Value to produce */ public interface ArrayHandler { + /** + * Construct a {@link ValueHandler} reading a list of values, using the given value handler. + * @param valueHandler Value handler for each element. + * @param Element type + * @return A {@link ValueHandler} reading an {@link ArrayList} of {@code T} values. + */ static ValueHandler> listOf(ValueHandler valueHandler) { - return new ValueHandler>() { + return new ValueHandler<>() { public ArrayHandler> handleArray() { return new ListValue<>(valueHandler); } diff --git a/src/main/java/org/duke/json/JsonWriter.java b/src/main/java/org/duke/json/JsonWriter.java index 2c69b738ce..306c6d6a41 100644 --- a/src/main/java/org/duke/json/JsonWriter.java +++ b/src/main/java/org/duke/json/JsonWriter.java @@ -15,7 +15,7 @@ public class JsonWriter implements AutoCloseable { private final Writer writer; private int indentationLevel = 0; - private ValueContext valueContext = new ValueContext(); + private final ValueContext valueContext = new ValueContext(); /** * Constructs a JsonWriter wrapping a output writer. @@ -122,7 +122,7 @@ private JsonWriter append(char literal) { /** * Flushes the underlying Writer. * - * @throws IOException + * @throws IOException Throws any exception from flushing the underlying stream. */ public void flush() throws IOException { this.writer.flush(); diff --git a/src/main/java/org/duke/json/Registry.java b/src/main/java/org/duke/json/Registry.java index 723c767911..c42f7361c7 100644 --- a/src/main/java/org/duke/json/Registry.java +++ b/src/main/java/org/duke/json/Registry.java @@ -10,9 +10,9 @@ * Registry mapping Java classes to appropriate JSON encoder functions. */ public class Registry { - private static HashMap, BiConsumer> encoderMap + private static final HashMap, BiConsumer> encoderMap = new HashMap<>(); - private static HashMap, BiConsumer> encoderCache + private static final HashMap, BiConsumer> encoderCache = new HashMap<>(); static { @@ -29,8 +29,8 @@ public class Registry { (Map) map, JsonWriter.ValueContext::writeValue)); register(Number.class, (ctx, val) -> ctx.writeNumber(val.doubleValue())); - register(String.class, (ctx, str) -> ctx.writeString(str)); - register(Boolean.class, (ctx, b) -> ctx.writeBoolean(b)); + register(String.class, JsonWriter.ValueContext::writeString); + register(Boolean.class, JsonWriter.ValueContext::writeBoolean); register(Object.class, (ctx, obj) -> ctx.writeString(obj.toString())); } @@ -46,6 +46,7 @@ private static BiConsumer getEncoderInner(Class< while (!Object.class.equals(current)) { BiConsumer encoder = encoderMap.get(current); if (encoder != null) { + //noinspection unchecked return (BiConsumer) encoder; } current = current.getSuperclass(); @@ -57,6 +58,7 @@ private static BiConsumer getEncoderInner(Class< Class c = (Class) iface; BiConsumer encoder = encoderMap.get(c); if (encoder != null) { + //noinspection unchecked return (BiConsumer) encoder; } } @@ -66,6 +68,7 @@ private static BiConsumer getEncoderInner(Class< BiConsumer encoder = encoderMap.get(Object.class); if (encoder != null) { + //noinspection unchecked return (BiConsumer) encoder; } throw new JsonException("No handler for class %s", clazz); @@ -73,6 +76,7 @@ private static BiConsumer getEncoderInner(Class< public static BiConsumer getEncoder(Class clazz) { if (encoderCache.containsKey(clazz)) { + //noinspection unchecked return (BiConsumer) encoderCache.get(clazz); } BiConsumer encoder = getEncoderInner(clazz); diff --git a/src/main/java/org/duke/json/ValueHandler.java b/src/main/java/org/duke/json/ValueHandler.java index 0a38e49147..e80fe75447 100644 --- a/src/main/java/org/duke/json/ValueHandler.java +++ b/src/main/java/org/duke/json/ValueHandler.java @@ -12,7 +12,7 @@ public interface ValueHandler { static ValueHandler> listOf(ValueHandler elemHandler) { return new ValueHandler<>() { public ArrayHandler> handleArray() { - return new ArrayHandler.ListValue(elemHandler); + return new ArrayHandler.ListValue<>(elemHandler); } }; } diff --git a/src/main/java/org/duke/ui/javafx/DukeFx.java b/src/main/java/org/duke/ui/javafx/DukeFx.java index 9df63ab519..4fb47110c8 100644 --- a/src/main/java/org/duke/ui/javafx/DukeFx.java +++ b/src/main/java/org/duke/ui/javafx/DukeFx.java @@ -8,7 +8,11 @@ import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Label; -import javafx.scene.layout.*; +import javafx.scene.layout.Border; +import javafx.scene.layout.BorderStroke; +import javafx.scene.layout.BorderStrokeStyle; +import javafx.scene.layout.BorderWidths; +import javafx.scene.layout.CornerRadii; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; @@ -129,7 +133,7 @@ private TextFlow makeDescriptionLine(Handler handler) { private Text makeArgumentNode(String text, boolean optional) { Color color; - if(optional) { + if (optional) { text = " [" + text + "]"; color = Color.GREEN; } else { @@ -150,29 +154,29 @@ public void sayCommand(Handler handler) { name.setFont(MONO_FONT); TextFlow syntaxLine = new TextFlow(name); - if(!handler.getDescriptionText().argument().isEmpty()) { + if (!handler.getDescriptionText().argument().isEmpty()) { String argDesc = handler.getDescriptionText().argument(); Text posArgument = makeArgumentNode(argDesc, handler.getDescriptionText().optional()); syntaxLine.getChildren().add(posArgument); } - for(Handler.NamedArgument namedArgument : handler.getNamedArguments()) { + for (Handler.NamedArgument namedArgument : handler.getNamedArguments()) { String argDesc = "/" + namedArgument.value() + " " + namedArgument.description(); syntaxLine.getChildren().add(makeArgumentNode(argDesc, true)); } dialogNodes.add(syntaxLine); - if(handler.getBindings().length > 1) { + if (handler.getBindings().length > 1) { Text aliasPrefix = new Text("Aliases:"); aliasPrefix.setFont(BASE_FONT); StringBuilder sb = new StringBuilder(); - for(Handler.Binding bind : handler.getBindings()) { - if(bind.value().equals(handler.getPrimaryBinding())) { + for (Handler.Binding bind : handler.getBindings()) { + if (bind.value().equals(handler.getPrimaryBinding())) { continue; } - if(sb.length() > 0) { + if (sb.length() > 0) { sb.append(','); } sb.append(' ').append(bind.value()); diff --git a/src/main/java/org/duke/ui/javafx/Message.java b/src/main/java/org/duke/ui/javafx/Message.java index ea5bfc94f3..c316f2f21d 100644 --- a/src/main/java/org/duke/ui/javafx/Message.java +++ b/src/main/java/org/duke/ui/javafx/Message.java @@ -1,6 +1,5 @@ package org.duke.ui.javafx; -import javafx.beans.property.StringProperty; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.geometry.HPos; @@ -50,25 +49,4 @@ public Message(UserInfo user, Node message) { this.getChildren().addAll(message, userIcon); } } - - public Node getMessage() { - return message; - } - - public void setMessage(Node message) { - this.message = message; - } -/* - public StringProperty textProperty() { - return messageText.textProperty(); - } - - public String getText() { - return messageText.getText(); - } - - public void setText(String value) { - messageText.setText(value); - } - */ } diff --git a/src/main/java/org/duke/ui/javafx/UserInfo.java b/src/main/java/org/duke/ui/javafx/UserInfo.java index cc80a30f4e..711ce49a7b 100644 --- a/src/main/java/org/duke/ui/javafx/UserInfo.java +++ b/src/main/java/org/duke/ui/javafx/UserInfo.java @@ -1,7 +1,6 @@ package org.duke.ui.javafx; import javafx.geometry.Insets; -import javafx.geometry.NodeOrientation; import javafx.geometry.Pos; import javafx.scene.image.Image; import javafx.scene.layout.Background; @@ -41,15 +40,6 @@ public Pos getAlignmentPosition() { return alignmentPosition; } - public NodeOrientation getOrdering() { - switch (alignmentPosition.getHpos()) { - case RIGHT: - default: - case LEFT: - return NodeOrientation.LEFT_TO_RIGHT; - } - } - public Background getBackground() { return new Background(new BackgroundFill( this.backgroundColor, CornerRadii.EMPTY, Insets.EMPTY diff --git a/src/main/java/org/duke/util/DateParser.java b/src/main/java/org/duke/util/DateParser.java index b6cb01bbb4..70bc83f596 100644 --- a/src/main/java/org/duke/util/DateParser.java +++ b/src/main/java/org/duke/util/DateParser.java @@ -1,12 +1,22 @@ package org.duke.util; import java.text.ParsePosition; -import java.time.*; +import java.time.DateTimeException; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.MonthDay; +import java.time.Year; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.format.FormatStyle; import java.time.format.TextStyle; -import java.time.temporal.*; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; +import java.time.temporal.TemporalAdjuster; +import java.time.temporal.TemporalAdjusters; +import java.time.temporal.TemporalQuery; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -52,8 +62,10 @@ mdFormatter, addYear(mdFormatter), .toFormatter(), DateTimeFormatter.ISO_LOCAL_TIME }; - private String text; - private ParsePosition pos; + + private final String text; + private final ParsePosition pos; + private DateParser(String input) { this.text = input; this.pos = new ParsePosition(0); @@ -69,8 +81,9 @@ private static DateTimeFormatter addYear(DateTimeFormatter fmt) { } /** - * Given an input string describing a date+time, return a {@link java.time.LocalDateTime} representing it. - *

+ * Given an input string describing a date+time, + * return a {@link java.time.LocalDateTime} representing it. + * * If the input specifies a specific date, we use that date. * Else, it returns the next matching date in the future. * @@ -94,7 +107,8 @@ private void eatWhitespace() { private R tryParse(DateTimeFormatter formatter, TemporalQuery query) { eatWhitespace(); - int ix = pos.getIndex(), errIx = pos.getErrorIndex(); + int ix = pos.getIndex(); + int errIx = pos.getErrorIndex(); try { TemporalAccessor parsed = formatter.parse(text, pos); R value = query.queryFrom(parsed); @@ -121,6 +135,7 @@ private R tryParses(DateTimeFormatter formatter, return value; } } catch (DateTimeException e) { + //Swallow any failed parse. } } return null; @@ -143,7 +158,7 @@ private Stream parseMonthDay(MonthDay md) { if (md.equals(MonthDay.of(2, 29))) { yearStream = yearStream.filter(y -> Year.of(y).isLeap()); } - return yearStream.mapToObj(y -> md.atYear(y)) + return yearStream.mapToObj(md::atYear) .filter(date -> date.isAfter(now)); } @@ -157,6 +172,10 @@ private LocalDateTime tryDates(Stream possibleDates, LocalTime exactT .findFirst().orElse(null); } + /** + * Parse into a {@link LocalDateTime} object based off the given user input. + * @return Best-guess {@link LocalDateTime} representing the input + */ public LocalDateTime parseDateTime() { for (DateTimeFormatter dt : dateFormatters) { LocalDateTime date = tryParses(dt,