Skip to content

Commit

Permalink
Cleanup some checkstyle/IntelliJ checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
iltep64 committed Sep 30, 2019
1 parent 0ebb733 commit bf6e1c3
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 68 deletions.
13 changes: 12 additions & 1 deletion src/main/java/org/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/duke/cmd/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ private Command(String type, String arguments, Map<String, String> namedArgument

/**
* From user input, parse it into a {@link Command}.
* <p>
*
* 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.
* <p>
*
* Each named argument starts with a switch "/(name)".
* The argument spans up til the next named argument or end of string.
*
Expand Down
35 changes: 28 additions & 7 deletions src/main/java/org/duke/cmd/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Handler> handlers
Expand All @@ -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
Expand All @@ -30,19 +34,27 @@ 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
*/
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);
}
}
Expand All @@ -67,10 +79,19 @@ public boolean dispatchCommand(String userInput) {
}
}

/**
* Returns the set of currently bound handlers.
* @return Currently bound handlers.
*/
public Set<Handler> 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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/duke/cmd/HelpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/duke/json/ArrayHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
* @param <T> Value to produce
*/
public interface ArrayHandler<T> {
/**
* Construct a {@link ValueHandler} reading a list of values, using the given value handler.
* @param valueHandler Value handler for each element.
* @param <T> Element type
* @return A {@link ValueHandler} reading an {@link ArrayList} of {@code T} values.
*/
static <T> ValueHandler<ArrayList<T>> listOf(ValueHandler<T> valueHandler) {
return new ValueHandler<ArrayList<T>>() {
return new ValueHandler<>() {
public ArrayHandler<ArrayList<T>> handleArray() {
return new ListValue<>(valueHandler);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/duke/json/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/duke/json/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* Registry mapping Java classes to appropriate JSON encoder functions.
*/
public class Registry {
private static HashMap<Class<?>, BiConsumer<JsonWriter.ValueContext, ?>> encoderMap
private static final HashMap<Class<?>, BiConsumer<JsonWriter.ValueContext, ?>> encoderMap
= new HashMap<>();
private static HashMap<Class<?>, BiConsumer<JsonWriter.ValueContext, ?>> encoderCache
private static final HashMap<Class<?>, BiConsumer<JsonWriter.ValueContext, ?>> encoderCache
= new HashMap<>();

static {
Expand All @@ -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()));
}

Expand All @@ -46,6 +46,7 @@ private static <T> BiConsumer<JsonWriter.ValueContext, T> getEncoderInner(Class<
while (!Object.class.equals(current)) {
BiConsumer<JsonWriter.ValueContext, ?> encoder = encoderMap.get(current);
if (encoder != null) {
//noinspection unchecked
return (BiConsumer<JsonWriter.ValueContext, T>) encoder;
}
current = current.getSuperclass();
Expand All @@ -57,6 +58,7 @@ private static <T> BiConsumer<JsonWriter.ValueContext, T> getEncoderInner(Class<
Class<?> c = (Class<?>) iface;
BiConsumer<JsonWriter.ValueContext, ?> encoder = encoderMap.get(c);
if (encoder != null) {
//noinspection unchecked
return (BiConsumer<JsonWriter.ValueContext, T>) encoder;
}
}
Expand All @@ -66,13 +68,15 @@ private static <T> BiConsumer<JsonWriter.ValueContext, T> getEncoderInner(Class<

BiConsumer<JsonWriter.ValueContext, ?> encoder = encoderMap.get(Object.class);
if (encoder != null) {
//noinspection unchecked
return (BiConsumer<JsonWriter.ValueContext, T>) encoder;
}
throw new JsonException("No handler for class %s", clazz);
}

public static <T> BiConsumer<JsonWriter.ValueContext, T> getEncoder(Class<? extends T> clazz) {
if (encoderCache.containsKey(clazz)) {
//noinspection unchecked
return (BiConsumer<JsonWriter.ValueContext, T>) encoderCache.get(clazz);
}
BiConsumer<JsonWriter.ValueContext, T> encoder = getEncoderInner(clazz);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/duke/json/ValueHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface ValueHandler<T> {
static <T> ValueHandler<ArrayList<T>> listOf(ValueHandler<T> elemHandler) {
return new ValueHandler<>() {
public ArrayHandler<ArrayList<T>> handleArray() {
return new ArrayHandler.ListValue<T>(elemHandler);
return new ArrayHandler.ListValue<>(elemHandler);
}
};
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/org/duke/ui/javafx/DukeFx.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/org/duke/ui/javafx/Message.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
*/
}
10 changes: 0 additions & 10 deletions src/main/java/org/duke/ui/javafx/UserInfo.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit bf6e1c3

Please sign in to comment.