forked from External/mage
Migrate from older version of google collections to guava. Required migration from MapMaker to CacheBuilder. See: https://github.com/google/guava/wiki/MapMakerMigration
This commit is contained in:
parent
8d2c649250
commit
b18efe2fbf
9 changed files with 212 additions and 156 deletions
|
|
@ -1,20 +1,21 @@
|
|||
package mage.client.components;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.MapMaker;
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import javax.swing.JPanel;
|
||||
import mage.client.util.ImageCaches;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import org.jdesktop.swingx.graphics.GraphicsUtilities;
|
||||
import org.jdesktop.swingx.graphics.ShadowRenderer;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
|
||||
import mage.client.util.ImageCaches;
|
||||
import mage.client.util.SoftValuesLoadingCache;
|
||||
|
||||
/**
|
||||
* Mage round pane with transparency. Used for tooltips.
|
||||
*
|
||||
|
|
@ -27,13 +28,12 @@ public class MageRoundPane extends JPanel {
|
|||
private final Color defaultBackgroundColor = new Color(141, 130, 112, 200); // color of the frame of the popup window
|
||||
private Color backgroundColor = defaultBackgroundColor;
|
||||
private static final int alpha = 0;
|
||||
private static final Map<ShadowKey, BufferedImage> SHADOW_IMAGE_CACHE;
|
||||
private static final Map<Key, BufferedImage> IMAGE_CACHE;
|
||||
private static final SoftValuesLoadingCache<ShadowKey, BufferedImage> SHADOW_IMAGE_CACHE;
|
||||
private static final SoftValuesLoadingCache<Key, BufferedImage> IMAGE_CACHE;
|
||||
|
||||
static {
|
||||
SHADOW_IMAGE_CACHE = ImageCaches.register(new MapMaker().softValues().makeComputingMap((Function<ShadowKey, BufferedImage>) key -> createShadowImage(key)));
|
||||
|
||||
IMAGE_CACHE = ImageCaches.register(new MapMaker().softValues().makeComputingMap((Function<Key, BufferedImage>) key -> createImage(key)));
|
||||
SHADOW_IMAGE_CACHE = ImageCaches.register(SoftValuesLoadingCache.from(MageRoundPane::createShadowImage));
|
||||
IMAGE_CACHE = ImageCaches.register(SoftValuesLoadingCache.from(MageRoundPane::createImage));
|
||||
}
|
||||
|
||||
private final static class ShadowKey {
|
||||
|
|
@ -136,7 +136,7 @@ public class MageRoundPane extends JPanel {
|
|||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
g.drawImage(IMAGE_CACHE.get(new Key(getWidth(), getHeight(), X_OFFSET, Y_OFFSET, backgroundColor)), 0, 0, null);
|
||||
g.drawImage(IMAGE_CACHE.getOrThrow(new Key(getWidth(), getHeight(), X_OFFSET, Y_OFFSET, backgroundColor)), 0, 0, null);
|
||||
}
|
||||
|
||||
private static BufferedImage createImage(Key key) {
|
||||
|
|
@ -150,7 +150,7 @@ public class MageRoundPane extends JPanel {
|
|||
Graphics2D g2 = image.createGraphics();
|
||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
BufferedImage shadow = SHADOW_IMAGE_CACHE.get(new ShadowKey(w, h));
|
||||
BufferedImage shadow = SHADOW_IMAGE_CACHE.getOrThrow(new ShadowKey(w, h));
|
||||
|
||||
{
|
||||
int xOffset = (shadow.getWidth() - w) / 2;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@
|
|||
package mage.client.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -10,20 +11,20 @@ import java.util.Map;
|
|||
*/
|
||||
public final class ImageCaches {
|
||||
|
||||
private final static ArrayList<Map> IMAGE_CACHES;
|
||||
private final static ArrayList<Cache<?, ?>> IMAGE_CACHES;
|
||||
|
||||
static {
|
||||
IMAGE_CACHES = new ArrayList<>();
|
||||
}
|
||||
|
||||
public static Map register(Map map) {
|
||||
public static <C extends Cache<K, V>, K, V> C register(C map) {
|
||||
IMAGE_CACHES.add(map);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void flush() {
|
||||
for (Map map : IMAGE_CACHES) {
|
||||
map.clear();
|
||||
for (Cache<?, ?> map : IMAGE_CACHES) {
|
||||
map.invalidateAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
package mage.client.util;
|
||||
|
||||
import static com.google.common.cache.CacheBuilder.newBuilder;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.ForwardingLoadingCache;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
|
||||
public class SoftValuesLoadingCache<K, V> extends ForwardingLoadingCache<K, Optional<V>> {
|
||||
|
||||
private final LoadingCache<K, Optional<V>> cache;
|
||||
|
||||
public SoftValuesLoadingCache(CacheLoader<K, Optional<V>> loader) {
|
||||
cache = newBuilder().softValues().build(loader);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LoadingCache<K, Optional<V>> delegate() {
|
||||
return cache;
|
||||
}
|
||||
|
||||
public V getOrThrow(K key) {
|
||||
V v = getOrNull(key);
|
||||
if (v == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public V getOrNull(K key) {
|
||||
try {
|
||||
return get(key).orElse(null);
|
||||
} catch (ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public V peekIfPresent(K key) {
|
||||
Optional<V> value = getIfPresent(key);
|
||||
if (value != null) {
|
||||
return value.orElse(null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <K, V> SoftValuesLoadingCache<K, V> from(CacheLoader<K, Optional<V>> loader) {
|
||||
return new SoftValuesLoadingCache<>(loader);
|
||||
}
|
||||
|
||||
public static <K, V> SoftValuesLoadingCache<K, V> from(Function<K, V> loader) {
|
||||
return from(CacheLoader.from(k -> Optional.ofNullable(loader.apply(k))));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -5,16 +5,16 @@
|
|||
*/
|
||||
package mage.client.util;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.MapMaker;
|
||||
import com.mortennobel.imagescaling.ResampleOp;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.GraphicsConfiguration;
|
||||
import java.awt.GraphicsDevice;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Transparency;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.mortennobel.imagescaling.ResampleOp;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -68,19 +68,20 @@ public final class TransformedImageCache {
|
|||
}
|
||||
}
|
||||
|
||||
private static final Map<Key, Map<BufferedImage, BufferedImage>> IMAGE_CACHE;
|
||||
private static final SoftValuesLoadingCache<Key, SoftValuesLoadingCache<BufferedImage, BufferedImage>> IMAGE_CACHE;
|
||||
|
||||
static {
|
||||
// TODO: can we use a single map?
|
||||
IMAGE_CACHE = ImageCaches.register(new MapMaker().softValues().makeComputingMap((Function<Key, Map<BufferedImage, BufferedImage>>) key -> new MapMaker().weakKeys().softValues().makeComputingMap(image -> {
|
||||
if (key.width != image.getWidth() || key.height != image.getHeight()) {
|
||||
image = resizeImage(image, key.width, key.height);
|
||||
}
|
||||
if (key.angle != 0.0) {
|
||||
image = rotateImage(image, key.angle);
|
||||
}
|
||||
return image;
|
||||
})));
|
||||
IMAGE_CACHE = ImageCaches.register(SoftValuesLoadingCache.from(
|
||||
key -> SoftValuesLoadingCache.from(image -> {
|
||||
if (key.width != image.getWidth() || key.height != image.getHeight()) {
|
||||
image = resizeImage(image, key.width, key.height);
|
||||
}
|
||||
if (key.angle != 0.0) {
|
||||
image = rotateImage(image, key.angle);
|
||||
}
|
||||
return image;
|
||||
})));
|
||||
}
|
||||
|
||||
private static BufferedImage rotateImage(BufferedImage image, double angle) {
|
||||
|
|
@ -145,6 +146,6 @@ public final class TransformedImageCache {
|
|||
if (resHeight < 3) {
|
||||
resHeight = 3;
|
||||
}
|
||||
return IMAGE_CACHE.get(new Key(resWidth, resHeight, angle)).get(image);
|
||||
return IMAGE_CACHE.getOrThrow(new Key(resWidth, resHeight, angle)).getOrThrow(image);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue