Skip to content

Commit

Permalink
[426] Add cache for bounds
Browse files Browse the repository at this point in the history
The bounds for a node can be computed several times for a parent or a
child bounds calculation. This commit adds a cache to avoid computing it
again.

Bug: #426
  • Loading branch information
lredor committed Sep 17, 2024
1 parent 918e7ca commit 45c05a9
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -132,6 +133,8 @@ public final class GMFHelper {
*/
private static final int ICON_TEXT_GAP = 3;

private static Map<Node, Rectangle> boundsCache = new HashMap<>();

private GMFHelper() {
// Helper to not instantiate
}
Expand Down Expand Up @@ -617,9 +620,21 @@ public static Rectangle getAbsoluteBounds(Node node, boolean insetsAware, boolea
* @return the absolute bounds of the node relative to the origin (Diagram)
*/
public static Rectangle getAbsoluteBounds(Node node, boolean insetsAware, boolean boxForConnection, boolean recursiveGetBounds) {
Point location = getAbsoluteLocation(node, insetsAware);
Rectangle bounds = getBounds(node, false, false, boxForConnection, recursiveGetBounds, location);
return new PrecisionRectangle(location.preciseX(), location.preciseY(), bounds.preciseWidth(), bounds.preciseHeight());
if (!recursiveGetBounds) {
boundsCache.clear();
}
Rectangle result;
if (boundsCache.containsKey(node)) {
result = boundsCache.get(node);
} else {
Point location = getAbsoluteLocation(node, insetsAware);
Rectangle bounds = getBounds(node, false, false, boxForConnection, recursiveGetBounds, location);
result = new PrecisionRectangle(location.preciseX(), location.preciseY(), bounds.preciseWidth(), bounds.preciseHeight());
if (recursiveGetBounds) {
boundsCache.put(node, result);
}
}
return result.getCopy();
}

/**
Expand Down

0 comments on commit 45c05a9

Please sign in to comment.