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:
John Hitchings 2019-01-26 13:01:14 -08:00
parent 8d2c649250
commit b18efe2fbf
9 changed files with 212 additions and 156 deletions

View file

@ -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();
}
}
}

View file

@ -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))));
}
}

View file

@ -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);
}
}