Commit 3fd1da4b authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Bruk av ny instanceof-konstruksjon

parent dcc2afc9
......@@ -41,8 +41,8 @@ public class TodoList extends AbstractTodoList {
public void addTodoItems(TodoItem... items) throws IllegalStateException {
for (TodoItem item : items) {
TodoListItem todoListItem = null;
if (item instanceof TodoListItem) {
todoListItem = (TodoListItem) item;
if (item instanceof TodoListItem tli) {
todoListItem = tli;
} else {
todoListItem = new TodoListItem(this);
todoListItem.setText(item.getText());
......
......@@ -23,8 +23,7 @@ class TodoItemDeserializer extends JsonDeserializer<TodoItem> {
}
TodoItem deserialize(JsonNode jsonNode) {
if (jsonNode instanceof ObjectNode) {
ObjectNode objectNode = (ObjectNode) jsonNode;
if (jsonNode instanceof ObjectNode objectNode) {
TodoItem item = new TodoItem();
JsonNode textNode = objectNode.get("text");
if (textNode instanceof TextNode) {
......
......@@ -30,8 +30,7 @@ class TodoListDeserializer extends JsonDeserializer<AbstractTodoList> {
}
AbstractTodoList deserialize(JsonNode treeNode) {
if (treeNode instanceof ObjectNode) {
ObjectNode objectNode = (ObjectNode) treeNode;
if (treeNode instanceof ObjectNode objectNode) {
JsonNode nameNode = objectNode.get("name");
if (! (nameNode instanceof TextNode)) {
return null;
......
......@@ -27,12 +27,11 @@ class TodoModelDeserializer extends JsonDeserializer<TodoModel> {
}
TodoModel deserialize(JsonNode treeNode) {
if (treeNode instanceof ObjectNode) {
ObjectNode objectNode = (ObjectNode) treeNode;
if (treeNode instanceof ObjectNode objectNode) {
TodoModel model = new TodoModel();
JsonNode itemsNode = objectNode.get("lists");
if (itemsNode instanceof ArrayNode) {
for (JsonNode elementNode : ((ArrayNode) itemsNode)) {
if (itemsNode instanceof ArrayNode arrayNode) {
for (JsonNode elementNode : arrayNode) {
AbstractTodoList list = todoListDeserializer.deserialize(elementNode);
if (list != null) {
model.addTodoList(list);
......
......@@ -111,8 +111,8 @@ public class TodoDocumentAppController implements DocumentListener<TodoModel, Fi
private <T extends JsonNode> T getConfigProperty(String... path) {
JsonNode node = config;
for (String segment : path) {
if (node instanceof ObjectNode) {
node = ((ObjectNode) node).get(segment);
if (node instanceof ObjectNode objectNode) {
node = objectNode.get(segment);
} else {
return null;
}
......@@ -125,14 +125,14 @@ public class TodoDocumentAppController implements DocumentListener<TodoModel, Fi
Iterator<String> segments = List.of(path).iterator();
while (segments.hasNext()) {
String segment = segments.next();
if (node instanceof ObjectNode) {
if (node instanceof ObjectNode objectNode) {
if (! segments.hasNext()) {
((ObjectNode) node).set(segment, newNode);
objectNode.set(segment, newNode);
return;
} else {
ObjectNode objectNode = JsonNodeFactory.instance.objectNode();
((ObjectNode) node).set(segment, objectNode);
node = objectNode;
ObjectNode objectNode2 = JsonNodeFactory.instance.objectNode();
objectNode.set(segment, objectNode2);
node = objectNode2;
}
}
}
......
......@@ -29,8 +29,7 @@ public class TodoItemListCellDragHandler {
}
private void handleDragStart(MouseEvent event) {
if (event.getSource() instanceof TodoItemListCell) {
TodoItemListCell listCell = (TodoItemListCell) event.getSource();
if (event.getSource() instanceof TodoItemListCell listCell) {
if (!listCell.isEmpty()) {
Dragboard dragboard = listCell.startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
......@@ -42,11 +41,9 @@ public class TodoItemListCellDragHandler {
}
private void handleDragOver(DragEvent event) {
if (event.getGestureSource() instanceof TodoItemListCell
&& event.getSource() instanceof TodoItemListCell) {
TodoItem sourceItem = ((TodoItemListCell) event.getGestureSource()).getItem();
TodoItemListCell targetCell = (TodoItemListCell) event.getSource();
if ((!targetCell.isEmpty()) && acceptDrop(sourceItem, targetCell.getItem())) {
if (event.getGestureSource() instanceof TodoItemListCell sourceCell
&& event.getSource() instanceof TodoItemListCell targetCell) {
if ((!targetCell.isEmpty()) && acceptDrop(sourceCell.getItem(), targetCell.getItem())) {
event.acceptTransferModes(TransferMode.MOVE);
// TODO: give feedback that drop will be accepted
}
......@@ -60,10 +57,9 @@ public class TodoItemListCellDragHandler {
private void handleDragEnd(DragEvent event) {
boolean success = false;
if (event.getGestureSource() instanceof TodoItemListCell
&& event.getGestureTarget() instanceof TodoItemListCell) {
TodoItem sourceItem = ((TodoItemListCell) event.getGestureSource()).getItem();
TodoItemListCell targetCell = (TodoItemListCell) event.getGestureTarget();
if (event.getGestureSource() instanceof TodoItemListCell sourceCell
&& event.getGestureTarget() instanceof TodoItemListCell targetCell) {
TodoItem sourceItem = sourceCell.getItem();
if ((!targetCell.isEmpty()) && acceptDrop(sourceItem, targetCell.getItem())) {
int newIndex = todoList.indexOf(targetCell.getItem());
if (newIndex >= 0) {
......
......@@ -169,11 +169,11 @@ public class TodoListControllerTest extends ApplicationTest {
}
private TodoItemListCell findTodoItemListCell(Predicate<TodoItemListCell> test, int num) {
return (TodoItemListCell) waitForNode(node -> node instanceof TodoItemListCell && test.test((TodoItemListCell) node), num);
return (TodoItemListCell) waitForNode(node -> node instanceof TodoItemListCell todoItemListCell && test.test(todoItemListCell), num);
}
private Node findTodoItemListCellNode(Predicate<TodoItemListCell> test, String selector, int num) {
Node listCell = waitForNode(node -> node instanceof TodoItemListCell && (selector == null || node.lookup(selector) != null) && test.test((TodoItemListCell) node), num);
Node listCell = waitForNode(node -> node instanceof TodoItemListCell todoItemListCell && (selector == null || node.lookup(selector) != null) && test.test(todoItemListCell), num);
return listCell.lookup(selector);
}
......
......@@ -122,8 +122,8 @@ public class FileMenuController {
@FXML
public void handleOpenAction(final ActionEvent event) {
File selection = null;
if (event.getSource() instanceof MenuItem) {
final File file = new File(((MenuItem) event.getSource()).getText());
if (event.getSource() instanceof MenuItem menuItem) {
final File file = new File(menuItem.getText());
if (file.exists()) {
selection = file;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment