[Refactoring] small refactoring

This commit is contained in:
Thibaud Gasser 2019-10-06 12:41:09 +02:00
parent 96f120d943
commit e94c2a50d2
5 changed files with 15 additions and 12 deletions

View File

@ -8,7 +8,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.List; import java.util.List;
public class ImageController<T> extends Controller<List<Image>> implements ActionListener { public class ImageController extends Controller<List<Image>> implements ActionListener {
private int index; private int index;

View File

@ -8,6 +8,7 @@ import java.awt.*;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -27,9 +28,9 @@ public class ImageLoader extends SwingWorker<List<Image>, Integer> {
private void loadImages(File current) throws NullPointerException { private void loadImages(File current) throws NullPointerException {
if (current.isFile()) { if (current.isFile()) {
final Image icon = ImageLib.createScaledImage(current, this.iconWidth, -1); final Optional<Image> icon = ImageLib.createScaledImage(current, this.iconWidth, -1);
if (icon != null) { if (icon.isPresent()) {
images.add(icon); images.add(icon.get());
publish(images.size()); publish(images.size());
} }
return; return;
@ -43,7 +44,7 @@ public class ImageLoader extends SwingWorker<List<Image>, Integer> {
} }
@Override @Override
protected List<Image> doInBackground() throws Exception { protected List<Image> doInBackground() {
loadImages(root); loadImages(root);
return images; return images;
} }
@ -51,7 +52,7 @@ public class ImageLoader extends SwingWorker<List<Image>, Integer> {
@Override @Override
protected void process(List<Integer> data) { protected void process(List<Integer> data) {
if (this.isCancelled()) return; if (this.isCancelled()) return;
app.setStatus(String.valueOf(data.get(data.size() - 1)) + " images loaded"); app.setStatus(data.get(data.size() - 1) + " images loaded");
} }
@Override @Override

View File

@ -5,19 +5,20 @@ import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
public final class ImageLib { public final class ImageLib {
public static Image createScaledImage(File file, int iconWidth, int iconHeight) { public static Optional<Image> createScaledImage(File file, int iconWidth, int iconHeight) {
final BufferedImage img; final BufferedImage img;
try { try {
if ((img = ImageIO.read(file)) != null) { if ((img = ImageIO.read(file)) != null) {
return img.getScaledInstance(iconWidth, iconHeight, Image.SCALE_DEFAULT); return Optional.of(img.getScaledInstance(iconWidth, iconHeight, Image.SCALE_DEFAULT));
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return Optional.empty();
} }
private ImageLib() {} private ImageLib() {}

View File

@ -7,7 +7,7 @@ import javax.swing.*;
public class View<T> extends JPanel { public class View<T> extends JPanel {
private final T model; private final T model;
private Controller controller; private Controller<T> controller;
protected View(T model) { protected View(T model) {
this.model = model; this.model = model;
@ -17,7 +17,7 @@ public class View<T> extends JPanel {
return model; return model;
} }
protected Controller getController() { protected Controller<T> getController() {
return controller; return controller;
} }

View File

@ -16,12 +16,13 @@ public class AStarWorker extends SwingWorker<List<Node>, List<Node>> {
private final Node start; private final Node start;
private final Node goal; private final Node goal;
private final Grid target; private final Grid target;
private List<Node> path; private List<Node> path = Collections.emptyList();
public AStarWorker(Node start, Node goal, Grid target) { public AStarWorker(Node start, Node goal, Grid target) {
this.start = start; this.start = start;
this.goal = goal; this.goal = goal;
this.target = target; this.target = target;
Thread.currentThread().setName("AStarWorker");
} }
@Override @Override