From 5be59b37f08ed6dea158387b0dd548ef2612821a Mon Sep 17 00:00:00 2001 From: Thibaud Date: Tue, 19 Mar 2019 21:37:11 +0100 Subject: [PATCH] Avoid use of try-catch as control flow --- .../ovh/gasser/newshapes/shapes/SCollection.java | 14 +++++++++++--- .../java/ovh/gasser/newshapes/ui/Controller.java | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/ovh/gasser/newshapes/shapes/SCollection.java b/src/main/java/ovh/gasser/newshapes/shapes/SCollection.java index 985f472..6eddb68 100644 --- a/src/main/java/ovh/gasser/newshapes/shapes/SCollection.java +++ b/src/main/java/ovh/gasser/newshapes/shapes/SCollection.java @@ -1,8 +1,10 @@ package ovh.gasser.newshapes.shapes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import ovh.gasser.newshapes.App; -import ovh.gasser.newshapes.attributes.SelectionAttributes; import ovh.gasser.newshapes.ShapeVisitor; +import ovh.gasser.newshapes.attributes.SelectionAttributes; import ovh.gasser.newshapes.util.Streamable; import java.awt.*; @@ -11,6 +13,7 @@ import java.util.List; import java.util.Spliterator; public class SCollection extends AbstractShape implements Streamable { + private final static Logger logger = LoggerFactory.getLogger(SCollection.class); private final List children; private SCollection(Shape... shapes) { @@ -25,12 +28,17 @@ public class SCollection extends AbstractShape implements Streamable { @Override public Rectangle getBounds() { 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(); for (Shape s : children) bounds = bounds.union(s.getBounds()); return bounds; } catch (IndexOutOfBoundsException e){ - // If the SCollection is empty, set the bounds to fill the window - return new Rectangle(App.WIN_SIZE); + logger.error("getBounds(): {}"); + throw new RuntimeException(e); } } diff --git a/src/main/java/ovh/gasser/newshapes/ui/Controller.java b/src/main/java/ovh/gasser/newshapes/ui/Controller.java index 6eb77ef..8e29120 100644 --- a/src/main/java/ovh/gasser/newshapes/ui/Controller.java +++ b/src/main/java/ovh/gasser/newshapes/ui/Controller.java @@ -11,7 +11,7 @@ import java.awt.event.MouseEvent; import java.util.Optional; 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 Shape model; private Selection selection; @@ -46,7 +46,7 @@ public class Controller { s -> { if (selection != null) resetSelection(); selection = new Selection(s, true); - this.logger.debug("Selecting {}", selection.shape); + logger.debug("Selecting {}", selection.shape); }, () -> { if (selection != null) resetSelection(); @@ -56,7 +56,7 @@ public class Controller { } private void resetSelection() { - this.logger.debug("Un-selecting {}", selection.shape); + logger.debug("Un-selecting {}", selection.shape); selection.unselect(); selection = null; }