mirror of
https://github.com/correl/mage.git
synced 2024-12-25 03:00:15 +00:00
Singleton object should not have map with values that can't be GCed
This commit is contained in:
parent
166c6a3590
commit
12820b54d4
1 changed files with 47 additions and 55 deletions
|
@ -1,5 +1,7 @@
|
|||
package mage.client.util.gui;
|
||||
|
||||
import com.google.common.collect.MapMaker;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
@ -7,7 +9,7 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* Class for dealing with arrows in the game.
|
||||
*
|
||||
*
|
||||
* @author nantuko, noxx
|
||||
*/
|
||||
public class ArrowBuilder {
|
||||
|
@ -18,31 +20,25 @@ public class ArrowBuilder {
|
|||
instance = new ArrowBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores arrow panels per game
|
||||
*/
|
||||
private final Map<UUID, JPanel> arrowPanels = new HashMap<>();
|
||||
private final Map<UUID, Map<Type, List<Arrow>>> map = new MapMaker().weakKeys().weakValues().makeMap();
|
||||
/**
|
||||
* The top panel where arrow panels are added to.
|
||||
*/
|
||||
private JPanel arrowsManagerPanel;
|
||||
private int currentWidth;
|
||||
private int currentHeight;
|
||||
|
||||
public static ArrowBuilder getBuilder() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* The top panel where arrow panels are added to.
|
||||
*/
|
||||
private JPanel arrowsManagerPanel;
|
||||
|
||||
/**
|
||||
* Stores arrow panels per game
|
||||
*/
|
||||
private final Map<UUID, JPanel> arrowPanels = new HashMap<UUID, JPanel>();
|
||||
|
||||
private final Map<UUID, Map<Type, List<Arrow>>> map = new HashMap<UUID, Map<Type, java.util.List<Arrow>>>();
|
||||
|
||||
private int currentWidth;
|
||||
private int currentHeight;
|
||||
|
||||
public enum Type {
|
||||
PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the panel where all arrows are being drawn.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public JPanel getArrowsManagerPanel() {
|
||||
|
@ -58,7 +54,7 @@ public class ArrowBuilder {
|
|||
}
|
||||
return arrowsManagerPanel;
|
||||
}
|
||||
|
||||
|
||||
private JPanel getArrowsPanel(UUID gameId) {
|
||||
if (!arrowPanels.containsKey(gameId)) {
|
||||
JPanel arrowPanel = new JPanel();
|
||||
|
@ -73,20 +69,9 @@ public class ArrowBuilder {
|
|||
return arrowPanels.get(gameId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Not synchronized method for arrows panel.
|
||||
* Doesn't create JPanel in case the panel doesn't exist.
|
||||
* Works faster.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
/*public JPanel getPanelRef() {
|
||||
return arrowsManagerPanel;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Adds new arrow.
|
||||
*
|
||||
*
|
||||
* @param startX
|
||||
* @param startY
|
||||
* @param endX
|
||||
|
@ -100,18 +85,25 @@ public class ArrowBuilder {
|
|||
arrow.setColor(color);
|
||||
arrow.setArrowLocation(startX, startY, endX, endY);
|
||||
arrow.setBounds(0, 0, Math.max(startX, endX) + 40, Math.max(startY, endY) + 30); // 30 is offset for arrow heads (being cut otherwise)
|
||||
|
||||
synchronized (map) {
|
||||
p.add(arrow);
|
||||
Map<Type, java.util.List<Arrow>> innerMap = map.computeIfAbsent(gameId, k -> new HashMap<Type, List<Arrow>>());
|
||||
java.util.List<Arrow> arrows = innerMap.computeIfAbsent(type, k -> new ArrayList<Arrow>());
|
||||
arrows.add(arrow);
|
||||
}
|
||||
|
||||
p.add(arrow);
|
||||
Map<Type, List<Arrow>> innerMap = map.computeIfAbsent(gameId, k -> new HashMap<>());
|
||||
List<Arrow> arrows = innerMap.computeIfAbsent(type, k -> new ArrayList<>());
|
||||
arrows.add(arrow);
|
||||
p.revalidate();
|
||||
p.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Not synchronized method for arrows panel.
|
||||
* Doesn't create JPanel in case the panel doesn't exist.
|
||||
* Works faster.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
/*public JPanel getPanelRef() {
|
||||
return arrowsManagerPanel;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Removes all arrows from the screen.
|
||||
*/
|
||||
|
@ -119,15 +111,13 @@ public class ArrowBuilder {
|
|||
if (map.containsKey(gameId)) {
|
||||
Map<Type, List<Arrow>> innerMap = map.get(gameId);
|
||||
JPanel p = getArrowsPanel(gameId);
|
||||
synchronized (map) {
|
||||
if (p != null && p.getComponentCount() > 0) {
|
||||
p.removeAll();
|
||||
p.revalidate();
|
||||
p.repaint();
|
||||
}
|
||||
innerMap.clear();
|
||||
map.remove(gameId);
|
||||
if (p != null && p.getComponentCount() > 0) {
|
||||
p.removeAll();
|
||||
p.revalidate();
|
||||
p.repaint();
|
||||
}
|
||||
innerMap.clear();
|
||||
map.remove(gameId);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,18 +127,16 @@ public class ArrowBuilder {
|
|||
java.util.List<Arrow> arrows = innerMap.get(type);
|
||||
if (arrows != null && !arrows.isEmpty()) {
|
||||
JPanel p = getArrowsPanel(gameId);
|
||||
synchronized (map) {
|
||||
for (Arrow arrow : arrows) {
|
||||
p.remove(arrow);
|
||||
}
|
||||
innerMap.put(type, new ArrayList<Arrow>());
|
||||
for (Arrow arrow : arrows) {
|
||||
p.remove(arrow);
|
||||
}
|
||||
innerMap.put(type, new ArrayList<>());
|
||||
p.revalidate();
|
||||
p.repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setSize(int width, int height) {
|
||||
this.currentWidth = width;
|
||||
this.currentHeight = height;
|
||||
|
@ -173,4 +161,8 @@ public class ArrowBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue