Singleton object should not have map with values that can't be GCed

This commit is contained in:
vraskulin 2017-02-08 19:50:57 +03:00
parent 166c6a3590
commit 12820b54d4

View file

@ -1,5 +1,7 @@
package mage.client.util.gui; package mage.client.util.gui;
import com.google.common.collect.MapMaker;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.*; import java.util.*;
@ -7,7 +9,7 @@ import java.util.List;
/** /**
* Class for dealing with arrows in the game. * Class for dealing with arrows in the game.
* *
* @author nantuko, noxx * @author nantuko, noxx
*/ */
public class ArrowBuilder { public class ArrowBuilder {
@ -18,31 +20,25 @@ public class ArrowBuilder {
instance = new 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() { public static ArrowBuilder getBuilder() {
return instance; 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. * Get the panel where all arrows are being drawn.
*
* @return * @return
*/ */
public JPanel getArrowsManagerPanel() { public JPanel getArrowsManagerPanel() {
@ -58,7 +54,7 @@ public class ArrowBuilder {
} }
return arrowsManagerPanel; return arrowsManagerPanel;
} }
private JPanel getArrowsPanel(UUID gameId) { private JPanel getArrowsPanel(UUID gameId) {
if (!arrowPanels.containsKey(gameId)) { if (!arrowPanels.containsKey(gameId)) {
JPanel arrowPanel = new JPanel(); JPanel arrowPanel = new JPanel();
@ -73,20 +69,9 @@ public class ArrowBuilder {
return arrowPanels.get(gameId); 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. * Adds new arrow.
* *
* @param startX * @param startX
* @param startY * @param startY
* @param endX * @param endX
@ -100,18 +85,25 @@ public class ArrowBuilder {
arrow.setColor(color); arrow.setColor(color);
arrow.setArrowLocation(startX, startY, endX, endY); 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) arrow.setBounds(0, 0, Math.max(startX, endX) + 40, Math.max(startY, endY) + 30); // 30 is offset for arrow heads (being cut otherwise)
p.add(arrow);
synchronized (map) { Map<Type, List<Arrow>> innerMap = map.computeIfAbsent(gameId, k -> new HashMap<>());
p.add(arrow); List<Arrow> arrows = innerMap.computeIfAbsent(type, k -> new ArrayList<>());
Map<Type, java.util.List<Arrow>> innerMap = map.computeIfAbsent(gameId, k -> new HashMap<Type, List<Arrow>>()); arrows.add(arrow);
java.util.List<Arrow> arrows = innerMap.computeIfAbsent(type, k -> new ArrayList<Arrow>());
arrows.add(arrow);
}
p.revalidate(); p.revalidate();
p.repaint(); 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. * Removes all arrows from the screen.
*/ */
@ -119,15 +111,13 @@ public class ArrowBuilder {
if (map.containsKey(gameId)) { if (map.containsKey(gameId)) {
Map<Type, List<Arrow>> innerMap = map.get(gameId); Map<Type, List<Arrow>> innerMap = map.get(gameId);
JPanel p = getArrowsPanel(gameId); JPanel p = getArrowsPanel(gameId);
synchronized (map) { if (p != null && p.getComponentCount() > 0) {
if (p != null && p.getComponentCount() > 0) { p.removeAll();
p.removeAll(); p.revalidate();
p.revalidate(); p.repaint();
p.repaint();
}
innerMap.clear();
map.remove(gameId);
} }
innerMap.clear();
map.remove(gameId);
} }
} }
@ -137,18 +127,16 @@ public class ArrowBuilder {
java.util.List<Arrow> arrows = innerMap.get(type); java.util.List<Arrow> arrows = innerMap.get(type);
if (arrows != null && !arrows.isEmpty()) { if (arrows != null && !arrows.isEmpty()) {
JPanel p = getArrowsPanel(gameId); JPanel p = getArrowsPanel(gameId);
synchronized (map) { for (Arrow arrow : arrows) {
for (Arrow arrow : arrows) { p.remove(arrow);
p.remove(arrow);
}
innerMap.put(type, new ArrayList<Arrow>());
} }
innerMap.put(type, new ArrayList<>());
p.revalidate(); p.revalidate();
p.repaint(); p.repaint();
} }
} }
} }
public void setSize(int width, int height) { public void setSize(int width, int height) {
this.currentWidth = width; this.currentWidth = width;
this.currentHeight = height; this.currentHeight = height;
@ -173,4 +161,8 @@ public class ArrowBuilder {
} }
} }
public enum Type {
PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS
}
} }