Avoid use of try-catch as control flow

This commit is contained in:
Thibaud Gasser 2019-03-19 21:37:11 +01:00
parent 51885d8c53
commit 5be59b37f0
2 changed files with 14 additions and 6 deletions

View File

@ -1,8 +1,10 @@
package ovh.gasser.newshapes.shapes; package ovh.gasser.newshapes.shapes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ovh.gasser.newshapes.App; import ovh.gasser.newshapes.App;
import ovh.gasser.newshapes.attributes.SelectionAttributes;
import ovh.gasser.newshapes.ShapeVisitor; import ovh.gasser.newshapes.ShapeVisitor;
import ovh.gasser.newshapes.attributes.SelectionAttributes;
import ovh.gasser.newshapes.util.Streamable; import ovh.gasser.newshapes.util.Streamable;
import java.awt.*; import java.awt.*;
@ -11,6 +13,7 @@ import java.util.List;
import java.util.Spliterator; import java.util.Spliterator;
public class SCollection extends AbstractShape implements Streamable<Shape> { public class SCollection extends AbstractShape implements Streamable<Shape> {
private final static Logger logger = LoggerFactory.getLogger(SCollection.class);
private final List<Shape> children; private final List<Shape> children;
private SCollection(Shape... shapes) { private SCollection(Shape... shapes) {
@ -25,12 +28,17 @@ public class SCollection extends AbstractShape implements Streamable<Shape> {
@Override @Override
public Rectangle getBounds() { public Rectangle getBounds() {
try { try {
if (children.isEmpty()) {
// If the SCollection is empty, set the bounds to fill the window
return new Rectangle(App.WIN_SIZE);
}
Rectangle bounds = children.get(0).getBounds(); Rectangle bounds = children.get(0).getBounds();
for (Shape s : children) bounds = bounds.union(s.getBounds()); for (Shape s : children) bounds = bounds.union(s.getBounds());
return bounds; return bounds;
} catch (IndexOutOfBoundsException e){ } catch (IndexOutOfBoundsException e){
// If the SCollection is empty, set the bounds to fill the window logger.error("getBounds(): {}");
return new Rectangle(App.WIN_SIZE); throw new RuntimeException(e);
} }
} }

View File

@ -11,7 +11,7 @@ import java.awt.event.MouseEvent;
import java.util.Optional; import java.util.Optional;
public class Controller { public class Controller {
private final Logger logger = LoggerFactory.getLogger(ShapesView.class); private final static Logger logger = LoggerFactory.getLogger(ShapesView.class);
private final ShapesView view; private final ShapesView view;
private final Shape model; private final Shape model;
private Selection selection; private Selection selection;
@ -46,7 +46,7 @@ public class Controller {
s -> { s -> {
if (selection != null) resetSelection(); if (selection != null) resetSelection();
selection = new Selection(s, true); selection = new Selection(s, true);
this.logger.debug("Selecting {}", selection.shape); logger.debug("Selecting {}", selection.shape);
}, },
() -> { () -> {
if (selection != null) resetSelection(); if (selection != null) resetSelection();
@ -56,7 +56,7 @@ public class Controller {
} }
private void resetSelection() { private void resetSelection() {
this.logger.debug("Un-selecting {}", selection.shape); logger.debug("Un-selecting {}", selection.shape);
selection.unselect(); selection.unselect();
selection = null; selection = null;
} }