From 81ec87d893db63a749828fba9af851ed07076cca Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 16 Jan 2012 02:12:20 +0100 Subject: Revert changes to Usermap --- .../src/com/earth2me/essentials/UserMap.java | 76 +++++++++------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/UserMap.java b/Essentials/src/com/earth2me/essentials/UserMap.java index c979e5046..d15438c7d 100644 --- a/Essentials/src/com/earth2me/essentials/UserMap.java +++ b/Essentials/src/com/earth2me/essentials/UserMap.java @@ -1,20 +1,22 @@ package com.earth2me.essentials; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.util.concurrent.UncheckedExecutionException; import java.io.File; -import java.lang.ref.SoftReference; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; +import java.util.Collections; import java.util.Set; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.ExecutionException; import org.bukkit.entity.Player; -public class UserMap implements IConf +public class UserMap extends CacheLoader implements IConf { private final transient IEssentials ess; - private final transient Map> users = new HashMap>(); - //CacheBuilder.newBuilder().softValues().build(this); - //private final transient ConcurrentSkipListSet keys = new ConcurrentSkipListSet(); + private final transient Cache users = CacheBuilder.newBuilder().softValues().build(this); + private final transient ConcurrentSkipListSet keys = new ConcurrentSkipListSet(); public UserMap(final IEssentials ess) { @@ -35,19 +37,16 @@ public class UserMap implements IConf { return; } - synchronized (users) + keys.clear(); + users.invalidateAll(); + for (String string : userdir.list()) { - users.clear(); - - for (String string : userdir.list()) + if (!string.endsWith(".yml")) { - if (!string.endsWith(".yml")) - { - continue; - } - final String name = string.substring(0, string.length() - 4); - users.put(Util.sanitizeFileName(name), null); + continue; } + final String name = string.substring(0, string.length() - 4); + keys.add(Util.sanitizeFileName(name)); } } }); @@ -55,43 +54,40 @@ public class UserMap implements IConf public boolean userExists(final String name) { - return users.containsKey(Util.sanitizeFileName(name)); + return keys.contains(Util.sanitizeFileName(name)); } public User getUser(final String name) { try { - synchronized (users) - { - final SoftReference softRef = users.get(Util.sanitizeFileName(name)); - User user = softRef == null ? null : softRef.get(); - if (user == null) - { - user = load(name); - users.put(name, new SoftReference(user)); - } - return user; - } + return users.get(Util.sanitizeFileName(name)); + } + catch (ExecutionException ex) + { + return null; } - catch (Exception ex) + catch (UncheckedExecutionException ex) { return null; } } + @Override public User load(final String name) throws Exception { for (Player player : ess.getServer().getOnlinePlayers()) { if (player.getName().equalsIgnoreCase(name)) { + keys.add(Util.sanitizeFileName(name)); return new User(player, ess); } } final File userFile = getUserFile(name); if (userFile.exists()) { + keys.add(Util.sanitizeFileName(name)); return new User(new OfflinePlayer(name, ess), ess); } throw new Exception("User not found!"); @@ -105,28 +101,20 @@ public class UserMap implements IConf public void removeUser(final String name) { - synchronized (users) - { - users.remove(Util.sanitizeFileName(name)); - } + keys.remove(Util.sanitizeFileName(name)); + users.invalidate(Util.sanitizeFileName(name)); } public Set getAllUniqueUsers() { - synchronized (users) - { - return new HashSet(users.keySet()); - } + return Collections.unmodifiableSet(keys); } public int getUniqueUsers() { - synchronized (users) - { - return users.size(); - } + return keys.size(); } - + public File getUserFile(final String name) { final File userFolder = new File(ess.getDataFolder(), "userdata"); -- cgit v1.2.3