Archiviert
13
0

Merge pull request #26 from YukonAppleGeek/master

Fix ConcurrentPlayerMap to use SafeCacheBuilder
Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-12-11 03:33:14 -08:00
Commit bec5706e33
2 geänderte Dateien mit 12 neuen und 15 gelöschten Zeilen

Datei anzeigen

@ -8,12 +8,11 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import com.comphenix.protocol.utility.SafeCacheBuilder;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification; import com.google.common.cache.RemovalNotification;
@ -62,8 +61,8 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
/** /**
* A cache of the associated keys for each player. * A cache of the associated keys for each player.
*/ */
protected Cache<Object, Player> keyLookup = createKeyCache();
protected ConcurrentMap<Object, Player> keyLookup = createKeyCache();
/** /**
* The method used to retrieve a unique key for a player. * The method used to retrieve a unique key for a player.
*/ */
@ -115,8 +114,8 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
* Construct a cache of keys and the associated player. * Construct a cache of keys and the associated player.
* @return The key map. * @return The key map.
*/ */
protected Cache<Object, Player> createKeyCache() { protected ConcurrentMap<Object, Player> createKeyCache() {
return CacheBuilder.newBuilder(). return SafeCacheBuilder.newBuilder().
weakValues(). weakValues().
removalListener( removalListener(
new RemovalListener<Object, Player>() { new RemovalListener<Object, Player>() {
@ -154,7 +153,7 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
if (newPlayer != null) { if (newPlayer != null) {
// Update the reference // Update the reference
keyLookup.asMap().put(key, newPlayer); keyLookup.put(key, newPlayer);
} else { } else {
valueLookup.remove(key); valueLookup.remove(key);
} }
@ -183,8 +182,6 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
protected Player lookupPlayer(Object key) { protected Player lookupPlayer(Object key) {
try { try {
return keyLookup.get(key); return keyLookup.get(key);
} catch (ExecutionException e) {
return null;
} catch (UncheckedExecutionException e) { } catch (UncheckedExecutionException e) {
return null; return null;
} }
@ -198,7 +195,7 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
protected Object cachePlayerKey(Player player) { protected Object cachePlayerKey(Player player) {
Object key = keyMethod.apply(player); Object key = keyMethod.apply(player);
keyLookup.asMap().put(key, player); keyLookup.put(key, player);
return key; return key;
} }
@ -228,7 +225,7 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
Object playerKey = keyMethod.apply((Player) key); Object playerKey = keyMethod.apply((Player) key);
TValue value = valueLookup.remove(playerKey); TValue value = valueLookup.remove(playerKey);
keyLookup.asMap().remove(playerKey); keyLookup.remove(playerKey);
return value; return value;
} }
return null; return null;
@ -240,7 +237,7 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
Object playerKey = keyMethod.apply((Player) key); Object playerKey = keyMethod.apply((Player) key);
if (valueLookup.remove(playerKey, value)) { if (valueLookup.remove(playerKey, value)) {
keyLookup.asMap().remove(playerKey); keyLookup.remove(playerKey);
return true; return true;
} }
} }
@ -277,7 +274,7 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
@Override @Override
public void clear() { public void clear() {
valueLookup.clear(); valueLookup.clear();
keyLookup.invalidateAll(); keyLookup.clear();
} }
}; };
} }
@ -300,7 +297,7 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
if (player == null) { if (player == null) {
// Remove entries that cannot be found // Remove entries that cannot be found
source.remove(); source.remove();
keyLookup.asMap().remove(entry.getKey()); keyLookup.remove(entry.getKey());
} else { } else {
return new SimpleEntry<Player, TValue>(player, entry.getValue()); return new SimpleEntry<Player, TValue>(player, entry.getValue());
} }

Datei anzeigen

@ -199,7 +199,7 @@ public class SafeCacheBuilder<K, V> {
* @throws IllegalStateException if a removal listener was already set * @throws IllegalStateException if a removal listener was already set
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <K1 extends K, V1 extends V> SafeCacheBuilder<K1, V1> removalListener(RemovalListener<? super K, ? super V> listener) { public <K1 extends K, V1 extends V> SafeCacheBuilder<K1, V1> removalListener(RemovalListener<? super K1, ? super V1> listener) {
builder.removalListener(listener); builder.removalListener(listener);
return (SafeCacheBuilder<K1, V1>) this; return (SafeCacheBuilder<K1, V1>) this;
} }